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
Lets jump into the practical examples of cp command,
Lets 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:~#
Lets 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:~#
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
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:~#
To copy a directory from one place to another use -r or -R option in cp command. Lets 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:~#
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:~#
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, lets 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#
There are some scenarios where you dont 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:~#
Lets 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#
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, lets 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:
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.
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
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#
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.
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#
Sparse is a regular file which contains long sequence of zero bytes that doesnt 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.
Lets 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,
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.
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.
These are meant to be used at the command line. Linux GUI interfaces have point and click similar to Windows.
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.
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.
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.
bttt
Good Hunting... from Varmint Al
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.
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. :)
Thank you.
Good Hunting... from Varmint Al
Thanks for the info.
Know of a Foxit (PDF reader) workalike for Linux?
I use a Linux command line at work all day(3 Red Hat mainframes). When I go home, I never use the command line.
Evince is a PDF reader for Linux. It typically comes installed with any linux system you install. If not, it’s easy to find.
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!
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.
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.
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.