Friday, April 12, 2019

UNIX cp command with examples

The ‘cp’ command in used is used to copy files or directories from one place to another.

Make a copy of a file

cp source destination

sam@asus:~/unix% cp sample1.txt sample1-copy.txt
sam@asus:~/unix% ls -l sample1*
-rw-rw-r-- 1 sam sam 101 Apr  5 20:30 sample1-copy.txt
-rw-rw-r-- 1 sam sam 101 Apr  4 21:31 sample1.txt
sam@asus:~/unix% 

The ‘cp’ command gives you the ability to copy multiple files at once. You can either specify the names of each file on the command line or you can use the shell expansion character ‘*’ to get files which meet a certain criteria.

cp file1 file2 file3… destination
sam@asus:~/unix% cp sample1.txt sample2.txt sample3.txt backup/
sam@asus:~/unix% ls -l backup/
total 12
-rw-rw-r-- 1 sam sam 101 Apr  5 20:40 sample1.txt
-rw-rw-r-- 1 sam sam  64 Apr  5 20:40 sample2.txt
-rw-rw-r-- 1 sam sam  97 Apr  5 20:40 sample3.txt
sam@asus:~/unix% 

Or using the wildcard character ‘*’ to copy all text files in to the ‘backup’ directory.

sam@asus:~/unix% cp *.txt backup/
sam@asus:~/unix% ls -l backup/
total 40
-rw-rw-r-- 1 sam sam 10240 Apr  5 20:27 file1.txt
-rw-rw-r-- 1 sam sam     0 Apr  5 20:27 file2.txt
-rw-rw-r-- 1 sam sam     0 Apr  5 20:27 file3.txt
-rw-rw-r-- 1 sam sam 10240 Apr  5 20:27 output.txt
-rw-rw-r-- 1 sam sam   101 Apr  5 20:27 sample1.txt
-rw-rw-r-- 1 sam sam    64 Apr  5 20:27 sample2.txt
-rw-rw-r-- 1 sam sam    97 Apr  5 20:27 sample3.txt
-rw-rw-r-- 1 sam sam    79 Apr  5 20:27 sample4.txt
sam@asus:~/unix% 

Make a copy of a directory

To make a copy of a directory and all its sub-directories you would use the ‘-R’. The ‘-R’ option tells cp to copy all files recursively.

cp -R dir1/ dir2/ dir3/… dest/
sam@asus:~/unix% cp -R test/ backup/
sam@asus:~/unix% ls backup/
sample1.txt  sample2.txt  sample3.txt  test
sam@asus:~/unix% 

No comments:

Post a Comment

Exploiting Weak WEBDAV Configurations

The server we are going to audit has the following fingerprint. 80/tcp open http Apache httpd 2.2.8 ((Ubuntu) DAV/2) Next we need t...