Linux cat command

Linux cat command

 Linux cat command


The Linux cat command is used to display file content. You can also use this to create new files or copy file contents to other files.

Syntax:

$ cat file_name

Examples:

#1. Display File Content

For example, use the following command to list the content of /etc/passwd file.

$ cat /etc/passwd

Output:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin

#2. Display Multiple File Content

You can also display the multiple file content in a single command.

$ cat /etc/passwd /etc/group

#3. Display Line Numbers

Use -n switch to display the line numbers with results of cat command.

$ cat -n /etc/passwd

#4. Create New File

You can also create files with the cat command. The type command line below (change filename as per yours)

$ cat > myfile

Write your content here
and here
[CTRL+C] to exit

#5. Redirect file content to other files

You can use the redirect operator with the cat command to copy file content to others.

$ cat sourcefile > otherfile

You can also copy multiple file content to other files.

$ cat sourcefile1 sourcefile2 sourcefile3 > otherfile
Reactions

Post a Comment

0 Comments

close