In this Blog, we will learn how we can use cp command in Linux.
cp command is used to copy files and directories in Linux.
So, lets see how we can use it.
Open your Terminal Alt+Ctrl+t .
Syntax for cp command is:
cp [options] [source] [Destination]
In above example, text1.txt file already exists in Desktop then I use cp command and give destination file name as text2.txt which does not exists but cp command created new file and copied the content of text1.txt to text2.txt.
Now, if you want to copy a file into a directory then we will use command as:
cp [file_name] [Dir_name]
If you want to copy more than one file in a directory you can use command as:
cp [1st file] [2nd file] [Dir_name ]
Now if you want to copy the files to a directory in which same named file already exists and if you give same command as above then it will overwrite the content of that file in this situation you should use one option along with cp command i.e., -i which means interactive this will ask a permission before overwriting.
cp -i [file_name] [file_name] [Dir_name]
It will ask question overwrite or not ?
In reply if you give y then it will overwrite and if n then not overwrite.
If we want to copy content of one directory into the another directory which does not exists at all and if we use same command as above then it will show error.
To remove this type of error we will use option -R (Recursive) along with above command as:
cp -R [Dir_name] [Dir_name(which doesn't exists before)]
These are the some common examples of cp command you can get more options details using command:
man cp
For any query and suggestions you may contact with me or you may put comment in comment box.
Thank You.
Contact Details:
Phone: +91-8208826234
E-mail: shivampawar1038@gmail.com
cp command is used to copy files and directories in Linux.
So, lets see how we can use it.
Open your Terminal Alt+Ctrl+t .
Syntax for cp command is:
cp [options] [source] [Destination]
In above example, text1.txt file already exists in Desktop then I use cp command and give destination file name as text2.txt which does not exists but cp command created new file and copied the content of text1.txt to text2.txt.
Now, if you want to copy a file into a directory then we will use command as:
cp [file_name] [Dir_name]
If you want to copy more than one file in a directory you can use command as:
cp [1st file] [2nd file] [Dir_name ]
Now if you want to copy the files to a directory in which same named file already exists and if you give same command as above then it will overwrite the content of that file in this situation you should use one option along with cp command i.e., -i which means interactive this will ask a permission before overwriting.
cp -i [file_name] [file_name] [Dir_name]
It will ask question overwrite or not ?
In reply if you give y then it will overwrite and if n then not overwrite.
If we want to copy content of one directory into the another directory which does not exists at all and if we use same command as above then it will show error.
To remove this type of error we will use option -R (Recursive) along with above command as:
cp -R [Dir_name] [Dir_name(which doesn't exists before)]
These are the some common examples of cp command you can get more options details using command:
man cp
For any query and suggestions you may contact with me or you may put comment in comment box.
Thank You.
Contact Details:
Phone: +91-8208826234
E-mail: shivampawar1038@gmail.com
Awesome
ReplyDeleteThank You.
DeleteThat's understandable
ReplyDeleteThank You.
Delete