Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename all files in a folder with a prefix in a single command

Tags:

linux

unix

rename

Rename all the files within a folder with prefix "Unix_"

Suppose a folder has two files

a.txt b.pdf 

then they both should be renamed from a single command to

Unix_a.txt Unix_b.pdf 
like image 660
vasanthi Avatar asked Jun 13 '11 10:06

vasanthi


People also ask

Is there a way to rename multiple files at once with different names?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.

Can you change the name of multiple files at once?

Right-click on the first file in the folder, then click “Rename.” 3. Type the new name for the file, then press the Tab key on your keyboard. This will simultaneously save the file's new name, then select the following file so you can instantly start typing a new name for that as well.


1 Answers

If your filenames contain no whitepace and you don't have any subdirectories, you can use a simple for loop:

$ for FILENAME in *; do mv $FILENAME Unix_$FILENAME; done  

Otherwise use the convenient rename command (which is a perl script) - although it might not be available out of the box on every Unix (e.g. OS X doesn't come with rename).

A short overview at debian-administration.org:

  • Easily renaming multiple files

If your filenames contain whitespace it's easier to use find, on Linux the following should work:

$ find . -type f -name '*' -printf "echo mv '%h/%f' '%h/Unix_%f\n'" | sh 

On BSD systems, there is no -printf option, unfortunately. But GNU findutils should be installable (on e.g. Mac OS X with brew install findutils).

$ gfind . -type f -name '*' -printf "mv \"%h/%f\" \"%h/Unix_%f\"\n" | sh 
like image 123
miku Avatar answered Sep 25 '22 00:09

miku