

Go to the destination directory and confirm whether all the files and sub directories copied #cd /output #ls -l Sample Output: drwxr-xr-x 6 root root 4096 dec 12 13:37 testdirĮx:4 How to select and copy all the files from a directory to another directory Here i have applied verbose and force option since while copying each and every file it will ask the confirmation from the user (Difficult for the system administrators if the directory contains some 100 or 500 files), i have granted yes to all by using the -f option. This is the destination directory #cp -rvf /testdir /output Now the directory /testdir is having some couple of files and some sub directories inside #mkdir /output Let me create a directory and add some files inside #mkdir /testdir #cd /testdir #touch f1 f2 f3 f4 #mkdir d1 d2 d3 d4 #cd / It allows directories including all of their contents to be copied: Go to the /vasanth directory and confirm #cd /vasanth #ls Sample output: mydata mydoc myfile Ex:3 To copy all directory and all its content to another directory:Ī directory and all its content can be copied from source to destination with the recursive option -r Here i have applied the verbose option (its not mandatory ) Sample output: mydoc -> /vasanth/mydoc `mydata -> ` /vasanth/mydata` `myfile -> ` /vasanth/myfile` Now copy all these files to a directory called /vasanth #mkdir /vasanth #cp -v /mydoc /mydata /myfile /vasanth We have successfully created three files with some contents. Let me create some couple of files with some contents : #echo “Buy lots of eggs” >/mydoc #echo “Buy some chicken” >/mydata #echo “Buy some cake” >/myfile Leave space between the files to copy multiple files to a directory To confirm go the /tmp directory and verify #cd /tmp #ls Sample output: backup.txtĮx:2To copy multiple files in to a directory syntax: #cp Successfully the file copied to the directory /tmp Now copy this file to the /tmp directory #cp /backup.txt /tmp Note:I have mentioned some of the important options here ,you can refer the man page to know more about the available options Ex:1copy a file called “backup.txt” in to another directory called /tmp.įirst create a file with some contents #echo “This is my first line” >/backup.txtĬheck the contents #cat /backup.txt Sample output: This is my first line You need to use the “cp” command to copies files and directories under the UNIX like operating systems Syntax:To copy a file to a directory #cp Options: -r -recursive ( use this option only when you need to remove the entire directory and all its contents) -v-Verbose -f-Forcefully How do i copy files and directories under UNIX like operating systems?

Total Shares Copy Command in Unix(cp command)
