We often need to copy or move files from one location to another in order to arrange files according to the need. We also can copy our computer data to an external drive or another computer available locally or remotely in order to keep the backup of the important data.
Moving regular files and directories is useful when we want to keep exactly one copy of the data at a new location. The mv
command is used to move files from one location to another.
The syntax of using the mv
command is as follows:
mv [option] source... destination
Here, source
is the file or directory to be moved. Multiple source files can be specified and destination
is the location in which the files and directories should be moved.
Some of the important options of the mv
command are explained in following table:
Option |
Description |
---|---|
|
Don't overwrite an existing file |
|
Prompt before overwriting an existing file |
|
Don't prompt while overwriting an existing file |
|
Move a source file only when the source is newer than the destination or when the destination is missing |
|
Print name of the files being moved |
To move a directory from one location to another, execute the following command:
$ mkdir ~/test_dir1 # Directory test_dir1 created in home directory $ mv ~/test_dir1/ /tmp # moving directory to /tmp
The test_dir1
directory has been moved to /tmp
and no copy of test_dir1
exists in the home directory now.
Now, we will create a directory called test_dir1
again in the user's home directory:
$ mkdir ~/test_dir1 # Directory test_dir1 created in home directory
Try again to move test_dir1
in /tmp
with the –i
option:
$ mv -i ~/test_dir1/ /tmp mv: overwrite '/tmp/test_dir1'?
We can see that the -i
option asks a user explicitly whether we want to overwrite an existing directory with a new directory or not.
Creating copies of files is a very common operation that can be performed locally or to a remote system.
To copy the files on a local machine, the cp
command is used.
The syntax of using the cp
command is as follows:
cp [option] source … destination
Here, source
can be a single file, multiple file, or a directory, while destination
can be a file if source
is a single file. Otherwise, destination
will be a directory.
Some of important options to the cp
command are as follows:
Options |
Description |
---|---|
|
Don't prompt while overwriting an existing file |
|
Prompt before overwriting an existing file |
|
Copy directories recursively |
|
Copy a source file only when the source is newer than the destination or when the destination is missing |
|
Preserve attributes of a copied file with the original file |
|
Verbose output of which file is being copied |
To copy a file to another location, execute the following command:
$ touch ~/copy_file.txt # Creating a file $ cp ~/copy_file.txt /tmp/ # Copying file to /tmp
Now, we have two copies of the copy_file.txt
file that are at the user's home directory and the /tmp
directory.
To copy a directory, we use cp
with the -R
option:
$ mkdir ~/test_dir2 # Creating a test diretcory $ cp -R ~/test_dir2 /tmp/
The test_dir2
directory gets copied to /tmp
along with all the contents available in the directory.
To copy files on a remote machine, the scp
command is used. It copies files between hosts on a network. The scp
command uses ssh
to authenticate the target host and transfer data.
The simple syntax of scp
is as follows:
scp [option] user1@host1:source user2@host2:destination
Here, in user1@host1:source
, user1
is the username of the source from where a file will be copied and host1
is the hostname or IP address; source
can be a file or a directory to be copied.
In user2@host2:destination
, user2
is the username of the target host where files should be copied and host2
is the hostname or IP address; destination
can be a file or directory where it gets copied. If no destination is specified, a copy will be made in the target host's home directory.
If no remote source and destination to provided, a copy will be made locally.
A few important options of scp
are discussed in the following table:
Option |
Description |
---|---|
|
Enable compression while transferring data over a network |
|
Limit the used bandwidth specified in Kbit/s |
|
Preserve attributes of a copied file with the original file |
|
Don't print any progress output on |
|
Copy directory recursively |
|
Verbose output while the copy is in progress |
To copy files to a remote server, it is very important that the ssh
server is already running on the server. If it is not, make sure to start the ssh
server. To copy files, use the scp
command as follows:
$ scp -r ~/test_dir2/ foo@localhost:/tmp/test_dir2/
Here, we have made a copy to a local machine. So, the hostname used is localhost
. Now, we have another directory test_dir2
inside /tmp/test_dir2/
:
$ ls -l /tmp/test_dir2 total 0 drwxrwxr-x. 2 foo foo 40 Aug 25 00:44 test_dir2
3.144.232.137