How to Rename a File in Linux

By | 2019-09-10

To rename a file in Linux, you use the mv command. The mv command can also rename directories and move files into different directories.

When using mv, always specify the current filename first, followed by one or more spaces, and last the new name.

For example, to rename a file named my_file.pdf to your_file.pdf, use the following command:

$ mv my_file.pdf your_file.pdf

Keep in mind that if there is already a file named your_file.pdf, it will be overwritten. To prevent this, use the -ioption. The -i option causes mv to prompt you before overwriting the file or aborting the operation. Answer the prompt with y if you wish to overwrite the file or n if you do not.

$ mv -i my_file.pdf your_file.pdf 
mv: overwrite 'your_file.pdf'? n
$ mv -i my_file.pdf your_file.pdf 
mv: overwrite 'your_file.pdf'? y

How to Handle Spaces and Other Special Characters

If a file name has a space, asterisk, or pretty much any non alphanumeric character, the command interpreter will likely interpret it instead of treat it as part of a file name. To get around this, surround the file name with or . There are differences with how the interpreter handles single and double quotes, but in most cases using either would be fine.

Suppose you need to rename the file bad * name to better_name. Here are two examples:

$ mv 'bad * name' better_name
$ mv -i "bad * name" better_name
mv: overwrite 'better_name'? y

References