How to rename all files in a folder
And finally I would like to remove all dots (.) except the one at the end (.mp4) What commands should I use to create such script ?
Useful commands are grep, sed, and awk. What have you tried, and did it work, or fail? Have you searched for answers, with a search such as google.com/search?q=mass+rename+linux ? BTW, Linux 14.04 has reached end-of-life ubuntu.com/about/release-cycle and we are limited to provide support to current versions askubuntu.com/help/on-topic , such as 16.04 LTS, 18.04 LTS, 19.04, and alternate flavors ubuntu.com/download/flavours
«What commands should I use to create such script ?» script?! we got a «rename» command for that 😉 Have a look at man rename and man rename.ul .
I use Krusader file manager with krename . menu is file-multirename . you can prefilter using menu view-custom before that .
4 Answers 4
Firstly: Install rename by running the following command in the terminal:
Secondly: In the terminal, cd to the directory containing your files.
Finally: Rename the files to your desired format by running the following command in the terminal:
To see how the rename command will operate on your files but without really renaming them ( just print the output to the terminal ), you can add the option -n after it. Like so:
Explanation — as requested by Hamza:
Substitutes the ORIGINAL string with the NEW string.
To see how it works as simple as it gets:
- Please run in the terminal rename -n ‘s/file.number1.2010.720p.otherinfo.mp4/NEW.mp4/’ *
- Assuming you have a file named file.number1.2010.720p.otherinfo.mp4 in the current directory.
- The output would be rename(file.number1.2010.720p.otherinfo.mp4, NEW.mp4)
Starts at the beginning of the string ^ and then matches one or more character/s ( any character ) (.+) before the dot \.
This group is put into a variable $1 and repeated four more times ( five in total ) each group is put into a variable ( $2 , $3 , $4 , $5 ) until .mp4 represented as \.mp4 is reached.
Makes sure the string ends with .mp4 by using the $ symbol which matches the end of the string.
This part is, however, a bit flexible and will give you undesired results if the file naming is inconsistent and you have files with more than five parts separated by dots in their names like file.number1.2010.720p.otherinfo.extrainfo.mp4
If this is the case, please substitute this part with the more strict regular expression below. This way it will only operate on files with five parts separated by dots in their names only:
Defines the new file name as what is under the variable for the first group $1 ( in this case file ) + what is under the variable for the second group $2 ( in this case number(x) ) + .mp4
Operates on all the files in the current directory.
Rename all files in a folder with a prefix in a single command
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
Also would recommend for f in *; do [[ -f $
If you quote variables as you should, then for FILENAME in *; do mv «$FILENAME» «Unix_$FILENAME»; done works 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.
Try the rename command in the folder with the files:
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.
this is awesome! i’d suggest to add folder/* , because * is a bit dangerous if command accidently will be repeated in another place
I think this is just what you’er looking for:
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)
Beware of processing the output of ls — it can lead to problems if there are spaces or other oddball characters in the file names.
is 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
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.