TranslateProject/sources/tech/20230627.0 ⭐️⭐️ Using cat Command in Linux.md

9.1 KiB

Using cat Command in Linux

The cat command is used to print the file contents of text files.

At least, that's what most Linux users use it for and there is nothing wrong with it.

Cat actually stands for 'concatenate' and was created to merge text files. But withsingle argument, it prints the file contents. And for that reason, it is a go-to choice for users to read files in the terminal without any additional options.

Using the cat command in Linux

To use the cat command, you'd have to follow the given command syntax:

cat [options] Filename(s)

Here,

  • [options] are used to modify the default behavior of the cat command such as using the -n option to get numbers for each line.
  • Filename is where you'll enter the filename of the file that you want to work with.

To make things easy, I will be using a text file named Haruki.txt throughout this guide which contains the following text lines:

Hear the Wind Sing (1979)
Pinball, 1973 (1980)
A Wild Sheep Chase (1982)
Hard-Boiled Wonderland and the End of the World (1985)
Norwegian Wood (1987)
Dance Dance Dance (1990)
South of the Border, West of the Sun (1992)
The Wind-Up Bird Chronicle (1994)
Sputnik Sweetheart (1999)
Kafka on the Shore (2002)
After Dark (2004)
1Q84 (2009-2010)
Colorless Tsukuru Tazaki and His Years of Pilgrimage (2013)
Men Without Women (2014)
Killing Commendatore (2017)

So, what will be the output when used without any options? Well, let's have a look:

cat Haruki.txt

use cat command in Linux

As you can see, it printed the whole text file!

But you can do a lot more than just this. Let me show you some practical examples.

1. Create new files

Most Linux users use the touch command to create new files but the same can be done using the cat command too!

The cat command has one advantage over the touch command in this case, as you can add text to the file while creating. Sounds cool. Isn't it?

To do so, you'd have to use the cat command by appending the filename to the > as shown:

cat > Filename

For example, here, I created a file named NewFile.txt:

cat > NewFile.txt

Once you do that, there'll be a blinking cursor asking you to write something and finally, you can use Ctrl + d to save the changes.

If you wish to create an empty file, then just press the Ctrl + d without making any changes.

Using cat command

That's it! Now, you can use the ls command to show the contents of the current working directory:

use the ls command to list the contents of the current working directory

2. Copy the file contents to a different file

Think of a scenario where you want to redirect the file content of FileA to the FileB

Sure, you can copy and paste. But what if there are hundreds or thousands of lines?

Simple. You use the cat command with the redirection of data flow. To do so, you'd have to follow the given command syntax:

cat FileA > FileB

🚧 If you use the above syntax to redirect file contents, it will erase the file contents of the FileB and then will redirect the file contents of the FileA.

For example, I will be using two text files FileA and FileB which contains the following:

check the file contents using the cat command

And now, if I use the redirection from FileA to FileB, it will remove the data of FileB and then redirect the data of FileA:

cat FileA > FileB

redirect the file content using the cat command

Similarly, you can do the same with multiple files:

cat FileA FileB > FileC

redirect file content of multiple files using the cat command

As you can see, the above command removed the data of FileC and then redirected the data of FileA and FileB.

Append the content of one file to another

There are times when you want to append data to the existing data and in that case, you'll have to use the >> instead of single >:

cat FileA >> FileB

For example, here, I will be redirecting two files FileA and FileB to the FileC:

cat FileA.txt FileB.txt >> FileC.txt

redirect file content without overriding using the cat command

As you can see, it preserved the data of the FileC.txt and the data was appended at the end of it.

💡 You can use the>>to add new lines to an existing file. Usecat >> filenameand start adding the text you want and finally save the changes withCtrl+D.

4. Show the numbers of line

You may encounter such scenarios where you want to see the number of lines, and that can be achieved using the -n option:

cat -n File

For example, here, I used the -n option with the Haruki.txt:

get the number of the lines in the cat command

5. Remove the blank lines

Left multiple blank lines in your text document? The cat command will fix it for you!

To do so, all you have to do is use the -s flag.

But there's one downside of using the -s flag. You're still left with one blank space:

remove blank lines with the cat command

As you can see, it works but the results are close to the expectations.

So how would you remove all the empty lines? By piping it to the grep command:

cat File | grep -v '^$'

Here, the -v flag will filter out the results as per thespecified pattern and '^$' is a regular expression that matches the empty lines.

And here are the results when I used it over the Haruki.txt:

cat Haruki.txt | grep -v '^$'

remove all the blank lines in text files using the cat command piped with grep regular expression

Once you have the perfect output, you can redirect it to a file to save the output:

cat Haruki.txt | grep -v '^$' > File

save output of cat command by redirection

That's what you've learned so far

Here's a quick summary of what I explained in this tutorial:

Command Description
cat <Filename> Prints the file content to the terminal.
cat >File Create a new file.
cat FileA > FileB File contents of the FileB will be overridden by the FileA.
cat FileA >> FileB File contents of the FileA will be appended at the end of the FileB.
cat -n File Shows the number of lines while omitting the file contents of the File.
`cat File more`
`cat File less`
`cat File grep -v '^$'`

🏋️It's time to exercise

If you learned something new, executing it with different possibilities is the best way to remember.

And for that purpose, here are some simple exercises you can do with the cat command. They will be super basic as cat too isone of the most basic commands.

For practice purposes, you can use our text files from GitHub.

  • How would you create an empty file using the cat command?
  • Redirect output produced by the cat command to a new file IF.txt
  • Can you redirect three or more file inputs to one file? If yes, then how?

via: https://itsfoss.com/cat-command/

作者:Sagar Sharma 选题:lkxed 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出