How to Rename File and Directory in Linux?
To rename a file there are other commands also like 'mv'. But 'rename' command is slightly advanced then others. This command will be rarely used and it works differently on different distros of linux. We'll work on Debian/Ubuntu examples.
Generally, renaming is not a big task, but when you want to rename a large group of files at once then it will be difficult to rename it with 'mv' command. In these cases, it is adviced to use 'rename' command. It can convert upper case files to lower case files and vice versa and cn overwrite files using perl expressions. This command is a part of perl script.
This ('s/old-name/new-name/') is the PCRE (perl compatible regular expression) which denotes files to rename and how.
Let's see an example of basic rename command:
In the example below we have converted all the files ending with '.txt' into files ending with '.pdf'.
In another example, we have converted file into document for all files ending with .pdf
rename has some optional arguments but a mandatory perl expression that comes with every opption and guides it how to work.
Renaming Files using the mv Command
mv is a command of Unix that moves one or multiple directories or files from one position to another. If filenames are on a similar filesystem, it provides a general file rename. The content of the file is copied to the fresh location, and the previous file is deleted. Using the mv command needs the user to contain write permission for directories the file would move between. It is due to the mv command modifying both directory's content (the target and the source) involved in the moving process. When using mv on files positioned on a similar filesystem, the timestamp of the file is not updated.
The mv, ln, and cp commands are implemented as one program using hard-linked binaries on UNIX implementations copied from AT&T UNIX. The behaviour is chosen from the argv path name. It is a basic method by which closely associated commands that have been bundled as a unit permit the user to describe the specific course of the desired action.
Options of the mv Command
Most mv versions support:
The syntax of mv is mentioned below:
For instance, to rename the myfile1.txt file as myfile2.txt, we would execute the below command:
Rename multiple files using the mv command
The command can only rename one file at once, but it can be utilized in conjunction with many commands like find or in Bash while or for loops for renaming multiple files.
The below example displays how to apply Bash for loop for renaming every file in our current directory by modifying their extensions:
Let's explain the code step-by-step:
Two rename command versions are available with different syntaxes. If we don't have it installed on our system, we can install it easily with the package manager of our distribution:
The following are some more basic examples of how to implement the rename command:
Moving versus removing and copying
Moving files in a similar file system are basically implemented differently as compared to copying the file and deleting the original. A new link is included in the new directory, and the actual one is removed on platforms that don't provide support for the rename syscall. The file data is not accessed. Every POSIX-conformant system operates the rename call.
An original move is dramatically faster as compared to the copy-and-move process. The i-number of the file doesn't change. No permission is needed for reading the file being carried insofar as it's only cataloguing data that's being modified as an outcome of the "move". Since the target and source directories are being changed, entries are being established in the target directory and removed from inside the source directory.
In both directories, "write" permission is needed to finish the move. Carrying files from a single file system to another may entirely fail or may be performed automatically as an atomic copy-and-delete function. The original details are reliant on the implementation.
Next TopicLinux install Command