Home > Linux > Copy file in Linux

Copy file in Linux


To copy files, you use the cp command. The following will copy file to file2. Note that if file2 doesn’t exist, it’ll be created, but if it exists, it’ll be overwritten:
cp file file2

There aren’t any undo commands in the Linux CLI, so accidentally overwriting an important file would probably make you pull your head off. The risk of doing so is smaller if you use the -i option (“interactive”) with cp. The following does the same as the above, but if file2 exists, you’ll be prompted before overwriting:

cp -i file file2
cp: overwrite `file2'? n
$

So it’s a good idea to use the -i option whenever you’re dealing with important files you don’t want to lose!

If you want to copy file into directory dir1:
cp file dir1

The following would do the same as the above, copy file into dir1, but under a different name:
cp file dir1/file2

You can also copy multiple files into one directory with a single command:
cp file1 file2 file3 dir1

Note that if the last argument isn’t a directory name, you’ll get an error message complaining about it.

Advertisements
Categories: Linux Tags: , , , , , , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: