Free Republic
Browse · Search
General/Chat
Topics · Post Article

Skip to comments.

16 Useful ‘cp’ Command Examples for Linux Beginners
Linux Techi ^ | 10 February 2019 | Pradeep Kumar

Posted on 02/12/2019 7:11:11 AM PST by ShadowAce

In this article we will demonstrate 16 useful cp command examples specially for the linux beginners. Following is the basic syntax of cp command,

Copy a file to another file

# cp {options} source_file target_file

Copy File(s) to another directory or folder

# cp {options} source_file   target_directory 

Copy directory to directory

# cp {options} source_directory target_directory

Let’s jump into the practical examples of cp command,

Example:1) Copy file to target directory

Let’s assume we want copy the /etc/passwd file to /mnt/backup directory for some backup purpose, so run below cp command,

root@linuxtechi:~# cp /etc/passwd /mnt/backup/
root@linuxtechi:~#

Use below command to verify whether it has been copied or not.

root@linuxtechi:~# ls -l /mnt/backup/
total 4
-rw-r--r-- 1 root root 2410 Feb  3 17:10 passwd
root@linuxtechi:~#

Example:2 Copying multiple files at the same time

Let’s assume we want to copy multiples (/etc/passwd, /etc/group & /etc/shadow) at same time to target directory (/mnt/backup)

root@linuxtechi:~# cp /etc/passwd /etc/group /etc/shadow /mnt/backup/
root@linuxtechi:~#

Example:3) Copying the files interactively (-i)

If you wish to copy the files from one place to another interactively then use the “-i” option in cp command, interactive option only works if the destination directory already has the same file, example is shown below,

root@linuxtechi:~# cp -i /etc/passwd /mnt/backup/
cp: overwrite '/mnt/backup/passwd'? y
root@linuxtechi:~#

In the above command one has to manually type ‘y’ to allow the copy operation

Example:4) Verbose output during copy command (-v)

If you want the verbose output of cp command then use “-v” option, example is shown below

root@linuxtechi:~# cp -v /etc/fstab  /mnt/backup/
'/etc/fstab' -> '/mnt/backup/fstab'
root@linuxtechi:~#

In case you want to use both interactive mode and verbose mode then use the options “-iv”

root@linuxtechi:~# cp -iv /etc/fstab  /mnt/backup/
cp: overwrite '/mnt/backup/fstab'? y
'/etc/fstab' -> '/mnt/backup/fstab'
root@linuxtechi:~#

Example:5) Copying a directory or folder (-r or -R)

To copy a directory from one place to another use -r or -R option in cp command. Let’s assume we want to copy the home directory of linuxtechi user to “/mn/backup”,

root@linuxtechi:~# cp -r /home/linuxtechi /mnt/backup/
root@linuxtechi:~#

In above command, -r option will copy the files and directory recursively.

Now verify the contents of linuxtechi directory on target place,

root@linuxtechi:~# ls -l /mnt/backup/linuxtechi/
total 24
drwxr-xr-x 2 root root 4096 Feb  3 17:41 data
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_1.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_2.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_3.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_4.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_5txt
-rw-r--r-- 1 root root    0 Feb  3 17:41 file_5.txt
root@linuxtechi:~#

Example:6) Archive files and directory during copy (-a)

While copying a directory using cp command we generally use -r or -R option, but in place of -r option we can use ‘-a’ which will archive the files and directory during copy, example is shown below,

root@linuxtechi:~# cp -a /home/linuxtechi /mnt/backup/
root@linuxtechi:~# ls -l /mnt/backup/linuxtechi/
total 24
drwxr-xr-x 2 root root 4096 Feb  3 17:41 data
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_1.txt
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_2.txt
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_3.txt
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_4.txt
-rw-r--r-- 1 root root    7 Feb  3 17:40 file_5txt
-rw-r--r-- 1 root root    0 Feb  3 17:39 file_5.txt
root@linuxtechi:~#

Example:7) Copy only when source file is newer than the target file (-u)

There can be some scenarios where you want copy the files only if the source files are newer than the destination ones. This can be easily achieved using “-u” option in the cp command.

In the Example:6  we have copied the linuxtechi home directory to /mnt/backup folder, in the linuxtechi home folder we have 5 txt files, let’s edit couple of them and then copy all the txt files using “cp -u”.

root@linuxtechi:~# cd /home/linuxtechi/
root@linuxtechi:/home/linuxtechi# echo "LinuxRocks" >> file_1.txt
root@linuxtechi:/home/linuxtechi# echo "LinuxRocks" >> file_4.txt
root@linuxtechi:/home/linuxtechi# cp -v -u  file_*.txt /mnt/backup/linuxtechi/
'file_1.txt' -> '/mnt/backup/linuxtechi/file_1.txt'
'file_4.txt' -> '/mnt/backup/linuxtechi/file_4.txt'
root@linuxtechi:/home/linuxtechi#

Example:8) Do not overwrite the existing file while copying (-n)

There are some scenarios where you don’t want to overwrite the existing destination files while copying. This can be accomplished using the option ‘-n’ in ‘cp’ command

root@linuxtechi:~# cp -i /etc/passwd /mnt/backup/
cp: overwrite '/mnt/backup/passwd'?

As you can see in above command, it is prompting us to overwrite the existing file, if you use -n then it will not prompt for the overwrite and also will not overwrite the existing file.

root@linuxtechi:~# cp -n /etc/passwd /mnt/backup/
root@linuxtechi:~#

Example:9) Creating symbolic links using cp command (-s)

Let’s assume we want to create symbolic link of a file instead copying using cp command, for such scenarios use ‘-s’ option in cp command, example is shown below

root@linuxtechi:~# cp -s /home/linuxtechi/file_1.txt /mnt/backup/
root@linuxtechi:~# cd /mnt/backup/
root@linuxtechi:/mnt/backup# ls -l file_1.txt
lrwxrwxrwx 1 root root 27 Feb  5 18:37 file_1.txt -> /home/linuxtechi/file_1.txt
root@linuxtechi:/mnt/backup#

Example:10) Creating Hard link using cp command (-l)

If you want to create hard link of a file instead copy using cp command, then use ‘-l’ option. example is shown below,

root@linuxtechi:~# cp -l /home/linuxtechi/devops.txt /mnt/backup/
root@linuxtechi:~#

As we know in hard link, source and linked file will have the same inode numbers, let’s verify this using following commands,

root@linuxtechi:~# ls -li /mnt/backup/devops.txt
918196 -rw-r--r-- 2 root root 37 Feb  5 20:02 /mnt/backup/devops.txt
root@linuxtechi:~# ls -li /home/linuxtechi/devops.txt
918196 -rw-r--r-- 2 root root 37 Feb  5 20:02 /home/linuxtechi/devops.txt
root@linuxtechi:

Example:11) Copying attributes from source to destination (–attributes-only)

If you want to copy only the attributes from source to destination using cp command, then use option “–attributes-only

root@linuxtechi:/home/linuxtechi# cp --attributes-only /home/linuxtechi/distributions.txt /mnt/backup/
root@linuxtechi:/home/linuxtechi# ls -l /home/linuxtechi/distributions.txt
-rw-r--r-- 1 root root 41 Feb  5 19:31 /home/linuxtechi/distributions.txt
root@linuxtechi:/home/linuxtechi# ls -l /mnt/backup/distributions.txt
-rw-r--r-- 1 root root 0 Feb  5 19:34 /mnt/backup/distributions.txt
root@linuxtechi:/home/linuxtechi#

In the above command, we have copied the distribution.txt file from linuxtechi home directory to /mnt/backup folder, if you have noticed, only the attributes are copied, and content is skipped. Size of distribution.txt under /mn/backup folder is zero bytes.

Example:12) Creating backup of existing destination file while copying (–backup)

Default behavior of cp command is to overwrite the file on destination if the same file exists, if you want to make a backup of existing destination file during the copy operation then use ‘–backup‘ option, example is shown below,

