Move and Copy Files in Linux at the Command Line

The following article will explain how to copy and move files between different directories within the Linux command line interface.  These commands are very useful especially if you are doing routine system administration on a Linux server.  Each command will be broken down into the two segments of moving files and then copying files. 

Moving Files:

The command to use for moving files and directories on a Linux server is "mv".  This command is useful when you know you want your file to leave it's starting directory and be placed within another directory on the filesystem. Here is how to use the MV command.

1) Login to the Linux server via an SSH session.

mv1

2) Once you are at the command line you initiate the move command by typing in mv and then the starting file/directory location following by the destination file/directory location.  The formatting of the command looks like this:

mv2

It it important to note that there is a single space in between the starting location and the destination. 

If you are just moving a single file and you are already in the starting directory then you can just type in the filename followed by the destination directory and filename.  It is recommend to use the full directory/filename structure as good practice to ensure the move command is executed correctly. 

The "mv" command can also be used for renaming files and directories as well.  An example of this is if you want to rename a subdirectory to something else but still leave it in the same location within the parent directory structure.

mv3

The other flag that you can use when moving a full directory is doing it recursively with the -R flag.  This will move the directory and everything in it to the destination which you want to move it to.

Copying Files:

The command to use for copying files and directories on a Linux server is "cp".  This command is useful when you know you want to copy your file from it's starting directory and place a copy of it in a different location.  Using the "cp" command will leave the original file or directory in it's existing location.

Here is how to use the 'cp" command.

1)You can initiate the copy command by typing in mv and then the starting file/directory location and then the destination file/directory location.  The formatting of the command looks like this:

cp1

As mentioned above please note the single space in between the starting location and the destination.  

If you are just copying a single file and you are already in the starting directory then you can just type in the filename followed by the destination directory and filename.  It is recommend to use the full directory/filename structure as good practice to ensure the copy command is executed correctly.

You can also rename directories and files you are copying with the "cp" command by typing in the new directory/filename in your destination.

cp2

Also as mentioned above the flag that you can use when copying a full directory is doing it recursively with the -R flag.  This will copy the directory and everything in it to the destination which you want to move it to.