Skip to main content

Linux fundamentals: How to copy, move, and rename files and directories

Learn how to use the mv and cp commands to manage your Linux files and directories.
Image
Two windows with a small pane open

Image by Martin Pyško from Pixabay

Copying, moving, and renaming files and directories are standard tasks for sysadmins and end users. Depending on your Linux distribution, you can accomplish these operations in various ways.

The Bash shell is usually the most efficient tool for file management. This article assumes you already have a basic understanding of how to open a Linux terminal and enter commands. (See How to access the Linux terminal if you want a refresher.) Connect to your Linux terminal with your regular user account, and get ready to reorganize.

Change to your home directory and create a new directory named mydir for the exercises. The command to create a new directory is mkdir:

$ mkdir mydir
$ cd mydir/

Move files and directories

The mv command moves both directories and files. Check its options and parameters from the --help results below:

$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
  -f, --force                  do not prompt before overwriting
  -i, --interactive            prompt before overwrite
  -n, --no-clobber             do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -Z, --context                set SELinux security context of destination
                                 file to default type
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

Rename files and directories

You also use the mv command to rename directories and files if the destination doesn't already exist. If the destination exists, then they're moved using the syntax mv {source} {destination}. Here is an example of moving existing files to existing directories:

$ ls -l
total 0
drwxrwxr-x. 2 localuser localuser  6 Jun  9 14:57 dir1
drwxrwxr-x. 2 localuser localuser  6 Jun  9 17:52 dir2
drwxrwxr-x. 2 localuser localuser  6 Jun  9 17:52 dir3
drwxrwxr-x. 3 localuser localuser 21 Jun  9 16:57 dir4
-rw-rw-r--. 1 localuser localuser  0 Jun  9 17:31 file1
-rw-rw-r--. 1 localuser localuser  0 Jun  9 17:33 file2
-rw-rw-r--. 1 localuser localuser  0 Jun  9 17:33 file3

$ mv file1 dir1/

$ mv file2 dir2/

$ mv file3 dir3/

$ ls -l
total 0
drwxrwxr-x. 2 localuser localuser 19 Jun  9 17:53 dir1
drwxrwxr-x. 2 localuser localuser 19 Jun  9 17:53 dir2
drwxrwxr-x. 2 localuser localuser 19 Jun  9 17:53 dir3
drwxrwxr-x. 3 localuser localuser 21 Jun  9 16:57 dir4
[mydir]$ ls -lR
.:
total 0
drwxrwxr-x. 2 localuser localuser 19 Jun  9 17:53 dir1
drwxrwxr-x. 2 localuser localuser 19 Jun  9 17:53 dir2
drwxrwxr-x. 2 localuser localuser 19 Jun  9 17:53 dir3
drwxrwxr-x. 3 localuser localuser 21 Jun  9 16:57 dir4

./dir1:
total 0
-rw-rw-r--. 1 localuser localuser 0 Jun  9 17:31 file1

./dir2:
total 0
-rw-rw-r--. 1 localuser localuser 0 Jun  9 17:33 file2

./dir3:
total 0
-rw-rw-r--. 1 localuser localuser 0 Jun  9 17:33 file3

./dir4:
total 0
drwxrwxr-x. 2 localuser localuser 19 Jun  9 17:35 subdir1

./dir4/subdir1:
total 0
-rw-rw-r--. 1 localuser localuser 0 Jun  9 17:35 file4

[ Boost your Bash skills. Download the Bash shell scripting cheat sheet. ]

And you can use the mv command to move directories into other directories:

$ ls -1
dir1
dir2
dir3
dir4

$ mv dir1/ dir2/

$ mv dir2/ dir3/

$ ls -1
dir3
dir4

The dir1 and dir2 directories still exist; you've just moved them. See what it looks like for yourself:

$ ls -R
dir3
dir4

./dir3:
dir2
file3

./dir3/dir2:
dir1
file2

./dir3/dir2/dir1:
file1

./dir4:
subdir1

./dir4/subdir1:
file4

Copy files and directories

The cp command copies both files and directories. This command has many options, but the basic syntax is simple. Run cp {source} {destination} to copy from one place (source) to another (destination). Consider the following example:

$ ls -1
dir3
dir4

$ cp dir4/subdir1/file4 .

$ ls -1
dir3
dir4
file4

$ ls -R dir4/
dir4/:
subdir1

dir4/subdir1:
file4

[ Download the free Linux commands cheat sheet. ]

To copy an entire directory with its contents, use the -R option, as seen below:

$ cp -R dir3/ dir4/

$ ls -R
dir3
dir4
file4

./dir3:
total 0
dir2
file3

./dir3/dir2:
dir1
file2

./dir3/dir2/dir1:
file1

./dir4:
dir3
subdir1

./dir4/dir3:
dir2
file3

./dir4/dir3/dir2:
file2

./dir4/dir3/dir2/dir1:
file1

./dir4/subdir1:
file4

When you copy empty directories into other directories, there's no need for the -R parameter.

More to explore

For each command I've demonstrated, there are many more options I've left out for the sake of brevity.

As a sysadmin, you must know how to copy, move, and rename files and directories. These file-management commands are the basis of much of what you do on the system and are the building blocks for effective Linux administration. I hope this article aids you in understanding this topic, helps your Linux certification path, and adds to your general sysadmin knowledge.

Check out these related articles on Enable Sysadmin

Image
Typing on a laptop with headphones next to the computer
Red Hat Enterprise Linux 9 offers significant new features for hybrid cloud organizations. Here's how to install it so you can start testing.
Author’s photo

Alexon Oliveira

Alexon has been working as a Senior Technical Account Manager at Red Hat since 2018, working in the Customer Success organization focusing on Infrastructure and Management, Integration and Automation, Cloud Computing, and Storage Solutions. More about me

Red Hat Summit 2022: On Demand

Get the latest on Ansible, Red Hat Enterprise Linux, OpenShift, and more from our virtual event on demand.

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX