All Stage/Level Linux Administrators Can’t Live Without cp (copy) Command, is it true ?

All Stage/Level Linux Administrators Can’t Live Without cp (copy) Command, is it true ?

Yes. As per my personal experience (Almost 6.5 years) in Linux, i can assure Linux administrators can’t live without cp command because minimum 50 times i used cp command per day, even i have less work then imagine if the administrators have more work ?

cp is one of the very basic Linux/Unix command to copy files and folders/directories from one location to another location. While copying the files from source to destination, the source file name unchanged but if we needed, we can change the destination file name.

Common Syntax & Options for cp command

Syntax :
# cp [Option] [Source] [Destination]

Options :

  • -v : Verbose mode (show progress)
  • -r : Copy directories recursively
  • -n : do not overwrite an existing file
  • -d : Copy a link file
  • -i : prompt before overwrite
  • -p : preserve the specified attributes
  • –backup : make a backup of each existing destination file

1) How to copy a file from one location to another location ?

To copy a file from one location to another location, use the below command to do it. This will copy the source file to destination location with same name.

# pwd
/home/mageshm

# cp test-bzip2.txt /home/2daygeek/cp-test

Here i’m going to check the mentioned file were copied or not. See the below output.

# ls -lh /home/2daygeek/cp-test
total 12K
-rw-r--r--. 1 root     root       60 Jan 24 10:53 test-bzip2.txt

The output clearly shows, the mentioned file has been copied from source /home/mageshm directory to destination /home/2daygeek/cp-test directory.

2) How to copy a file from one location to another location with different name ?

To copy a file from one location to another location with different name, just give a desired destination name that’s and it’s won’t use any option for this operation. This will copy the source file to destination location with different name.

# cp test-bzip2.txt /home/2daygeek/cp-test/test-bzip2-new.txt

Here i’m going to check the mentioned file were copied or not. See the following output.

# ls -lh /home/2daygeek/cp-test
total 16K
-rw-r--r--. 1 root     root       60 Jan 24 11:04 test-bzip2-new.txt
-rw-r--r--. 1 root     root       60 Jan 24 10:53 test-bzip2.txt

The output clearly shows, the mentioned file has been copied from source /home/mageshm directory to destination /home/2daygeek/cp-test directory with different name.

3) How to copy multiple files from one location to another location ?

To copy multiple files from one location to another location, just mention the list of files which you want to copy followed by cp command and it’s won’t use any special option for this operation. This will copy the source files to destination location with same name.

# cp test3-bzip2.txt test2-bzip2.txt test10.zip /home/2daygeek/cp-test

Here i’m going to check the mentioned files were copied or not. See the following output.

# ls -lh /home/2daygeek/cp-test
total 12M
-rw-r--r--. 1 root     root      12M Jan 24 11:07 test10.zip
-rw-r--r--. 1 root     root       61 Jan 24 11:07 test2-bzip2.txt
-rw-r--r--. 1 root     root       61 Jan 24 11:07 test3-bzip2.txt
-rw-r--r--. 1 root     root       60 Jan 24 11:04 test-bzip2-new.txt
-rw-r--r--. 1 root     root       60 Jan 24 10:53 test-bzip2.txt

The output clearly shows, the mentioned files has been copied from source /home/mageshm directory to destination /home/2daygeek/cp-test directory.

4) How to copy a folder (all files and folders recursively) from one location to another location ?

The above three examples shows, how to copy single or multiple files from source to destination but following example bit different.

To copy a folder (all files and folders) recursively from one location to another location, just add -r option with cp command which will do the trick. This will copy the source folder to destination location with same name.

# cp -r links /home/2daygeek/cp-test

Here i’m going to check the mentioned folder were copied or not. See the below output.

# ls -lh /home/2daygeek/cp-test
total 12M
drwxr-xr-x. 3 root     root     4.0K Jan 24 11:12 links/
-rw-r--r--. 1 root     root      12M Jan 24 11:07 test10.zip
-rw-r--r--. 1 root     root       61 Jan 24 11:07 test2-bzip2.txt
-rw-r--r--. 1 root     root       61 Jan 24 11:07 test3-bzip2.txt
-rw-r--r--. 1 root     root       60 Jan 24 11:04 test-bzip2-new.txt
-rw-r--r--. 1 root     root       60 Jan 24 10:53 test-bzip2.txt

The output clearly shows, the mentioned folder links has been copied from source /home/mageshm directory to destination /home/2daygeek/cp-test directory recursively.

5) How to copy multiple folders (all files and folders recursively) from one location to another location ?

To copy multiple folders (included all files and folders) recursively from one location to another location, use the same above format and add list of folders which you want to copy. This will copy the source folders to destination location with same name.

# cp -r public_ftp/ public_html/ /home/2daygeek/cp-test

Here i’m going to check the mentioned folders were copied or not. See the following output.

# ls -lh /home/2daygeek/cp-test
total 12M
drwxr-xr-x. 3 root     root     4.0K Jan 24 11:12 links/
drwxr-xr-x. 2 root     root     4.0K Jan 24 11:19 public_ftp/
drwxr-xr-x. 3 root     root     4.0K Jan 24 11:19 public_html/
-rw-r--r--. 1 root     root      12M Jan 24 11:07 test10.zip
-rw-r--r--. 1 root     root       61 Jan 24 11:07 test2-bzip2.txt
-rw-r--r--. 1 root     root       61 Jan 24 11:07 test3-bzip2.txt
-rw-r--r--. 1 root     root       60 Jan 24 11:04 test-bzip2-new.txt
-rw-r--r--. 1 root     root       60 Jan 24 10:53 test-bzip2.txt

The output clearly shows, the mentioned folders public_html & public_ftp has been copied from source /home/mageshm directory to destination /home/2daygeek/cp-test directory recursively.

6) How to copy group (Pattern) of files from one location to another location ?

To copy group of files (I means same extension files like .txt, .jpeg, .png, .php, etc,.) from one location to another location, just use wildcard * with file extension which you want to copy and it’s won’t use any special option for this operation. This will copy the group of pattern files like .txt files to destination location with same name.

# cp *.txt /home/2daygeek/cp-test

Here i’m going to check the mentioned group files were copied or not. See the following output. You might get doubt while seeing the below output because the output is same like above. Have a look on timestamp for clarification, all the files ware copied newly (Before performing this operation, i have deleted all the text files where copied already).

# ls -lh /home/2daygeek/cp-test
total 12M
drwxr-xr-x. 3 root     root     4.0K Jan 24 11:12 links/
drwxr-xr-x. 2 root     root     4.0K Jan 24 11:19 public_ftp/
drwxr-xr-x. 3 root     root     4.0K Jan 24 11:19 public_html/
-rw-r--r--. 1 root     root      12M Jan 24 11:07 test10.zip
-rw-r--r--. 1 root     root       61 Jan 24 11:20 test2-bzip2.txt
-rw-r--r--. 1 root     root       61 Jan 24 11:20 test3-bzip2.txt
-rw-r--r--. 1 root     root       60 Jan 24 11:20 test-bzip2.txt

The output clearly shows, the mentioned group of files .txt extension has been copied from source /home/mageshm directory to destination /home/2daygeek/cp-test directory.

7) How to copy all files from one location to another location using wild-cards ?

To copy all files (except directory) from one location to another location, just use wildcard * option followed by cp command. This will copy all the files from source to destination location with same name and omitting directories.

# cp * /home/2daygeek/cp-test
cp: omitting directory `links'
cp: omitting directory `perl5'
cp: omitting directory `public_ftp'
cp: omitting directory `public_html'
cp: cannot stat `s-link-file': No such file or directory
cp: cannot stat `s-link-folder': No such file or directory

Here i’m going to check the mentioned files were copied or not. See the below output.

# ls -lh /home/2daygeek/cp-test
total 212M
-rw-r--r--. 1 root     root     100M Jan 24 11:26 test100.dat
-rw-r--r--. 1 root     root     101M Jan 24 11:26 test100.zip
-rw-r--r--. 1 root     root      12M Jan 24 11:26 test10.zip
-rw-r--r--. 1 root     root       61 Jan 24 11:26 test2-bzip2.txt
-rw-r--r--. 1 root     root       61 Jan 24 11:26 test3-bzip2.txt
-rw-r--r--. 1 root     root       60 Jan 24 11:26 test-bzip2.txt

The output clearly shows, the above operation successfully copied all the files from source /home/mageshm directory to destination /home/2daygeek/cp-test directory and omitted directories while copy.

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...

Shares
Close
Please support the site
By clicking any of these buttons you help our site to get better