Copy Command (cp)
Copy Operation: copy mean creating duplicate or clone of data, file or directory, cut source file will remain
as it is on its location.
Consider above diagram, here source file is a.txt and its source path is /home. By copy operation we simply
create a clone of that file in buffer memory, later get pasted at given destination path or Directory.
Here it is /home/Anil.
Note: In copy operation source file will remain as it is on its location.
1) To Copy from File1 to File2 (File to File)
Syntax: cp [source] [destination]
$ cp file1 file2
Total content of file1 will be copied to file2.
If file2 is not already available, then this command will create that file.
If file2 is already available and contains some data, then this data will be over written with file1
content.
2) To Copy File’s to Directory:
$ cp file1 file2 Anil
file1 and file2 will be copied to Anil directory.
Here we can specify any number of files, but last argument should be directory.
Anil directory should be available already.
3) To Copy all Files of One Directory to another Directory:
$ cp dir1/* dir2
All files of dir1 will be copied to dir2
But dir2 should be available already.
4) To Copy Total Directory to another Directory:
$ cp dir1 dir2
cp: -r not specified; omitting directory 'dir1'
Whenever we are copying one directory to another directory, compulsory we should use -r
option.
$ cp -r dir1 dir2
total dir1 will be copied to dir2
Note: If the destination directory (dir2) already available then total dir1 will be copied to dir2. If the
destination directory (dir2) not already available, then destination directory will be created and all
files of source directory will be copied to destination directory but source directory won't be copied.
5) To Copy Multiple Directories into a Directories:
$ cp -r dir1 dir2 dir3 dir4 dir5
dir1,dir2,dir3 and dir4 will be copied to dir5
Moving and Renaming Directories
Move Operation: In move operation we simply transfer one file to another location.
after move operation source file will not be available at source location.
Consider above diag.
1] show’s current file status and directory status.
2] when we perform move operation a.txt get move from /home path to Anil directory whose path is
/home/Anil.
Here a.txt will no longer available at /home path.
Both moving and renaming activities can be performed by using single command: mv
1) Renaming of files:
$ mv old name newname
E.g: $ file1.txt file2.txt file1.txt will be renamed to file2.txt
2) Renaming of Directories:
$ mv dir1 dir2 dir1 will be renamed to dir2
3) Moving files to directory:
$ mv a.txt b.txt c.txt Anil
a.txt, b.txt and c.txt will be moved to Anil directory.
4) Moving of all files from one directory to another directory:
$ mv dir1/* dir2
All files of dir1 will be moved to dir2. After executing this command dir1 will become empty.
4) Moving total directory to another directory:
$ mv dir1 dir2
Note: If dir2 is already available then dir1 will be moved to dir2. If dir1 is not already available then
dir1 will be renamed to dir2.