How to concatenate files in linux
How to concatenate files in linux
cat – concatenate files and print on the standard output
When using or administrating linux system you are usually confronted with a command line. There are no graphical aids such as mouse or windows to help you navigate through directories or edit files. Linux system from administration point of view consists of multiple configuration files so called config files. By editing or creating these files an administrator is changing the behaviour of various services available within the system. Another situation where you can encounter text files are log files. Log files are produced by services running on the system. Information stored in the log file is there for the administrator as a help to troubleshoot and optimize running services.
Whether we are talking about linux log files or config files we are, even so, talking about simple ASCII text files. Therefore, the skills to be able to read a content of such text files is imperative.
2.1. Reading files
Fortunately there is a cat command which assists a user to read a content of a given file or concatenate several files and display their content to the screen. Let’s use ls command to see what files are located in our home directory:
linuxconfig.org:~$ ls file1 file2 linuxconfig.org:~$
ls command revealed that there are currently two files located inside our home directory. To view the content of these files a cat command can be used. Typing a cat command accompanied by a file name as its argument will show a contend if this file on the screen.
linuxconfig.org:~$ cat file1 b line d line e line linuxconfig.org:~$
file1 contains 3 lines. Same as for a file1 we can use cat command to see a content of file2:
linuxconfig.org:~$ cat file2 a line c line linuxconfig.org:~$
From these two simple cat command examples, we can learn two things:
- providing a file name to a cat command as an argument instructs a cat command to read the content of any given text file
- the content of a file is displayed on the screen. Screen in this case is very often also referred to as standard output ( STDOUT)
It is important to understand that cat command accepts multiple arguments. All arguments must be separated by a space delimiter. Hence, to see a content of file1 and file2 using a single command both file names can be passed to a cat command:
linuxconfig.org:~$ cat file1 file2 b line d line e line a line c line linuxconfig.org:~$
This is called concatenation, which means that a content of both files is concatenated into a single output. Concatenation can also extend its usage with redirection. Suppose, that we want to create a new file file3 which will contain a content of both files, file1 and file2. In this situation, we use the cat command to concatenate content of both files and then use the “>” operator to redirect standard output to a file called file3.
linuxconfig.org:~$ cat file1 file2 > file3 linuxconfig.org:~$
After execution of this command, no output is presented on the screen. This is perfectly normal considering that we have used > operator to redirect an output from the cat command to yet another file called file3. When we now try to read a content of the file3 with the cat command the output produced should contain a content of file1 and file2:
linuxconfig.org:~$ ls file1 file2 file3 linuxconfig.org:~$ cat file3 b line d line e line a line c line linuxconfig.org:~$
Please not that it is not important to create a file3 prior to redirecting an output into it. If the file3 would exist it will be overwritten and if the file does not exist it will be created. Another approach where a concatenated output from a cat command can be used is with a combination of “ | “ ( pipe ) operator. Pipe operator can be used to redirect output of one command as an input to other commands. In the next example, we are going to use a sort command which will alphabetically sort any data.
linuxconfig.org:~$ cat file1 file2 | sort a line b line c line d line e line linuxconfig.org:~$
What took place here is a simple example of how a output of one command can be redirected as an input to other command with | operator for further processing. Cat command produced a concatenated output from file1 and file2, | operator redirected this output to a sort command which sorted all lines into an alphabetical order. As we already had file3, which contained all lines of file1 and file2, we could simply redirect a content of file3 to a sort command like this:
linuxconfig.org:~$ cat file3 | sort a line b line c line d line e line linuxconfig.org:~
This produced precisely the same output as in our previous example. However, this is a useless use of cat command which unnecessary drains a computer processing power. sort command itself can accept files as arguments, and consequently, we could just pass a file3 as an argument directly to a sort command:
linuxconfig.org:~$ sort file3 a line b line c line d line e line linuxconfig.org:~$
2.3. Creating files
When the cat command does not obtain any arguments, it waits for an input from a keyboard. If you try to run cat command lacking any arguments, cat will wait for your input from a keyboard until it receives an end-of-file ( EOF ) signal produced by CTRL+D key combination. When entering some input from a keyboard cat command will simply repeat any input and displays on the screen.
linuxconfig.org:~$ cat keyboard input keyboard input linuxconfig.org:~$
This keyboard input can be redirected directly into a file with > operator. The following example illustrates this idea:
linuxconfig.org:~$ cat > file4 f line linuxconfig.org:~$ cat file4 f line linuxconfig.org:~$
After entering a “f line” line ENTER kye was pressed to produce new line character followed by CTRL+D, which produces an end-of-file signal. Reading a content of file4 with the cat command confirms that an input from a keyboard was undeniably redirected into a file4.
To append a new line into file4 file with > redirection would not work. As it was already mentioned above, a > operator will overwrite a content of file4. To append a new line a >> append operator needs to be used instead. Consider a following example:
linuxconfig.org:~$ cat file4 f line linuxconfig.org:~$ cat >> file4 g line linuxconfig.org:~$ cat file4 f line g line linuxconfig.org:~$
The procedure of inserting a new line with >> append operator is exactly the same is it was in the previous example. The only distinction is that >> will add a new line after existing EOF character.
cat [OPTION] [FILE]...
5. Frequently used options
-n, --number number all output lines
Source taken from http://linuxconfig.org/