Folders are used to store multiple files in Linux. We can rename files one by one or rename all files in a folder. Linux provides different commands and methods to rename all files in a folder. The for loop
, find
command or rename
command can be used to rename all files in a folder.
Rename All Files Using for Loop
The Linux bash provides for loop
in order to iterate over multiple items. We can iterate over all files in Linux folders and change files name at every step. In the following example, we list and read file names into the FILE
and then use the mv
command to rename.
$ for FILE in *; do mv $FILE New_$FILE; done
Rename All Files Using find Command
Another way to rename all files in a folder is by using the find
command. The find command can find all files and then rename them with the mv
command.
$ find . -type -f -name '*' -printf "echo mv '%h/%f' '%h/Unix_%f\n'" | bash
Rename All Files with rename Command
The rename
command is created to rename multiple files. We can use it with the following syntax to rename all files in a folder. In the following example, we rename all files by prefixing their existing names with “New”.
$ rename 's/^/New_/' *