2011-06-11 13 views
13

के अनुसार लिनक्स में कई फाइलों का नाम बदलना मैं एमवी कमांड के साथ तीन चीजें करने की कोशिश कर रहा हूं, लेकिन यह सुनिश्चित नहीं है कि यह संभव है? शायद एक स्क्रिप्ट की जरूरत है। मुझे समझ नहीं आ रहा कि कैसे लिखूं। सभी फाइलें एक ही फ़ोल्डर में हैं।एक पैटर्न

1) फ़ाइलें v9.zip के साथ समाप्त सिर्फ ज़िप होना चाहिए (v9 हटाया)

2) युक्त फ़ाइलें _ होना चाहिए -

3) अपरकेस अक्षर एक छोटा अक्षर के बगल के साथ फ़ाइलें (या अपरकेस के बगल में लोअरकेस) उनके बीच एक जगह होनी चाहिए। तो MoveOverNow अब से अधिक ले जाएँ किया जाएगा और भगोड़ा दूर चला की जाएगी [AZ] [az] या [az] [AZ] हो जाता है [AZ] [az] और [az] [AZ]

+0

मुझे यकीन नहीं है कि आप ऐसा करने के लिए एमवी का उपयोग करेंगे ... शायद सशर्त परिदृश्यों के अनुसार, एक स्क्रिप्ट समाधान देखें। – quakkels

+2

आपका अंतिम बिंदु underspecified लगता है। यह निर्धारित करने का नियम क्या है कि 'ruNaway' 'ruN away' या 'ru Naway' होना चाहिए? –

+0

1 और 2 को 'नाम बदलें' का उपयोग करके तुच्छ होना चाहिए। 3 अधिक शामिल है इसलिए कुछ स्क्रिप्टिंग की आवश्यकता होगी। आप किस शेल का उपयोग करते हैं, बाश? –

उत्तर

8

मेरा पसंदीदा समाधान my own rename script है । सबसे सरल उदाहरण है कि अपनी समस्याओं को नक्शे ये हैं:

% rename 's/_/-/g' * 
% rename 's/(\p{Lower})(\p{Upper})/$1 $2/g' * 

हालांकि मैं वास्तव में मेरे फ़ाइल नाम में खाली स्थान के नफरत है, विशेष रूप से खड़ी खाली स्थान के:

% rename 's/\s//g' * 
% rename 's/\v//g' * 

वगैरह। यह लैरी वॉल द्वारा एक पटकथा पर आधारित है, लेकिन, विकल्पों के साथ बढ़ाया में के रूप में:

usage: /home/tchrist/scripts/rename [-ifqI0vnml] [-F file] perlexpr [files] 
    -i   ask about clobbering existent files 
    -f   force clobbers without inquiring 
    -q   quietly skip clobbers without inquiring 
    -I   ask about all changes 
    -0   read null-terminated filenames 
    -v   verbosely says what its doing 
    -V   verbosely says what its doing but with newlines between old and new filenames 
    -n   don't really do it 
    -m   to always rename 
    -l   to always symlink 
    -F path  read filelist to change from magic path(s) 

जैसा कि आप देख, यह न सिर्फ फाइलों का नाम बदल सकते हैं, लेकिन जहां सांकेतिक लिंक एक ही पैटर्न का उपयोग कर ओर इशारा करते हैं । आपको s/// पैटर्न का उपयोग करने की आवश्यकता नहीं है, हालांकि अक्सर कोई होता है।

other tools in that directory ज्यादातर यूनिकोड काम के लिए हैं, जिनमें से कुछ सुपर-उपयोगी हैं।

+1

उह वाह, मुझे यकीन नहीं है कि मुझे इस तरह की चरम शक्ति के लिए उचित रूप से लाइसेंस प्राप्त है। आपका बहुत बहुत धन्यवाद! –

+2

training.perl.com/scripts/rename का लिंक टूटा हुआ है। –

9

मैंने इनका परीक्षण नहीं किया है, इसलिए मैंने आदेशों के सामने echo डाल दिया ताकि आप उन्हें वास्तविक रूप से चलाने के लिए गूंज को हटाने से पहले उन्हें आजमा सकें।

1)

for f in *v9.zip; do echo mv "${f}" "${f%v9.zip}.zip"; done 

2)

for f in *_*; do echo mv "${f}" "${f//_/-}"; done 

अपने तीसरे समस्या के रूप में मुझे यकीन है कि यह भी किया जा सकता है लेकिन शायद कच्चे खोल से एक और अधिक परिष्कृत दृष्टिकोण एक-लाइनर्स में मदद मिलेगी कर रहा हूँ , जैसा कि @ क्रिसमस का उल्लेख है।

