Rename a file in Linux Or Moving Files

I have a file called “file” in my directory, and I am going to change its name to “archivo”, using the command ‘mv’: The ‘mv’ command is used to move the files, but you can move a file to the same location using a different name.

$ mv file new_name

Also, you can move a file to a different location and a different name. Also, you can rename directories using the same syntax. You can get verbose output using the ‘v’ option. If you rename a file using the name of an existing file, the ‘mv’ command will overwrite the existing file, but if you don’t want it, just pass the ‘i’ option, and it will prompt before overwriting file. If you don’t want to use the ‘i’ option, you should make a backup of the existing file. The backup is calledfile1~ You can add a suffix to the backup name, just type the following syntax.

$ mv -b -S “suffix” file_name existing_file_name

Options List

–backup options If you want to know more detailed information about ‘mv’ just type: $ man mv  at your terminal.  

Rename multiple files in Linux using ‘rename’

Synopsis rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E perlexpr]*|perlexpr [ files ] This command is slightly more advanced than mv because it requires the knowledge of, or at least a basic familiarity with regular expressions, “rename” renames the filenames (multiple files) supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input. Options Examples: I have two files: file1.c and file2.c, and I want to change the extension of file1 to .txt, so I’m going to use the following command:

$ rename ’s/.c/.txt/’ file1.c

I made a new file named file3.c, and I want to change the extension of files 2 and 3, so I’m going to use the following command:

$ rename ’s/.c/.txt/’ *

Now I have four files:awesomefile, greatfile, bigfile, linuxfile; and I want to change a specific part of their names, i.e. change “file” to “document”, the final result of this should be: awesomedocument, greatdocument, bigdocument and linuxdocument So I’m going to use the following command:

$ rename ’s/file/document/’ *

Also, I want to translate lowercase to uppercase, so I’m going to use the following command:

$ rename ‘y/a-z/A-Z/’ *

If you want to know more detailed information about ‘mv’ just type: $ man rename at your terminal.  

Renaming a Directory in Linux

Similar to rename a file in Linux, we can rename directory in Linux. With mv command, provide the source as the path of the directory and the new directory name as the path of the destination. Sounds confusing? Check it out – DirectoryA located inside home directory. Here is how we can rename this directory in Linux –

mv $HOME/DirectoryA $HOME/NewDirectoryName

If your desired directory is not in the current directory, then you have to provide the full path of the directory.

When Does mv command in Linux move and rename file(s)?

If one command does two functions, moving files and renaming files, you may be wondering when it moves a file and when it renames a file. The answer is straightforward. It will rename a file or directory if the source and destination paths are the same; otherwise, it will move or move and rename a file or directory.

Linux mv command to move files

Like I mentioned above, as long as source and destination are not the same, the source file or directory will be moved to the destination directory. The following command will move a file –

mv $HOME/DirectoryA/test.txt $HOME/Test/test.txt

The following mv command will move and rename the file –

mv $HOME/DirectoryA/test.txt $HOME/Test/test2.txt

Using mv command in Bash scripts

When writing a bash script, keep in mind that the user can run the script from any directory. Remember to provide the full path to the source and destination directories when using the mv command in a bash script. If you only input the name of the directory or file, assuming that the user will execute the script from that directory, the script may fail if the user executes it from a different location.

Conclusion

You can rename files in Linux using a file manager like dolphin or Nautilus but I think renaming files in Linux using the terminal is more fun, also I think that the ‘rename’ command is more powerful than the file manager. You can choose your own way, but I prefer always the terminal.