How to Move and Rename Files in Linux

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

In this tutorial, we will learn the basics of mv, a command that moves and renames files. We will also learn some advanced arguments for this command, arguments that will improve its use and make our lives easier.

These commands will work on most Linux machines. Our test PC was running Kubuntu 21.10, but you can also walk through this tutorial on a Raspberry pie. All procedures are performed through the terminal.

Rename a single file

(Image credit: Tom’s Hardware)

Let’s just start with a single file that we’re going to rename. The command syntax is structured as follows.

mv  

1. Create a new file named 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

Move a file

(Image credit: Tom’s Hardware)

The main use of the mv command is to move files and the syntax is the same as renaming. We have a source and a target.

Let’s 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 create any type of file, but until it has the correct data inside, it’s really just an empty shell.

$ touch blankfile.txt

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

$ mv blankfile.txt MoveFilesHere/

This assumes that the MoveFilesHere directory is directly below the directory where blankfile.txt is located. If you move your file to a directory that is at a different level, you may need to specify the full path (ex: /home/pi/scripts) . You can also move a file to its parent directory using ../ as the destination folder.

Use the ls command to verify that the file was moved successfully. Here we pass the ls(list) command to the additional parameter which is the directory to look in.

$ ls MoveFilesHere/

If we wanted to move a file and change its name, we would pass the target and provide the expected filename. So modifying the example above to move blankfile.txt into MoveFilesHere and rename it to 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 when we have to rename several files at the same time. Batch renaming is handled using the mv command, but we use a one-line Bash script to loop through the files we want to rename.

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

$ touch {a..z}.txt

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

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

Explore Linux MV Command

The mv command has a number of useful arguments (parameters) that we can pass when using the command. Here are some 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 existing files? For this we need –no-clobber (-n). This command will happily copy the files, but ignore any existing files in the target directory.

$ mv -n blankfile.txt MoveFilesHere

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

$ mv -u blankfile.txt MoveFilesHere

(Image credit: Tom’s Hardware)

Finally, if we want to see which files are being moved, we can use the -v (verbose) argument to show the filenames as they are being copied. Useful for quick debugs and looking busy in the data center.

$ mv -v blankfile.txt MoveFilesHere

Comments are closed.