+1

+1 क्योंकि पहली पंक्ति ने मुझे इस समस्या के मेरे सरल संस्करण में मदद की। धन्यवाद। – TecBrat

+1

यह वही था जो मुझे चाहिए था। यह मुझे आदेश को हल करने की इजाजत देता है जब तक कि यह सही नहीं था तब गूंज और 'आग' को हटा दें! – ABrowne

3

ऊपर जवाब डेबियन, Ubuntu के लिए लागू आदि

RHEL और सह के लिए: नाम बदलने from_pattern to_pattern फ़ाइलों

20

वहाँ सबसे Debian/Ubuntu आधारित distros के साथ प्रदान की एक rename आदेश जिसके आधार पर रॉबिन बार्कर द्वारा लिखा गया था है 1 99 8 (लैरी) से लैरी वॉल का मूल कोड।

यहाँ प्रलेखन से एक अंश है:

"rename" renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames 
    specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input. 

    For example, to rename all files matching "*.bak" to strip the extension, you might say 

      rename 's/\.bak$//' *.bak 

    To translate uppercase names to lower, you'd use 

      rename 'y/A-Z/a-z/' * 

यह तो आप आकार से मिलान करने के लिए पर्ल भाव का उपयोग कर सकते पर्ल का उपयोग करता है, वास्तव में मेरा मानना ​​है कि यह tchrist की पटकथा की तरह काम करता है।

थोक फ़ाइल नामकरण के लिए उपकरणों का एक अन्य वास्तव में उपयोगी सेट renameutils collection by Oskar Liljeblad है। स्रोत कोड फ्री सॉफ्टवेयर फाउंडेशन द्वारा होस्ट किया जाता है। इसके अतिरिक्त कई डिस्ट्रोज़ (विशेष रूप से डेबियन/उबंटू आधारित डिस्ट्रोज़) में इन उपकरणों के साथ renameutils पैकेज है।

उन distros में से एक पर आप के साथ स्थापित कर सकते हैं:

$ qmv 

यह की सूची के साथ खुला एक पाठ संपादक पॉप जाएगा:

$ sudo apt-get install renameutils 

और फिर फ़ाइलों का नाम बदलना सिर्फ इस आदेश चला फाइलें, और आप उन्हें अपने संपादक की खोज के साथ जोड़ सकते हैं और फ़ंक्शन को प्रतिस्थापित कर सकते हैं।

2

मुझे लगता है कि लिंक टूटा हुआ है और मुझे वेबच्राइव में पृष्ठ को tchrist के पोस्ट में नामकरण स्क्रिप्ट में नहीं मिला, इसलिए यहां पर्ल में एक और है।

#!/usr/bin/perl 
# -w switch is off bc HERE docs cause erroneous messages to be displayed under 
# Cygwin 
#From the Perl Cookbook, Ch. 9.9 
# rename - Larry's filename fixer 
$help = <<EOF; 
Usage: rename expr [files] 

This script's first argument is Perl code that alters the filename 
(stored in \$_) to reflect how you want the file renamed. It can do 
this because it uses an eval to do the hard work. It also skips rename 
calls when the filename is untouched. This lets you simply use 
wildcards like rename EXPR * instead of making long lists of filenames. 

Here are five examples of calling the rename program from your shell: 

% rename 's/\.orig$//' *.orig 
% rename 'tr/A-Z/a-z/ unless /^Make/' * 
% rename '$_ .= ".bad"' *.f 
% rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i' * 
% find /tmp -name '*~' -print | rename 's/^(.+)~$/.#$1/' 

The first shell command removes a trailing ".orig" from each filename. 

The second converts uppercase to lowercase. Because a translation is 
used rather than the lc function, this conversion won't be locale- 
aware. To fix that, you'd have to write: 

% rename 'use locale; $_ = lc($_) unless /^Make/' * 

The third appends ".bad" to each Fortran file ending in ".f", something 
a lot of us have wanted to do for a long time. 

The fourth prompts the user for the change. Each file's name is printed 
to standard output and a response is read from standard input. If the 
user types something starting with a "y" or "Y", any "foo" in the 
filename is changed to "bar". 

The fifth uses find to locate files in /tmp that end with a tilde. It 
renames these so that instead of ending with a tilde, they start with 
a dot and a pound sign. In effect, this switches between two common 
conventions for backup files 
EOF 

$op = shift or die $help; 
chomp(@ARGV = <STDIN>) unless @ARGV; 
for (@ARGV) { 
    $was = $_; 
    eval $op; 
    die [email protected] if [email protected]; 
    rename($was,$_) unless $was eq $_; 
} 
संबंधित मुद्दे