Moving and copying files

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 files

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

-n

Don't overwrite an existing file

-i

Prompt before overwriting an existing file

-f

Don't prompt while overwriting an existing file

-u

Move a source file only when the source is newer than the destination or when the destination is missing

-v

Print name of the files being moved

Moving a directory to a new location

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.

Note

Use the mv command with the -i option to avoid an accidental overwrite of a file.

Renaming a file

We can also use the mv command to rename a filename. For example, we have the test_dir1 directory in the /tmp directory. Now, we want to rename it as test_dir. We can execute the following command:

$ mv  /tmp/test_dir1/  /tmp/test_dir  # directory got renamed to test_dir

Copying files

Creating copies of files is a very common operation that can be performed locally or to a remote system.

Copying files locally

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

-f

Don't prompt while overwriting an existing file

-i

Prompt before overwriting an existing file

-R

Copy directories recursively

-u

Copy a source file only when the source is newer than the destination or when the destination is missing

-p

Preserve attributes of a copied file with the original file

-v

Verbose output of which file is being copied

Copying a file to another location

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.

Copying files remotely

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

-C

Enable compression while transferring data over a network

-l limit

Limit the used bandwidth specified in Kbit/s

-p

Preserve attributes of a copied file with the original file

-q

Don't print any progress output on stdout

-r

Copy directory recursively

-v

Verbose output while the copy is in progress

Copying files to a remote server

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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.181.14