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,
I find vnstat to be useful in monitoring ‘net useage at home.
Especially if I configure the home network to make everything flow through the desktop.
In Windows, I click on "Upgrade", go take a leak and come back and it's upgraded. One mouse click....Done!
In Linux, I'm in the File Manager and see a folder with the program I want to run. What file do I click on to make it run? In Windows, just find the EXE file and click. Done! I can operate Linux from a gui, but trying to make my way around behind the gui make NO SENSE to a windows user. basically, I can surf the net, play an audio file, play a video file, and......gimmmeee a minute,.....That's about it. I work with drivers, disks, USB, diagnose problems and fix them, ect in Windows. I can't even mess with a dis without having to mount it first. Why can't it just see the disk and guess that I might want to mess with it and mount it automatically without trying to figure out if it's SDb4 or SDb5? Every single act in Linux is a pain in the butt.
I just think some guru software writer out there could change Linux distro's to act more user friendly( doesn't have to look like Windows) but just act more logically. I'm sure if I was raised on Linux( I'm 67 and started on Dos with a 286@12mhz) maybe Linux would make more sense to me. But starting with Microsoft just makes Windows more logical to me. I would love to use Linux, but it's too much work to get less done. If I spoke the lingo it would be great.
In Windows, I click on "Upgrade", go take a leak and come back and it's upgraded. One mouse click....Done!
Disclaimer: I run Fedora at home and Red Hat at work. I'm unfamiliar with Debian-based distros like Mint.
That being said, I believe there is a "Software Updater" or Manager, or something like that that performs the same function. Click it and you are updated.
There are also command-line variants where you just type in "apt-get upgrade" and your system gets updated.
Depending upon your version of Linux(Unix) you would use one of two methods to install pre-packaged Linux ( software packages ) called rpm files from a repository of updates made by the vendor.
RedHat Variants use yum
Debian variants use apt-get
Solaris(Oracle) Unix variants use pkgadd
At the command line level.
http://man7.org/linux/man-pages/man8/yum.8.html
or
http://man7.org/linux/man-pages/man1/dpkg.1.html
or
https://docs.oracle.com/cd/E19455-01/806-0625/6j9vfilsu/index.html
There is a binding between software file types and commands
that enable you to click on a package icon and install it through the graphical user interface of your choice.
The sudo comman and its config file has to give you administrative permission to run these commands through the graphical interface otherwise you have to sudo to root and type the command manually at a command line terminal application interface of your choice.
Setting this up varies also depending on your desktop version, Gnome, KDE, etc.
If you use the yum.conf file you can config the repositories and separate vendor repositories where software updates are grabbed off the internet and simply type:
sudo yum install.
debian has a similar command.
All these can be scheduled to occur automatically.
At the bottom of every Linux manual page like the two linked above there is a “SEE ALSO” section. Like this
SEE ALSO
apt-get(8), apt-cache(8), sources.list(5), apt.conf(5), apt-config(8), The APT User’s guide in /usr/share/doc/apt-doc/, apt_preferences(5), the APT Howto.
The number in parenthesis is the section of the manual.
Separte sections exist for commands section (1), programmers API section (2), software configurations(8) etc.
man -s 1 yum
is from section 1
man -s 8 yum
is from section 8 etc.
Full manuals have a package name and can be installed locally as a choice when you installed your distribution or later when the need arises.
Once the manuals are installed you can look up any command with the man command:
Example:
man yum
or
man dpkg.
even man man.
There is also a graphical interface program that enables you to browse the manual and all it’s pages like an adobe pdf reader.
Every command you need and Applications Programmer interface call at the C or C++ programming language is also available.
Software packages that are properly written will install new manual pages for that package.
For example a complete set of manual pages for all programs and library’s will be installed when you install the python packages of the Development environment packages.
There is an interface that allows you to browse graphically the repository of software availabke, patches, updates and third party vendor software also.
There are software packages that are distributed in a manner that requires you to unpack them from a compresses tar (tape archive), run a build or config command that sets up a file that has appropriate flags for the compilers etc and then run the make command, followed by make install.
Within the tar containing the software is usually a file named README that gives full instructions for configuring, building and installing the software.
These are usually not “professionally” packaged from a vendor or opensource source. Some are from universities or just available as is type stuff.
I’m done I could go on and on all day about this stuff.
Build software distribution packages for all the above methods is an art form in itself.
or
or
Pulling all of this togetor is very complex Integrated Development Environments like this
I’m so glad I retired. My brain was about to pop before I quit.
Pulling all this together for serious developers can take years after getting a BS and/or MS in Computer Science and the luck of working in a professional environments. The devil is in the details. Most environments aren’t professional.
When I login to my Linux, I can select the desktop environment I use before selecting my User Id and entering my password.
The Gnome environment has gone a long way for making it easy.
Different Desktop environments have different environments.
Unix unlike windows is a command line environment with a Graphical interface built on top off it.
Windows is a graphical environment that changes in every release with a poor operating system underneath it.
One was built from the bottom up; The other was built from the top down.
It is a whole different design philosophy in Unix that evolved from a research environment.
The best book for understanding what is under the hood in Unix and why is Kernighan and Pikes book,
“The Unix programming Environment”
Good Hunting... from Varmint Al
Good Hunting... from Varmint Al
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.