Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives its content as output. It helps us to create, view, and concatenate files. So let us see some frequently used cat commands.
1) To view a single file
Example: If our file_name = jayesh.txt
Note: `ls` command is used to display all files and directories in the current location.
2) To view multiple files
cat file_name1 file_name2
Example: If we have two files , file1 and file2.
cat file1 file2
cat file1 file2
3) To view contents of a file preceding with line numbers.
cat -n file_name
Example: If our file_name is file2.
cat -n file2
cat -n file2
4) Create a file and add content
cat > newfile_name
Example: If we want to create a newfile_name = jayesh1.
cat > jayesh1
`ls` command is used to display all files and directories in the current location.
5) Copy the contents of one file to another file.
$cat [filename-whose-contents-is-to-be-copied] > [destination-filename]
The content will be copied in destination file
6) Cat command can suppress repeated empty lines in output
cat -s file_name
Will suppress repeated empty lines in output
7) Cat command can append the contents of one file to the end of another file.
cat file_name1 >> file_name2
cat file1 >> file2
8) Cat command can display content in reverse order using tac command.
9) Cat command can highlight the end of line.
cat -E "filename"
Will highlight the end of line
10) If you want to use the -v, -E and -T option together, then instead of writing -vET in the command, you can just use the -A command line option.
cat -A "filename"
11) Cat command to open dashed files.
cat -- "-dashfile"
Will display the content of -dashfile
12) Cat command if the file has a lot of content and can’t fit in the terminal.
cat "filename" | more
Will show that much content, which could fit in terminal and will ask to show more.
13) Cat command to merge the contents of multiple files.
cat "filename1" "filename2" "filename3" > "merged_filename"
Will merge the contents of file in respective order and will insert that content in "merged_filename".
14) Cat command to display the content of all text files in the folder.
Will show the content of all text files present in the folder.
15) Cat command to write in an already existing file.
cat >> geeks.txt
The newly added text.
Will append the text "The newly added text." to the end of the file.
In this article we have discussed the `cat` command in Linux which is a versatile tool used for various file-related operations. We also discussed that it allows users to view, concatenate, create, copy, merge, and manipulate file contents. It is commonly used to display the content of a single file, multiple files, or add content to an existing file. Overall, we can say that `cat` command is an essential utility for managing and manipulating files in Linux.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!