Blog

03/06/2019

Ubuntu Copy File Command



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:[donotprint][/donotprint]

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

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.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...