How to Move Files and Directories in Linux using Terminal?


This post may contain affiliate links/ads and I may earn a small commission when you click on the links/ads at no additional cost to you. As an Amazon Affiliate, I earn from qualifying purchases. Techsphinx also participates in the StationX Affiliate program. You can read my full disclaimer here.

In this article, I will show you how you can move files and directories in Linux using the terminal.

You can easily move files and directories around in Linux using GUI. However, the command-line method has more options and can help you to achieve complex moving tasks easily and quickly.

There are many command-line utilities for moving files, but the most used is the mv command (which is pre-installed on all Linux systems).

How to Use the mv Command?

The mv command (short for “move”) can be used for both moving and renaming files and directories in Linux.

I have written complete guides on renaming files and directories, you can check them out:

How to Rename Files in Linux using Terminal?

How to Rename Directories in Linux using Terminal?

Before proceeding, let’s have a look at the basic syntax of the mv command.

Basic Syntax

mv <OPTIONS> <SOURCE> <DESTINATION>

mv: name of the command.

<OPTIONS>: Additional options you can use with the mv command.

<SOURCE>: Name of the source file/files. The source can be single or multiple files or even directories (folders).

<DESTINATION>: The destination can be a single file or directory. If you specify multiple files as source, then the destination must be a directory so that all the source files are moved into the destination directory.

Things to Remember

Here are few things to keep in mind when using the mv command:

  • You can mention multiple source files or directories, but the destination can be a single file or directory.
  • If you’ve mentioned multiple files or directories as the source, then the destination must be an existing directory. All the source files and directories will be moved into the destination directory.
  • If you specify a single file as the source and a single file as the destination at the same location, then you are renaming the file.
  • When you specify a single directory as the source and the destination directory doesn’t exist, then the source directory will be renamed to the destination. If the destination exists, then the source will be moved into the destination.
  • To move a file or directory in Linux, you’ll need the write permissions on both source and destination, else you’ll get a permission denied error.

Moving Files Using mv command

Let’s use the mv command to move files in Linux.

Moving a single file using the mv command

To move a single file, run the mv command followed by the source file and the destination directory.

mv file.jpg dest

The above command will move the “file.jpg” to the “dest” directory present in the same location.

You can provide the full path when using the mv command if the source and destination are not present in the current location.

mv /home/user/Desktop/myfolder/file.jpg  /home/user/Pictures/

This will move the “file.jpg” to the “Pictures” directory.

Moving multiple files using the mv command

If you want to move multiple files, just mention all the files separated by a space.

mv file1 file2 dest

When you run the above command, “file1” and “file2” will be moved to the “dest” directory.

You can also use wildcard and regular expressions in the mv command. Here’s an example of moving all files with “.jpg” extensions (in the current location) to the “Pictures” directory.

mv *.jpg ~/Pictures

Moving Directories using mv command

Let’s see how to move directories using the mv command.

Moving a single Directory using the mv command

To move a single directory, use the mv command followed by the source and destination directory separated by a space. The destination directory should exist beforehand.

mv dir1 dir2

The above command will move “dir1” into the “dir2” directory.

Moving Multiple directories using the mv command

If you want to move multiple directories, mention all the directories separated by a space. The last-mentioned directory will be considered as the destination directory.

mv dir1 dir2 dir3

When you execute the above command, “dir1” and “dir2” will be moved into “dir3”.

Just like moving files, you can also use wildcards to move multiple directories at once.

mv *_bak dest

The above command will move all directories (and files too) that end with “_bak”.

If you want the mv command to move only the directories, then you can use the find command to filter out all the directories then use the mv command to move the directories.

find . -maxdepth 1 -type d -iname '*_bak' -exec mv '{}' dest \;

find: Name of the find command.

.: Dot (.) means the current working directory.

-maxdepth 1: Means don’t go deeper than the current directory.

-type d: Only find directories.

-iname: Provide a query that adheres to a specific pattern. (In this case, all directories ending “_bak”)

-exec: execute a command with the result of the find command.

mv: move command

‘{}’: the result of the find command will be inserted here. In this case, all directories ending with “_bak”.

dest: name of the destination directory.

\;: Closing of -exec argument.

Additional Options for mv command

Here are some additional options you can use with the mv command for better control over moving files and directories in Linux.

Prompt before overwriting

By default, the mv command overwrites the destination file if it already exists with the same name as the source file.

If you don’t want to overwrite, then you can use the -i (interactive) option to ask before overwriting any file/files.

mv -i file.jpg dest

Output:

rahul@techsphinx:~/Desktop/myfolder$ mv -i file.jpg dest/
mv: overwrite 'dest/file.jpg'?

If you are ok with overwriting then, type “y” and hit enter.

Force overwriting

If you are attempting to overwrite a read-only file using the mv command, then it will prompt you for confirmation.

You can avoid being prompted using the -f (force) option.

mv -f file.jpg dest

Using this option, you can overwrite multiple read-only files without confirming for every file. However, be careful when using this option, if you don’t want to overwrite something important accidentally.

Overwriting Based on Modification Time

If you want to overwrite the file only if the source file is newer than the destination file, then you can use the -u (update) option with the mv command.

mv -u file.jpg dest

Note: The above command will only overwrite destination file/s based on the modification time and not on the difference of content.

So, be cautious when using the update option. A simple touch command can easily change the modification time without altering the contents of the file.

Do not overwrite existing files

You can disallow overwriting of files using the -n (no-clobber) option.

mv -n file.jpg dest

If the “file.jpg” exists in the “dest” directory, then the above command will do nothing, else it will move the “file.jpg” into the “dest” directory.

Backing up files

You can use the -b (backup) option to create a backup file if the destination file already exists.

mv -b file.jpg dest

The backup file will have the same name as the original file with a tilde (~) appended to it.

You can also create numbered backups

mv --backup=numbered file.jpg dest

The above command will create numbered backups. This is useful if you want to keep different versions of the same file.

Move files in Linux using numbered backups
Numbered Backup of files using mv command
Verbose output

By default, mv command doesn’t show what’s being done in the background. You can view the output using the -v (verbose) option.

mv -v techsphinx.jpg file.jpg lighter.jpg dest

output:

rahul@techsphinx:~/Desktop/myfolder$ mv -v techsphinx.jpg file.jpg lighter.jpg dest
renamed 'techsphinx.jpg' -> 'dest/techsphinx.jpg'
renamed 'file.jpg' -> 'dest/file.jpg'
renamed 'lighter.jpg' -> 'dest/lighter.jpg'

There are many other options you can use with the mv command; you can have a look at the mv command manual for the same.

man mv

Conclusion

Using the mv command you can easily move files and directories in Linux. I have also shown you some additional options you can use with the mv command.

I hope you learned something new today, if you have any suggestions or feedback, feel free to leave a comment.

If you like this post, then follow Techsphinx on Facebook and Twitter for more reviews, tricks, tips and tutorials.

This article needs update or correction? Report the issue here so I can update it.


Like it? Share with your friends!

Rahul R Nair

Rahul is obsessed with technology and electronic devices. He is also the founder of TechSphinx. Being a technophile, he is always busy doing some techy stuff or learning about the latest technologies. When not busy with his usual routine (staring at the computer screen) he likes to write and share his knowledge with the world.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x