Ubuntu Copy File Command

by on February 19, 2014 · 8 comments· LAST UPDATED February 19, 2014

in , ,

I'm a new Ubuntu Linux user. This seems like a newbie question, but I can not seem to find examples to copy files on Ubuntu. How do I copy file to another directory on Ubuntu Linux using command line terminal application? How can I copy files via terminal on Ubuntu Linux desktop?

You need to use cp command to copy file to another directory or external usb disk. The command line syntax is as follows to copy files via terminal:
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time10m

cp old_name new_name

OR

cp [options] old_name new_name

OR

cp source dest

OR

cp /path/to/source /path/to/dest/directory/

Examples: Copy file1.txt to /tmp directory

Open the Terminal and type the following command in the current directory to copy a file called file1.txt with the same name into /tmp/ directory:

 
cp file1.txt /tmp/
 

Use ls command to verify new files:

 
ls /tmp/
ls -l /tmp/
ls -l /tmp/file1.txt
 

You can copy multiple files into another directory. In this example, copy the files called foo.txt, bar.doc, resume.pdf into a directory called /media/backup/

 
cp  foo.txt bar.doc resume.pdf /media/backup/
 

Cp command can explain what is being done with -v option:

 
cp  -v foo.txt bar.doc resume.pdf /media/backup/
 

Sample session from all cp commands featured in this tutorials:

Animated gif: Ubuntu Linux copy files via terminal using cp command demo

Animated gif: Ubuntu Linux copy files via terminal using cp command demo

Ubuntu make a backup of each existing destination file

In this example, copy file named birthday_party.avi to /media/usbpen/ and make a backup of each existing destination file:

 
cp -b birthday_party.avi /media/usbpen/
 

Copy ~/Documents/ folder to /media/usbpen/

Pass the -r (recursive) option to cp command. In this example, make a copy of an existing directory called ~/Documents/, inclusive of all it contents (i.e., files, subdirectories, their subdirectories, etc.), to directory called /media/usbpen/:

 
cp -r ~/Documents/ /media/usbpen/
 

Copy files interactively

Pass the -i optipn to cp command to prompts the user to the screen before copying a file that would overwrite an existing file:

 
cp -i file1.txt /tmp/
 

If you do not wish to overwrite an existing file i.e. overrides a previous -i option, try:

 
cp -n file1.txt /tmp/
 

Copy all files in a directory to another directory by using the star wildcard

To copy all files from ~/Pictures/ to /nfs/backups/pics/ directory, enter:

 
cp ~/Pictures/* /nfs/backups/pics/
 

In this example, copy all of the files in the current directory that have the filename extension .py into another existing directory called /nfs/backups/python/:

 
cp *.py /nfs/backups/python
 

Dealing with permission denied error

If you try to copy files to /root/ or any other system areas, you will see an error that read as follows:

cp backdoor3.c /usr/
cp: /usr/backdoor3.c: Permission denied

Use the sudo command to copy files when you need superuser privileges on Ubuntu Linux:

sudo cp backdoor3.c /usr/

Tip: Searching for commands without knowing their exact names

You can use apropos command to displays a list of all topics in the built-in user manual that are related to the subject. The syntax is:

 
apropos query
apropos query | less
apropos keywords | grep 'something'
 

In this example, get the list of editing programs/commands that are available on a system:

 
apropos editor
apropos editor | less
 

Sample outputs:

ed                   (1)  - text editor
ex                   (1p)  - text editor
mcedit               (1)  - Internal file editor of GNU Midnight Commander
nano                 (1)  - Nano's ANOther editor, an enhanced free Pico clone
psed                 (1)  - a stream editor
sed                  (1p)  - stream editor
sed                  (1)  - stream editor for filtering and transforming text
vi                   (1p)  - screen-oriented (visual) display editor
vim                  (1)  - Vi IMproved, a programmers text editor

Finally, use whatis and man command to obtain information about the mcedit or vi command that apropos provides:

 
whatis mcedit
whatis vi
man vi
man mcedit
 

And there you have it, cp command that copies files and directories on Ubuntu based systems. I strongly suggest that your read cp command man page or see our cp command examples page for more information.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 8 comments… read them below or add one }

1 Sri February 19, 2014 at 5:44 pm

No offense, but this is so noobish information. It is like posting instructions on how to walk. Only post advanced stuff, or I am going to remove your rss from my daily reads.

Reply

2 julian67 February 19, 2014 at 6:39 pm

I think noobie questions are OK. Everyone has to start somewhere and even experienced users can have surprising gaps in their knowledge – probably not many who don’t know cp inside out though :-)

Anyway imo a better answer to such a noob question might be to describe the command `apropos`, and show a small section of a sample output:

$ apropos copy
Clone (3pm) – recursively copy Perl datatypes
asn1_copy_node (3) – API function
bcopy (3) – copy byte sequence
copysign (3) – copy sign of a number
copysignf (3) – copy sign of a number
copysignl (3) – copy sign of a number
cow-shell (1) – Start a copy-on-write session and invoke a shell.
cp (1) – copy files and directories
cpgr (8) – copy with locking the given file to the password or group file
cpio (1) – copy files to and from archives
cppw (8) – copy with locking the given file to the password or group file

etc etc…

apropos is a great discovery tool for people new to a unix-like OS or new to the command line and is also useful for experienced users. It doesn’t get mentioned very often but in conjunction with man and info pages and a little curiosity it is a great tool for exploring what your system contains and can offer you.

Reply

3 Nix Craft February 19, 2014 at 6:52 pm

Excellent suggestion. The faq has been updated to include your tip. I appreciate your feedback and post.

Reply

4 T.Mini February 19, 2014 at 6:58 pm

@julian67: Yup, give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.

@Sri: Please unsubscribe. You are not adding anything to the discussion by behaving like this.

Reply

5 julian67 February 19, 2014 at 7:19 pm

…and if you’re really sharp you can then direct him to a fishing tackle retailer and take commission on the sales :-)

Reply

6 Justin February 20, 2014 at 12:02 pm

How did you create the animated gif?

Reply

7 luser February 21, 2014 at 7:04 am
8 ae April 3, 2014 at 4:43 pm

good info., the -r was a lifesaver for me.

Reply

Leave a Comment

Tagged as:

Previous Faq:

Next Faq: