Batch rename in linux

Batch rename files

I want to batch re-name a number of files in a directory so that the preceding number and hypen are stripped from the file name.

Old file name: 2904495-XXX_01_xxxx_20130730235001_00000000.NEW New file name: XXX_01_xxxx_20130730235001_00000000.NEW 

9 Answers 9

It gets from the beginning the block 6 (that is, numbers) many times, then the hyphen — and deletes it from the file name.

If rename is not in your machine, you can use a loop and mv :

Test

$ ls 23-aa hello aaa23-aa $ rename 's/^7*-//;' * $ ls aa hello aaa23-aa 
$ ls 23-a aa23-a hello $ for f in *; > do > mv "$f" "$" > done $ ls a aa23-a hello 

rename is in util-linux on Fedora and Debian releases, but doesn’t seem to be included by default on OSX.

Yep, the mv command works for me. It seems there must be two versions of rename — mine requires three arguments ( rename from to file ).

I think this command would better if you execute the command below:

ls * | sed -e 'p;s/old-name/new-name/' | xargs -n2 mv 

Here
ls * — lists files in curent folder
sed -e — executes expression
p; — prints old file name
s/old-name/new-name/ — produce new filename
xargs -n2 — handles two arguments to mv
mv — gets two parameters and do move operation

Recommendation: before executing mv verify what you do is what you want to achieve with echo.

ls * | sed -e 'p;s/old-name/new-name/' | xargs -n2 echo 

Following example renames

SCCF099_FG.gz5329223404623884757.tmp to
SCCF099_FG.gz

ls *tmp | sed -e 'p;s/\(6\)\+\.tmp/ /g' | xargs -n2 echo ls *tmp | sed -e 'p;s/\(6\)\+\.tmp/ /g' | xargs -n2 mv 

I see this is old; but can you break down what’s going on in this line? Here’s my understanding: in the call to sed , if say there are 5 files present, the expression string prints each line and then prints its find-and-replaced copy immediately below; then, xargs parses this list of 10 items now 2-at-a-time, and passes them as arguments to mv . Is that right?

Slight variation I used for filenames containing spaces: find . -print0 | sed -z -e ‘p;s/old-name/new-name/’ | xargs -n2 -0 mv .

If the first numbers are always the same length:

for F in *new ; do mv $F $ done 

The $ does a substring expansion — takes the string starting at the 8th character.

There are many other string edits available in expansions to handle other cases.

Using renamer (Windows, Mac and Linux friendly):

This will strip all numbers and first hyphen from the start of all files in the current directory.

node package manager seems having issues with package dependencies, so it didn’t install just one command, and npm didn’t install dependencies in auto mode.

i just ran npm install -g renamer — it installed correctly and works fine.. please ensure you have the latest node.js installed.. if you’re still having problems, raise an issue here

This might look a little complex, but it’s pretty effective and works well on both *nix and OSX systems. It also acts recursively, renaming files in the current directory as well as any subdirectories:

find . -regex '.*/2\[-].*' -print > temp1 && \ cp temp1 temp2 && \ vi -c ":g/\(7\[-]\)\(.*\)/s//\2/" -c ":x" temp2 && \ paste temp1 temp2 > temp3 && \ vi -c ":g/^/s//mv /" -c ":x" temp3 && \ sh ./temp3 && \ rm temp1 temp2 temp3 

Here’s a breakdown of what just happened:

Читайте также:  Astra linux уровень целостности ssh

The first line says to find ( find ) all files, starting with those in the current directory ( . ), whose name matches the pattern ( -regex ) of «7 numbers, followed by a dash, followed by 0 or more characters» ( ‘.*/2\[-].*’ ), and write those file names and their respective paths ( -print ) to a file called temp1 ( > temp1 ). Note that the -print directive is probably not necessary in most cases but it shouldn’t hurt anything.

find . -regex '.*/1\[-].*' -print > temp1 && \ 

Then, copy ( cp ) the contents of temp1 to a file called temp2.

Next, open the file temp2 using the vi text editor and give vi two commands (using -c to signify each new command):

  1. Command #1:
    • Search each line of temp2 ( :g ) for the same pattern we searched for above, except this time group the results using parentheses ( \(4\[-]\)\(.*\) ).
    • For each matching line, move the cursor to where the match was found and replace the whole match with only the second group of the matched pattern ( \2 ).
  2. Command #2:
    • Save the changes made to temp2 and close the file ( 😡 ).

The result of which being this:

Now, concatenate the lines from temp1 with those of temp2 ( paste ) and write each newly combined line to a file called temp3 ( > temp3 ).

Next, run vi again, doing the same steps as above except this time search for the beginning of each line ( ^ ) inside the file temp3 and add mv and one space right after it ( mv ).

Then, execute the contents of temp3 ( ./temp3 ) as a shell script ( sh ).

Finally, remove ( rm ) each of the temporary files we created during the whole process.

Источник

How to Rename a Batch of Files in Linux

The “mv” command is generally used in Linux to rename a single file. But sometimes, it requires to rename a batch of files. Many utilities exist in Linux to do this task. Some of them are “rename”, “imv”, “qmv”, “mmv”, etc. These are not installed in Linux by default. These utilities are required to install to rename a batch of files. The batch of files can also be removed using the built-in “mv” command with the loop or other commands. The different ways to rename a batch of files in Linux are shown in this tutorial.

Rename Multiple Files Using the “MV” Command

Using the “mv” command is the easiest way to rename multiple files because this is a built-in command of Linux and it is not required to install before use. One or more files can be renamed using the “mv” command with a loop. The method of renaming multiple files using the “mv” command is shown in the following example.

