How to use cat and cp commands on Linux/Unix with examples

Cpanel/Whm License $3/mo Plesk License $10/mo Cloudlinux License $5/mo

(: July 29, 2019)

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.

.td_uid_2_5d9099b906593_rand.td-a-rec-img{text-align:left}.td_uid_2_5d9099b906593_rand.td-a-rec-img img{margin:0 auto 0 0}
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 same as –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:

How to extract .xz files on Linux – CenOS / Ubuntu / Debian

How to use rsync command on Linux/Unix with examples

How to extract .xz files on Linux – CenOS / Ubuntu / Debian

.td_uid_4_5d9099b906798_rand.td-a-rec-img{text-align:left}.td_uid_4_5d9099b906798_rand.td-a-rec-img img{margin:0 auto 0 0}

Related posts

Find Out Vmware Server Version Under Linux Server

SXI ADMIN

Telecom Giant KDDI Joins Enterprise Ethereum Alliance

SXI ADMIN

Bank Veterans Raise $1.5 Million for Digital Asset Startup

SXI ADMIN

UK Financial Regulators Are Preparing for a World of Crypto Assets

SXI ADMIN

9 Years: LocalBitcoins Trader Sentenced in Latest Money Transmission Case

SXI ADMIN

Coinbase Rolls Out System to Free Up Stuck Bitcoin Payments

SXI ADMIN

Mobile Payments Pioneer: I’m Betting on Bitcoin For Emerging Markets

SXI ADMIN

Korea Exchange Talks Top-Down Approach to Blockchain Innovation

SXI ADMIN

Install Snapd and Snap applications on Fedora 30 / Fedora 29/28

SXI ADMIN

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More