

We've created 3 files, then combined them into a single file using cat In the above example, we're creating 3 files using echo, combining the 3 files into one using cat, and then displaying the new combined file using cat. We can create a file containing the concatenation of multiple files like this: echo "File 1 Contents" > file1 In fact, we can use cat's concat and echo functionality to create files. In the previous examples, we've been using the echo command redirected to a file to create new files. This will result in the following output: Piping text into cat Creating a File You can also pipe text to cat, in which case that text is echoed. Each time I hit enter, the line was echoed. In the example here, I've typed a single word per line. If you run the cat command with no commands, cat will run in interactive mode and echo anything you type until you exit the command. This is a less common usage of cat, but is the basis for the next section. A complete breakdown can be found in the man pages. The output of two files concatenatedĬat also provides some switches to to do things such as show non-print characters (-v), or number your lines (-n). Notice however, that the cat command automatically inserts a line break between outputs. You can give cat many files and it will concatenate (combine) all of them. The output is the contents of the 1st file, followed by the contents of the 2nd file. If we run the following commands: echo "This is how we do it" > test1 #create 1st fileĮcho "*This is how we do it*" > test2 #create 2nd file If we use the command the same way, but give it two or more files, then it outputs the concatenation for the files. The previous example is actually a specific case of the cat command's main function, which is to concatenate files for display. The output is as follows: The output of the example commands (Con)cat We then use the cat command to display the contents. In this simple example, we're using a combination of echo and a redirect to create a file containing "Dance, Dance". echo "Dance, Dance" > cat_create #create a file The following is an example that you can try. The most common use of the cat command is to output the contents of a file. It can display a file, concatenate (combine) multiple files, echo text, and it can be used to create a new file.

There are 4 common usages of the cat command. The cat command is a very popular and versatile command in the 'nix ecosystem.