root@linuxtechi:~# cp --backup=simple -v /home/linuxtechi/distributions.txt /mnt/backup/distributions.txt
'/home/linuxtechi/distributions.txt' -> '/mnt/backup/distributions.txt' (backup: '/mnt/backup/distributions.txt~')
root@linuxtechi:~#

If you have noticed, backup has been created and appended tilde symbol at end of file. backup option accept following parameters

Example:13) Preserve mode, ownership and timestamps while copying (-p)

If you want to preserve the file attributes like mode, ownership and timestamps while copying then use -p option in cp command, example is demonstrated below,

root@linuxtechi:~# cd /home/linuxtechi/
root@linuxtechi:/home/linuxtechi# cp -p devops.txt /mnt/backup/
root@linuxtechi:/home/linuxtechi# ls -l devops.txt
-rw-r--r-- 1 root root 37 Feb  5 20:02 devops.txt
root@linuxtechi:/home/linuxtechi# ls -l /mnt/backup/devops.txt
-rw-r--r-- 1 root root 37 Feb  5 20:02 /mnt/backup/devops.txt
root@linuxtechi:/home/linuxtechi#

Example:14) Do not follow symbolic links in Source while copying (-P)

If you do not want to follow the symbolic links of source while copying then use -P option in cp command, example is shown below

root@linuxtechi:~# cd /home/linuxtechi/
root@linuxtechi:/home/linuxtechi# ls -l /opt/nix-release.txt
lrwxrwxrwx 1 root root 14 Feb  9 12:28 /opt/nix-release.txt -> os-release.txt
root@linuxtechi:/home/linuxtechi#
root@linuxtechi:/home/linuxtechi# cp -P os-release.txt /mnt/backup/
root@linuxtechi:/home/linuxtechi# ls -l /mnt/backup/os-release.txt
-rw-r--r-- 1 root root 35 Feb  9 12:29 /mnt/backup/os-release.txt
root@linuxtechi:/home/linuxtechi#

Note: Default behavior of cp command is to follow the symbolic links in source while copying.

Example:15) Copy the files and directory forcefully using -f option

There can be some scenarios where existing destination file cannot be opened and removed. And if you have healthy file which can be copied in place of existing destination file, then use cp command along with -f option

root@linuxtechi:/home/linuxtechi# cp -f distributions.txt  /mnt/backup/
root@linuxtechi:/home/linuxtechi#

Example:16) Copy sparse files using sparse option in cp command

Sparse is a regular file which contains long sequence of zero bytes that doesn’t consume any physical disk block. One of benefit of sparse file is that it does not consume much disk space and read operation on that file would be quite fast.

Let’s assume we have sparse cloud image named as “ubuntu-cloud.img”

root@linuxtechi:/home/linuxtechi# du -sh ubuntu-cloud.img
12M     ubuntu-cloud.img
root@linuxtechi:/home/linuxtechi# cp --sparse=always ubuntu-cloud.img /mnt/backup/
root@linuxtechi:/home/linuxtechi# du -sh /mnt/backup/ubuntu-cloud.img
0       /mnt/backup/ubuntu-cloud.img
root@linuxtechi:/home/linuxtechi#

Different options can be used while using sparse parameter in cp command,



TOPICS: Computers/Internet
KEYWORDS: linux
Navigation: use the links below to view more comments.
first 1-2021-27 next last

1 posted on 02/12/2019 7:11:11 AM PST by ShadowAce
[ Post Reply | Private Reply | View Replies]

To: rdb3; Calvinist_Dark_Lord; JosephW; Only1choice____Freedom; Ernest_at_the_Beach; martin_fierro; ...

2 posted on 02/12/2019 7:11:38 AM PST by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce

If that is for beginners than it’s no wonder that no matter how bad Windows is, Linux is still a fringe operating system and remains a long way from going prime time.


3 posted on 02/12/2019 7:44:23 AM PST by qam1 (There's been a huge party. All plates and the bottles are empty, all that's left is the bill to pay)
[ Post Reply | Private Reply | To 1 | View Replies]

To: qam1

