DEV Community

loading...
Cover image for THE CAT COMMAND IN LINUX

THE CAT COMMAND IN LINUX

Bazeng
Engineering is a life time commitment to learning. I am here to document what i learn
・2 min read

Cat(concatenate) command reads data from the file and gives their content as output.

It helps us to create, view, concatenate files.

Here are some of the use cases of the command :

1. Creating a new file:

$ cat >newfile

Enter fullscreen mode Exit fullscreen mode

Creates a new file called newfile

2. Reading a file:

You may have run into this while checking your os type.

$ cat /etc/os-release
Enter fullscreen mode Exit fullscreen mode

On my Os this outputs:

PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Enter fullscreen mode Exit fullscreen mode

3. Copy the contents of one file to another file.

$cat [filename-whose-contents-is-to-be-copied] > [destination-filename]

Enter fullscreen mode Exit fullscreen mode

4. View multiple files

$ cat file1 file2

Enter fullscreen mode Exit fullscreen mode

Outputs contents in file1 and file2 .

5. Append the contents of one file to the end of another file

$cat file1 >> file2

Enter fullscreen mode Exit fullscreen mode

Will append contents of file1 to file 2

6. Cat command to open dashed files.

$cat -- "-dashfile"

Enter fullscreen mode Exit fullscreen mode

Will display the content of -dashfile

7. Cat command if the file has a lot of content and can’t fit in the terminal.

$cat "filename" | more
Enter fullscreen mode Exit fullscreen mode

8. Merge the contents of multiple files

$cat "filename1" "filename2" "filename3" > "merged_filename"

Enter fullscreen mode Exit fullscreen mode

Will merge the contents of file in respective order and will insert that content in "merged_filename".

9. Display the content of all html files in the folder.

$cat *.html
Enter fullscreen mode Exit fullscreen mode

Will show the content of all html files present in the folder.

10. Write in an already existing file

$cat >> geeks.txt
The newly added text.
Enter fullscreen mode Exit fullscreen mode

Will append the text "The newly added text." to the end of the file.

Discussion (0)