Writing to Files Using cat Command on Linux
cat command is a Unix tool used for manipulating and displaying file contents. The command gets its name from the word "concatenate" because it has, among other things, the ability to concatenate files.
In this article, we'll go through a few easy ways to use this command to write text to a file with examples. Using
cat is very straightforward, so no prior programming or Unix experience is needed to follow along.
cat Command Basics
Starting, we'll just sum up the basics of the
cat command to help you out if you've never used it before or need a brief overview.
The syntax looks like this:
cat [OPTION]... [FILE]...
To quickly look up the syntax or command options, run cat with the help option:
$ cat --help
Or, you can use the manual pages:
$ man cat
These commands should display the following list of options:
-A, --show-all equivalent to -vET -b, --number-nonblank number nonempty output lines, overrides -n -e equivalent to -vE -E, --show-ends display $ 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 -u (ignored) -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB --help display this help and exit --version output version information and exit
Displaying File Contents on the Standard Output
To print the contents of a file to the standard output just name the file you want to display:
$ cat filename1
If the file is in a different directory, you'll have to navigate to it:
$ cat /dir1/dir2/filename1
We'll be expecting to see the contents of this file, printed to the standard output, in this case - the terminal:
Content of filename1!
This is the most common use of the cat command since it makes it easy to peek into the contents of a file without opening a text editor.
Writing Text to a File Using cat
To redirect the output of the
cat command from the standard output to a file, we can use the output redirection operator
$ cat filename1 > filename2
This will overwrite the contents of
filename2 with the contents of
filename1, so make sure that
filename2 doesn't contain anything you would mind losing. Now,
Content of filename1!
The output redirection operator will redirect the output of any command we call. For example, let's try it out with the
pwd command, which prints the name of the current working directory:
$ pwd > testfile
If we take a look at the
$ cat testfile
It contains the current working directory's path:
If the file you are redirecting to doesn't exist, a file by that name will be created:
$ cat filename1 > newfilename
Concatenating Files with cat
Concatenating multiple files using
cat is simple - just list the files in the desired order:
$ cat filename1 filename2 > outputfile $ cat outputfile
This takes the
filename2 files, concatenates them, and outputs the into a new
Content of filename1! Content of filename2!
Standard Input Between Files
When the name of the input file isn't listed,
cat starts reading from the standard input until it reaches
EOF (end-of-file). The end-of-file signal is sent by the
ctrl+d command line shortcut:
$ cat > outputfile Hello World $ cat outputfile
This would output:
We can even add text from the standard input in between files we wish to concatenate by using
- to indicate where we expect standard input. If we have files such as
filename3, and we want some text from the standard input in between
filename2, we would write:
$ cat filename1 - filename2 filename3 > output Text from standard input! $ cat output
output, we'll see something along the lines of:
Content of filename1! Text from standard input! Content of filename2! Content of filename3!
Appending Files with cat
In the previous examples, using the redirection operator discarded the previous contents of the
output file. What if we want to append the new content to the old content? To append files we use the
$ cat filename1 filename2 >> output $ cat output
And that should result in:
Original output file contents. Content of filename1! Content of filename2!
Concatenating Contents of all Files Directory with cat
To concatenate all contents of all files in a directory, we use the wildcard
$ cat /dir1/dir2/* > output
To concatenate all contents of all files in the current working directory, we'd use:
$ cat * > output
* can also be used to concatenate all files with the same extension:
$ cat *.txt > output
Enumerating Line Numbers
Enumeration of all lines of output is done with the
$ cat -n filename1 filename2 filename3 > output $ cat output
Which would write something along the lines of:
1 Content of filename1! 2 Content of filename2! 3 Content of filename3!
Write $ at the End of Every Line
-E option marks the end of every line in the file with the
$ cat -E filename1 filename2 filename3 > output $ cat output
Sorting Lines of Concatenated Files by Piping
This one is a bit of a cheat. The
cat command can't sort, but we can use piping to achieve that. The pipe command (
|) is used to turn the output of one command into the input of another. To sort the lines of a file, we'll use both
cat and another command, called
$ cat filename2 filename3 filename1 | sort > output $ cat output
This results in:
Content of filename1! Content of filename2! Content of filename3!
Cat is a simple, yet powerful Unix tool that comes pre-installed on most systems. It can be used on its own or in combination with other commands using pipes. Originally made by Ken Thompson and Dennis Ritchie in 1971,
cat's easy to use and intuitive functionalities stand the test of time.
In this article, we've explored some of the possibilities of using the
cat command to write text to files, check contents, concatenate and append files as well as enumerate lines and sort them.