Linux has many flavors and a plethora of ways to get things done. It is mainstream in many areas.

It’s mainstream on my Desktops.


4 posted on 02/12/2019 7:49:10 AM PST by Paladin2
[ Post Reply | Private Reply | To 3 | View Replies]

To: qam1

These are meant to be used at the command line. Linux GUI interfaces have point and click similar to Windows.


5 posted on 02/12/2019 7:53:33 AM PST by unixfox (Abolish Slavery, Repeal the 16th Amendment)
[ Post Reply | Private Reply | To 3 | View Replies]

To: qam1

These are command line examples that I can run on a Linux computer the size of your thumb, or on an i7 desktop. Linux doesn’t pretend to be Windows, but with a suitable GUI it can be as point-and-click easy to do the same thing.

The advantage is having the freedom to choose, and not being burdened with bloatware that isn’t necessary. For copying files from my NAS to a media player on the other side of the house for example, the CLI works just fine and I can do it from any device on my LAN.


6 posted on 02/12/2019 8:04:07 AM PST by bigbob (Trust Trump. Trust the Plan.)
[ Post Reply | Private Reply | To 3 | View Replies]

To: ShadowAce
This is why Linux will never be mainstream. I left DOS @ version 6.2. There is some sort of mental condition that keeps Linux people from fixing what could be the Microsoft killer. All that would have to be done is a few people rewrite Linux to operate sorta like Windows, or even easier, Apple. Apple's insides are pretty much Linux in drag. But Apple people will go for months or even years without pulling up a command line. When you hit "update" you don't have to know what goes into which folder on which drive, it just goes where the software writers tells it to go. Linux could do it, but they just refuse. How wonderful would it be to buy a machine from Walmart and the high school cheer leader could take it out of the box and operate it? They use their Android phone without a 6 week class because Android OS is designed for people to use intuitively.

If people have trouble with Windows, how will they learn Linux before they die? The Linux snobs just chuckle as they bark 2 pages of terminal commands to fix the problem a newbie might have, and when it doesn't work, they just chuckle as they make a smart remark saying they didn't stutter. The windows user sulks as they slither back to Bill Gates and the Linux users go back to speaking about the superiority of Linux.

The idea to move Linux to the main stream should be to make it easy to operate and understand. NO ONE wants to type 50 lines of commands. People will go through broken glass to be able to drag and drop a file rather than type in Terminal what they want it to do.

7 posted on 02/12/2019 8:14:40 AM PST by chuckles
[ Post Reply | Private Reply | To 1 | View Replies]

To: chuckles; ShadowAce
How wonderful would it be to buy a machine from Walmart and the high school cheer leader could take it out of the box and operate it?

How wonderful would it be to buy a machine from Walmart and I could take it out of the box and operate it?

Oh, man ... I just ride in 'em. I don't know what makes 'em work.


8 posted on 02/12/2019 8:26:18 AM PST by BlueLancer (Orchides Forum Trahite - Cordes Et Mentes Veniant)
[ Post Reply | Private Reply | To 7 | View Replies]

To: ShadowAce

bttt


9 posted on 02/12/2019 8:26:54 AM PST by imardmd1 (Fiat Lux)
[ Post Reply | Private Reply | To 1 | View Replies]

To: qam1
I have converted nearly a dozen people over from Windows to Linux Mint. All of them are happy with Linux and they do not need to use the command line. But the command line is a very powerful option if one wants to LEARN. Linux is really easy if you use Firefox and Thunderbird most of the time.

Good Hunting... from Varmint Al

10 posted on 02/12/2019 9:15:58 AM PST by Varmint Al
[ Post Reply | Private Reply | To 3 | View Replies]

To: ShadowAce

I’m not a Linux user. However, I’ve got an install DVD of Linux Mint, ver 19.1 I hope to be installing soon.

I assume Linux has a point and click interface, similar to Windows where I can select a file, navigate to the source directory and deposit the selected file. True?

This tutorial is kinda like using the DOS command COPY in Windows. Still has some uses but most Windows users prefer the point and click method.


