In this blog we will learn about cp command .cp command is used to copy files and directories in Linux .First of all lets open our terminal , We can press ctrl+alt+ t to do this .
1.For giving cp command you use cp and then options and then you can write the source and then the destination.Basic command : cp options source destination .For example If we want to copy a file into a new file so what we can do is we can just write cp and then write the name of file 1 which we want to copy and then the new file .Command : cp file_name newfile_name .
2.Second thing we can do here is we can copy a file to a directory .The command looks in this case : cp file_name directory_name .
If you want to copy mutiple file you can just give name of all those files like : cp file1 file2 destination_directory
3.If a file is already existing inside a directory , then the above commands will overwrite the previous files .So to prevent this you can use -i flag in the command . Command : cp -i file1 file2 destination . This command will ask you question and if you give Y here then it’s going to overwrite your file and if you give n here then it’s going to not overwrite it .So you can use I for theinteractive interface .
4.To copy a file to your current directory you can use .(dot) as destination. So command : cp file_path .
For the destination part, you can give relative as well as absolute path also .
5.To copy a directory you can use same type of command .But if directory contains some files and then it’s not able to copy .In order to copy our directory which contains a file or files you can give (- R ) which is for recursive copying .Command : cp -R source_directory destination_directory .
This is how you can use a cp command and for more detail you have this command : man cp .
Leave a Reply