Example: Rename the File Using the “For” Loop

Create a Bash file with the following script that renames the extension of all text files into doc files using the “for” loop and “mv” command. The list of all text files is printed first. Next, the “for” loop is used to iterate the list of all text files and change the extension of each text file into a doc using the “mv” command.

#!/bin/bash
#!/bin/bash
#print the list of all text files
ls * .txt

#Iterate the loop to read all text files
for value in ` ls * .txt ` ;
do
#Read the basename of the file
filename = ` basename $value .txt `
#Rename all files to doc files
mv $value $filename .doc;
done

Читайте также:  Linux getting mac address

#Print all doc files
ls * .doc

The following output appears after executing the previous command. According to the following output, there are four text files in the current location which are renamed to doc files:

Install the Rename Utility

The rename utility is another option of Linux to rename a batch of files using a regular expression pattern. If the rename utility is not installed by default, run the following command to install it:

Some uses of the “rename” command are mentioned in the following examples.

Example 1: Rename the Extension of the Batch of Files

The method of renaming the extension of doc files into docx files using the “rename” command is shown in this example.

The following command changes the “.doc” extension of all doc files into “.docx”:

Next, the “ls” command checks whether the task is done properly or not.

The following output appears after executing the previous commands. According to the following output, the extension of four files is changed into a “.docx” extension. These are t1.docx, t2.docx, t3.docx, and t4.docx.

Example 2: Rename the Particular Files with the Extension into Uppercase

The method of changing the name of all files with the extension that starts with the word “test” into all uppercase letters using the “rename” command is shown in this example.

The following command prints the list of all files and folders of the current location:

The following command renames the name of all files into uppercase which starts with the word “test”:

Next, the “ls” command checks whether the task is done properly or not.

The following output appears after executing the previous command. According to the following output, there are four files in the current location that starts with the word, “test”. These are test.txt, testdata.txt, testdata2.txt, and testfile.txt. These filenames are changed into all uppercase letters:

Install the MMV Utility

The mmv is another utility of Linux to rename a batch of files using wildcards. Any part of the filename can be added or removed using the “mmv” command. If the mmv utility is not installed by default, run the following command to install it:

One use of the “mmv” command is mentioned in the following example.

Example: Rename the Extension of the Batch of Files

The method of changing the extension of all text files from uppercase letters to lowercase letters using the “mmv” command is shown in this example.

The following command prints the list of all files and folders of the current location:

The following command changes the “.TXT” extension of all text files into “.txt”:

Next, the “ls” command checks whether the task is done properly or not.

The following output appears after executing the previous command. According to the output, the extensions of three text files are changed:

Install the Renameutils in Linux

The renameutils is another utility of Linux to rename a batch of files. If the renameutils utility is not installed by default, run the following command to install it:

The “qmv” is one of the commands of the renameutils utility that renames the files of a directory using the editor. The use of the “qmv” command is mentioned in the following example.

Читайте также:  Ie tab для linux

Example: Rename the Batch of Files Using the QMV Editor

The temp folder is selected here to rename the files of this folder. The following command prints the list of all files and folders of the temp folder:

The following command opens the files of the temp folder in the editor for editing:

The following similar editor is opened after executing the previous command. Here, the extensions of two text files are changed. These are t2.doc and t4.doc:

Next, the “ls” command checks whether the task is done properly or not.

The following output appears after executing the previous commands. According to the output, the extensions of two text files are changed:

Conclusion

The different ways of renaming the batch of files in Linux are shown in this tutorial using the “mv”, “rename”, “mmv”, and “qmv” commans. We hope that the Linux users will be able to rename a batch of files after reading this tutorial properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

Batch rename files in Linux

How I can rename images with ‘rename’ command from «something_full.jpg» to «something_500.jpg» recursive?

2 Answers 2

Basically, you can use the rename tool for that. It should come in a Perl-based version with Debian-based Linux distros, but you can easily download it from source as well (obviously, you need to make it executable first with chmod +x ).

The following command will replace the _full part with _500 on all JPG files in the current directory.

To do this recursively, starting from your current directory, use rename with find .

find . -type f -iname "*.jpg" -exec rename 's/_full/_500/' <> \; 

Note: You may want to test the command before actually executing is. In order to do that, add the -n option to rename (e.g. between rename and the ‘s//’ string).

@nik Why would you trade the security offered by find against something that involves piping constructs (and may fail due to spaces in filenames, etc.)? I have found the latter to be insecure and hard to read at best.

You may want to look into zmv. zmv is a zsh specific feature, and requires autoload -U zmv to be somewhere in your .zshrc file.

EDIT: As an example, the task the OP posed could be solved with

An excerpt from the zshcontrib man page:

zmv [ -finqQsvwW ] [ -C | -L | -M | -p program ] [ -o optstring ] srcpat dest

Move (usually, rename) files matching the pattern srcpat to corresponding files having names of the form given by dest, where srcpat contains parentheses surrounding patterns which will be replaced in turn by $1, $2, . in dest. For example,

zmv ‘(*).lis’ ‘$1.txt’

renames foo.lis to foo.txt , my.old.stuff.lis to my.old.stuff.txt , and so on.

The pattern is always treated as an EXTENDED_GLOB pattern. Any file whose name is not changed by the substitution is simply ignored. Any error (a substitution resulted in an empty string, two substitutions gave the same result, the destination was an existing regular file and -f was not given) causes the entire function to abort without doing anything.

EXTENDED_GLOB patterns are tremendously powerful. Here is a good primer on the concept.

Источник

Оцените статью
Adblock
detector