Rename all files in a folder with a prefix in a single command
Solution 1
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:
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
Solution 2
Try the rename command in the folder with the files:
rename 's/^/Unix_/' *
The argument of rename (sed s command) indicates to replace the regex ^ with Unix_. The caret (^) is a special character that means start of the line.
Solution 3
I think this is just what you'er looking for:
ls | xargs -I {} mv {} Unix_{}
Yes, it is simple yet elegant and powerful, and also one-liner. You can get more detailed intro from me on the page:Rename Files and Directories (Add Prefix)
Solution 4
I recently faced this same situation and found an easier inbuilt solution. I am sharing it here so that it might help other people looking for solution.
With OS X Yosemite, Apple has integrated the batch renaming capabilities directly into Finder. Details information is available here. I have copied the steps below as well,
Rename multiple items
Select the items, then Control-click one of them.
In the shortcut menu, select Rename Items.
-
In the pop-up menu below Rename Folder Items, choose to replace text in the names, add text to the names, or change the name format.
Replace text: Enter the text you want to remove in the Find field, then enter the text you want to add in the “Replace with” field.
Add text: Enter the text to you want to add in the field, then choose to add the text before or after the current name.
Format: Choose a name format for the files, then choose to put the index, counter, or date before or after the name. Enter a name in the Custom Format field, then enter the number you want to start with.
Click Rename.
If you have a common pattern in your files than you can use Replace text otherwise Add text would also do the job.
Solution 5
You can just use -i instead of -I {}
ls | xargs -i mv {} unix_{}
This also works perfectly.
-
ls- lists all the files in the directory -
xargs- accepts all files line by line due to the-ioption -
{}is the placeholder for all files, necessary ifxargsgets more than two arguments as input
Using awk:
ls -lrt | grep '^-' | awk '{print "mv "$9" unix_"$9""}' | sh
Related videos on Youtube
vasanthi
Updated on July 08, 2022Comments
-
vasanthi 4 monthsRename all the files within a folder with prefix
"Unix_"Suppose a folder has two files
a.txt b.pdfthen they both should be renamed from a single command to
Unix_a.txt Unix_b.pdf-
red eyes dev over 11 yearscheck this link => cyberciti.biz/tips/…
-
Vijay almost 10 yearscheck here:theunixshell.blogspot.com/2013/01/…
-
-
miku about 9 years@Matteo: Thanks the hint: updated my answer with a warning plus two examples withfind. -
twalberg about 9 yearsAlso would recommendfor f in *; do [[ -f ${f} ]] && mv ...; doneto catch only files (no sub-directories, links, etc.)... -
Mogsdad over 6 yearsYou should go through all your answers about your utility, and add the disclaimer that you are its author. -
Jahid over 6 years@Mogsdad : I don't think its that of a necessary info. If someone wants to find the author, it's pretty easy. -
user3000868 over 6 yearsExcellent - saved me a lot of time!
-
mcont almost 6 yearsCould you explain what's/^/.../'means? -
RoyalleBlue almost 6 years@Matteo
's/^/.../'is the perl expression argument for the rename command -
JamPow over 5 years
-iis deprecated, now is just-I -
Abror Esonaliev almost 5 yearsit's helped me on Debian 8.Thaks
-
Jonathan Leffler over 4 yearsIf you quote variables as you should, thenfor FILENAME in *; do mv "$FILENAME" "Unix_$FILENAME"; doneworks correctly regardless of what characters are in the file names. It does move directories, sockets, symlinks and other file types too; I presume that doesn't matter. -
Jonathan Leffler over 4 yearsBeware of processing the output ofls— it can lead to problems if there are spaces or other oddball characters in the file names. -
Aloso over 3 yearsThank you for this tool. For some reason,renamedoesn't work for me, butrnmdoes. -
Systems Rebooter over 3 yearsthis is awesome! i'd suggest to add folder/* , because * is a bit dangerous if command accidently will be repeated in another place -
Felipe Plazas about 3 yearsbrew install rename @OliverPearmain
-
DenisKolodin almost 3 yearsExample to replace a prefix:rename 's/^start_/run_' * -
Sebastian Kaczmarek over 2 yearsBased on your Linux distribution you may want to try to installrename,prenameorperl-renamepackages (they all work the same way) -
nullability almost 2 yearsSomehow this added a.before all my files -
kkgarg over 1 yearRequires installingrenamepackage in linux -
kkgarg over 1 yearGreat solution for Mac users -
Rishab Prasad about 1 yearis it possible to replace certain chracter(s) while renaming. For example, if the file name is 2.0.2.CR1.zip, it should become 2.0.2.GA.zip -
Dr Phil 8 monthsas of March 2022, the link to debian-administration.org doesn't work. -
miku 8 months@DrPhil, Wayback Machine to the rescue.