Skip to main content

Linux Command Line Tutorial For Beginners - cp command

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

Comments

Post a Comment

Popular posts from this blog

Linux Command Line Tutorial For Beginners - Introduction

Hi Friends, Welcome to Blog , Welcome to Linux . Now first question you may ask yourself why we should learn command line, even if we have GUI ? So, Simple answer is that GUI ( Graphical User Interface ) is helpful in many task but not useful in all task, and when you learn command, you will observe that you are able to do your day to day task or any task related to programming much faster that you can do with GUI . By using command you can able to see what happening in a background when you given a something task. Before dive into the world of Linux you should know two terms i.e., Shell and Terminal. Now what is Shell? Shell is a simply a program that takes inputs from the keyboard and gives that to the operating system to perform task.It is also called as CLI ( Command Line Interface ), Now the second term is Terminal, terminal is tool which is used to pass a shell command, this is a program that open up a window and lets interacts with shell. There are different operating...

Linux Command Line Tutorial For Beginners - ls command

ls is a Linux shell command that lists directory contents which may be files or directories. First of all lets open Terminal, Alt+Ctrl+T ls command uses following syntax:          ls [option] [file or directory name] if we give simple ls command it will show us all directories and files present in home directory, as when we open terminal by default we are in home directory. Now if we want to see what is in Downloads directory then we simply give command as:                          ls Downloads Note one thing i.e., command line is case sensitive so we should write name of directory or file as it is. In same way you can able to see contents of any directory. If we want to see content of root directory then we will write command as: As we know / is called as root directory.      ...