Learn how to – Use cat and cp commands on Linux/Unix with examples.
Introduction
There is no way you will spend your time on a Linux shell without the frequent need to copy or view the contents of a file. The cat and cp commands do that with the beauty they were designed to do.
The cat command
According to its man pages, cat concatenates files and prints on the standard output. Its simple syntax goes like:
cat [OPTION]… [FILE]…
The various options you can use are the following:
-A, --show-all
. Can show Tabs, end of lines using $ and non-printing-b, --number-nonblank
number nonempty output lines, overrides -n-e
equivalent to -vE-E, --show-ends
displays $ sign at end of each line-n, --number
number all output lines-s, --squeeze-blank
suppress repeated empty output lines-t
equivalent to -vT-T, --show-tabs
display TAB characters as ^I-v, --show-nonprinting
Examples of cat in action:
$ cat -A text.txt ## shows Tabs, end of lines using $ and non-printing
A test-preparation routine proven to help you pass Red HatM-bM-^@M-^Ys tough certification exams$
Clearly defined chapter learning objectives covering all RHCSA (EX200) and RHCE (EX300) exam topics$
Chapter-ending review questions to help you drill on key concepts you must know thoroughly$
Chapter-ending labs showing what real exam tasks and assignments look like$
$ cat -b rsync1.txt ## Numbers all non-blank lines
1 Features of Rsync include:
2 Support for copying links, devices, owners, groups, and permissions
3 Exclude and exclude-from options similar to GNU tar
4 A CVS exclude mode for ignoring the same files that CVS would ignore
5 Can use any transparent remote shell, including ssh or rsh
6 Does not require super-user privileges
7 Pipelining of file transfers to minimize latency costs
8 Support for anonymous or authenticated rsync daemons (ideal for mirroring)
$ cat -n rsync1.txt ## Numbers all lines whether empty or not
1 Features of Rsync include:
2
3 Support for copying links, devices, owners, groups, and permissions
4
5 Exclude and exclude-from options similar to GNU tar
6
7 A CVS exclude mode for ignoring the same files that CVS would ignore
8
9 Can use any transparent remote shell, including ssh or rsh
10 Does not require super-user privileges
11 Pipelining of file transfers to minimize latency costs
12 Support for anonymous or authenticated rsync daemons (ideal for mirroring)
Cat can also be used to combine two or more files into one.
cat file1.txt file2.txt >> file3.txt
The contents of file1.txt and file2.txt will be combined into file3.txt
Using Linux/Unix cp command
The simplest explanation of cp is a linux command line tool used for copying files and directories. It copies a file from SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Syntax
cp [source-file] [destination-file]
cp [source-file] [destination-directory]
cp [source-directory] [destination-directory]
Most common options for cp
-n, --no-clobber
do not overwrite an existing file-i, --interactive
prompt before overwrite-l, --link
hard link files instead of copying-R, -r, --recursive
copy directories recursively-s, --symbolic-link
make symbolic links instead of copying-p, --preserve=mode
, ownership, timestamps-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing-v, --verbose
show output of what is being done-Z
Set SELinux security context of destination file to default type
There is a tonne of other options to use with cp command. Kindly view its man pages for more.
Examples of cp in action
1.. Copy a file to a directory
cp /home/tech/django/index.html /home/pench/backup/
2.. Copy a file to the same directory
cp /home/tech/django/index.html index2.html
3.. Copy a directory recursively to another directory and show verbose output
cp -Rv /home/tech/django/ /home/pench/backup/ same as
cp -rv /home/tech/django/ /home/pench/backup/
4.. Copy files to a directory and making sure only new files are copied and others updated
cp -uv file1.txt file2.txt file3.txt /home/pench/backup/
5.. Cause cp to prompt and ask if you are sure about the copying
cp -i /etc/httpd/conf/httpd.conf /home/pench/backup/
6.. Copy files to a directory and making sure a file with same filename is not overwritten
cp -n file1.txt file2.txt file3.txt /home/pench/backup/
7.. Copy a file to a directory and preserve its ownership and timestamps
cp /home/tech/django/index.html /home/pench/backup/
8.. Create a symbolic link of index.html at index2.html with verbose output
cp -sv /home/tech/django/index.html index2.html
9.. Create a hard link of index.html at index2.html with verbose output
cp -lv /home/tech/django/index.html index2.html
Conclusion
We have looked at how to view and concatenate files with cat and how to copy your files and directories with cp command. We hope it was informative and led you to the right direction. If you are interested with other guides, please look below and click on the titles that catches your eye.
Similar:
- Extract .xz files on Linux – CenOS / Ubuntu / Debian
- Use rsync command on Linux/Unix with examples
- Extract .xz files on Linux – CenOS / Ubuntu / Debian
Leave a Reply
You must be logged in to post a comment.