Serverspace Black Friday
JT
May 16, 2024
Updated May 27, 2024

How to move files & directories in Linux?

Linux Maintenance

There are many different utilities in Linux, but some of them are used more often than others. For example, mv is a utility that allows you to change the name and location of a file/s and even move directories. Everything is just like in a graphical environment, but only through the command line.

How to move files?

To work with the utility, consider its syntax below; parentheses and <> signs are not used, but only show argument boundaries:

mv (-options) <source> <destination>
Help
Screenshot №1 — Help

Let's imagine that logs have been accumulated on the device and it is necessary to move them to the /tmp/backup folder. For one file we will use the command:

mv ./log.txt /tmp/backup
Move file
Screenshot №2 — Move file

As an analogue to this command, you can use the file descriptor command:

cat log.txt > /tmp/backup/log.txt && rm log.txt
ALternate way to move file
Screenshot №3 — Alternate way to move file

The alternative way more clearly reflects the logic of the process: the cat utility outputs the contents of a file, the > sign is a file descriptor that redirects the output to the target specified file. And the command rm log.txt removes files from the current directory. Which implements the move process. However, this method will not work for recursive moving of files or in other words all files from a folder. For this purpose in the mv command instead of the name of a specific file we will specify the mask *:

mv /tmp/backup/* /tmp/backup2
Move multiple files
Screenshot №4 — Move multiple files

Expected result: all files from one directory will be moved to the target directory.

How to move directories?

The process is very similar to moving files, except now you need to specify a folder. This process is realised by the following command:

mv /tmp/backup_antivirus /tmp/backup_all

As a result, the backup_antivirus folder will be moved to /tmp/backup_all and the absolute or full path will look like this: /tmp/backup_all/backup_antivirus!

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.