Skip to main content

How To Move and Rename Files in Linux

Move and Rename Files in Linux
(Image credit: Tom's Hardware)

Moving and renaming files in the Linux terminal is quick and painless, but first you need to understand how to do it. Whether you are sat at the machine, or remotely connected via SSH, these are the commands that will move files around your machine.

In this how-to we’ll learn the basics of mv, a command that moves, and renames files. We will also learn a few advanced arguments for this command, arguments which will enhance its use and make our lives easier.

These commands will work on most Linux machines. Our test PC ran Kubuntu 21.10 but you can also run through this how-to on a Raspberry Pi. All of the how-to is performed via the Terminal. 

Rename a single file

(Image credit: Tom's Hardware)

Let's start simple with just a single file which we shall rename. The command syntax is structured as follows.

mv <original name> <new name>

1. Create a new file called file1.txt.

$ touch file1.txt

2. Rename file1.txt, to newfile1.txt. The mv command can be used on files in the same directory, or with files in other locations.

$ mv file1.txt newfile1.txt

3. List the contents of the directory to see the new file. You should now see newfile1.txt.

$ ls

Moving a file 

(Image credit: Tom's Hardware)

The mv command’s primary use is to move files and the syntax is identical to renaming. We have a source and a target. 

Lets move a file to a subdirectory.

1. Create a new directory, MoveFilesHere.

$ mkdir MoveFilesHere

2. Create a new blank file using the touch command. Touch can be used to make any type of file, but until it has the correct data inside of it, it is really just an empty shell.

$ touch blankfile.txt

3. Use the mv command to move the file to the subdirectory MoveFilesHere. We specify the source, blankfile.txt and then the target which can be a directory (as is the case here) or we can move the file into a new directory with a new name by passing the target location and the intended filename.

$ mv blankfile.txt MoveFilesHere/

This assumes that the MoveFilesHere directory is directly below the directory that blankfile.txt is in. If you are moving your file to a directory that’s on a different level you may need to specify the full path (ex: /home/pi/scripts). You can also move a file up to its parent directory by using ../ as the destination folder.

Use the ls command to check that the file has been moved successfully. Here we pass ls (list) command the extra parameter which is the directory to look inside of.

$ ls MoveFilesHere/

If we wanted to move a file and change its name we would pass the target and supply the intended filename. So altering the above example to move blankfile.txt into MoveFilesHere and rename it as namechanged.txt we would use the following command.

$ mv blankfile.txt MoveFilesHere/namechanged.txt

How to Rename a Batch of Files in Linux 

(Image credit: Tom's Hardware)

There will come a time where we need to rename many files at the same time. Batch renaming is handled using the mv command, but we use a one line Bash script to iterate over the files that we wish to rename.

1. Create a batch of files to experiment with. This command will generate 26 files, a to z.txt.

$ touch {a..z}.txt

2. Rename all of the files from .txt to .log. We use a for loop that creates a variable (f) and iterates over all of the txt files (*.txt), replacing the value of the variable ($f is how we call the variable) with the filename which is then renamed from .txt to .log (using the (f%.txt} as a pattern matching operator for the filename stored in the f variable). At the end of the command we use “done” to signify the end of the for loop. The “--” part of the code denotes where the first part of the for loop, selecting the file to rename, ends, and where the second part, renaming the file, starts.

$ for f in *.txt; do mv -- "$f" "${f%.txt}.log"; done

Exploring the Linux MV Command

The mv command has a number of useful arguments (parameters) that we can pass when using the command. Here are a few examples.

(Image credit: Tom's Hardware)

If you need an interactive prompt when moving files, say there may be an identical file in the new directory, the -i argument will prompt for a decision when a conflict is found.

$ mv -i blankfile.txt MoveFilesHere

The opposite of an interactive prompt is where we force the command to overwrite files. Obviously check the target location before invoking this command.

$ mv -f blankfile.txt MoveFilesHere

What if we want to move our files, but not overwrite any existing files? For that we need –no-clobber (-n). This command will happily copy files over, but will skip any existing files in the target directory.

$ mv -n blankfile.txt MoveFilesHere

Need to only move files that have been updated? The -u argument will check that the source file (the one that we wish to copy) is newer than the target file. Useful for moving log files and incremental backups of documents.

$ mv -u blankfile.txt MoveFilesHere

(Image credit: Tom's Hardware)

Finally, if we want to see what files are being moved then we can use the -v (verbose) argument to display the names of the files as they copy across. Useful for quick debugs, and for looking busy in the data center.

$ mv -v blankfile.txt MoveFilesHere

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".