11 posted on 02/12/2019 10:05:57 AM PST by upchuck (When a society is open, then it [the Left] canÂ’t win. ~ Daniel Greenfield)
[ Post Reply | Private Reply | To 1 | View Replies]

To: upchuck
Yes, that is true.

Linux typically has multiple ways to accomplish any given task. It can work the way you want to work.

I've been posting command line tutorials here because there have been people who have expressed an interest--and because tutorials on how to copy-and-paste, or drag-and-drop are not really interesting. :)

12 posted on 02/12/2019 10:27:35 AM PST by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 11 | View Replies]

To: ShadowAce

Thank you.


13 posted on 02/12/2019 11:53:54 AM PST by upchuck (When a society is open, then it [the Left] canÂ’t win. ~ Daniel Greenfield)
[ Post Reply | Private Reply | To 12 | View Replies]

To: upchuck
Linux Mint is very easy to switch to after Windows. It looks very similar and has point and click. Also, the Linux kernel has all of the drivers for most all computers. When you install Windows and you have to use all those CD's install drivers, Not so with Linux. I have one, gal 95 years old, that does not understand anything about computers and she had no problem using Linux Mint.

Good Hunting... from Varmint Al

14 posted on 02/12/2019 12:01:09 PM PST by Varmint Al
[ Post Reply | Private Reply | To 11 | View Replies]

To: Varmint Al

Thanks for the info.

Know of a Foxit (PDF reader) workalike for Linux?


15 posted on 02/12/2019 12:42:46 PM PST by upchuck (When a society is open, then it [the Left] canÂ’t win. ~ Daniel Greenfield)
[ Post Reply | Private Reply | To 14 | View Replies]

To: ShadowAce

I use a Linux command line at work all day(3 Red Hat mainframes). When I go home, I never use the command line.


16 posted on 02/12/2019 12:47:18 PM PST by AppyPappy (How many fingers am I holding up, Winston?)
[ Post Reply | Private Reply | To 12 | View Replies]

To: upchuck

Evince is a PDF reader for Linux. It typically comes installed with any linux system you install. If not, it’s easy to find.


17 posted on 02/12/2019 1:33:30 PM PST by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 15 | View Replies]

To: ShadowAce

Well done, I enjoy your posts. I’m not a linux newbie though I pick up something new from your posts on this topic whenever I find them. Thanks!


18 posted on 02/12/2019 1:37:57 PM PST by usconservative (When The Ballot Box No Longer Counts, The Ammunition Box Does. (What's In Your Ammo Box?))
[ Post Reply | Private Reply | To 2 | View Replies]

To: Varmint Al
I converted my (now ex) mother in law from Windows XP to Linux about a year before Microsoft XP support ended.

Yes, I used Linux Mint for her. She hardly noticed the difference since she had Google Chrome as her browser before and honestly she liked Thunderbird eMail better than the crappy Outlook Web Interface. Since all she did was browse, listen to some German stations occasionally and send emails it did everything she needed AND I didn't have to deal with her getting computer viruses or worry about her breaking something.

19 posted on 02/12/2019 1:42:29 PM PST by usconservative (When The Ballot Box No Longer Counts, The Ammunition Box Does. (What's In Your Ammo Box?))
[ Post Reply | Private Reply | To 10 | View Replies]

To: AppyPappy

I find vnstat to be useful in monitoring ‘net useage at home.

Especially if I configure the home network to make everything flow the desktop.


20 posted on 02/12/2019 7:02:22 PM PST by Paladin2
[ Post Reply | Private Reply | To 16 | View Replies]


Navigation: use the links below to view more comments.
first 1-2021-27 next last

Disclaimer: Opinions posted on Free Republic are those of the individual posters and do not necessarily represent the opinion of Free Republic or its management. All materials posted herein are protected by copyright law and the exemption for fair use of copyrighted works.

Free Republic
Browse · Search
General/Chat
Topics · Post Article

FreeRepublic, LLC, PO BOX 9771, FRESNO, CA 93794
FreeRepublic.com is powered by software copyright 2000-2008 John Robinson