- cat a file and check it in one line
- Linux Cat Command Examples
- How Does the Cat Command Work?
- Example 1: Display the Content of All Files
- Example 2: Display Multiple Files
- Example 3: Copy the Output of One File to Another
- Example 4: Append the Output of a File to Another File
- Copy Multiple Files to Another Text File/Concatenate the Files
- Example 5: Display the Line Numbers in File
- Example 6: Create a File
- Example 7: Sort the Output
- Example 8: Remove the Consecutive Empty Lines
- Example 9: Display the Tab Characters
- Example 10: Print the Output of a File
- Conclusion
- About the author
- Syed Minhal Abbas
- cat >> file in one line
- 3 Answers 3
cat a file and check it in one line
You could use the conditional expressions in bash to check the file exists ( -f flag) and not empty with its -s flag.
Also you could just use cp to create a new copy of the file instead of cat -ing it.
if [[ -f /etc/myfile.txt && -s /etc/myfile.txt ]]; then cp /etc/myfile.txt newfile.txt fi
Not sure why you would bother using a one-line instead of proper readable example. Anyway you can use the above logic as
[[ -f /etc/myfile.txt && -s /etc/myfile.txt ]] && cp /etc/myfile.txt newfile.txt
I am not sure why my comments were deleted but I still think -r might be useful. Or did you find something that would say otherwise?
Keeping it a little readable I would write something like test -f /etc/myfile.txt && test -s /etc/myfile.txt && cp /etc/myfile.txt newfile.txt when a oneliner is needed.
[ -s "/etc/myfile.txt" ] && cat /etc/myfile.txt > newfile.txt
You don’t have to cat the file, use cp instead.
Here, -s option checks if file is non-zero in size. From man test on a Linux box,
-s FILE FILE exists and has a size greater than zero
EDIT: Above solution is enough if /etc/myfile.txt is a regular file. If one wants to check if it is a regular file before checking the non-zero size, -f flag is needed
[[ -f "/etc/myfile.txt" && -s "/etc/myfile.txt" ]] && cp /etc/myfile.txt newfile.txt
Linux Cat Command Examples
In Linux, the “cat” is the concatenation of files which combines multiple files into a single file. There are other several uses of the cat command in Linux which we will talk in this article to give you an understanding of how it works in different scenarios.
- How Does the Cat Command Work?
- Display the Content of All Files
- Display Multiple Files at Once
- Copy the Output of One File to Another File
- Append the Output of a File to Another File
- Display the Line Numbers in the File
- Create New Files
- Sort the Output
- Remove the Consecutive Empty Lines
- Display the Tab Characters
- Print the Output of a File
Let’s start the article with the cat command.
How Does the Cat Command Work?
Using the “cat” command, you can create a file, view the file content, concatenate the files, and redirect the file output. The syntax of this command as follows:
Use the previous command if you are present in the same directory. Otherwise, mention the path to that file as follows:
Different options of the cat command are listed in the following:
Options | Description |
---|---|
-n | To display the line number of file content |
-T | To display the tab-separated characters in a line-e |
-e | To display the “$” at the end of lines |
-s | You can omit the empty lines from the output. |
-a | To display all the file content |
To explore more options, use the following “help” utility:
Example 1: Display the Content of All Files
The common usage of the cat command is displaying the file contents. To display the file contents to a Terminal, simply type “cat” and the filename as follows:
To display all the files in a current directory, use the wildcard character with the cat command as follows:
To display only the contents of the text files in a directory, enter the following command:
Example 2: Display Multiple Files
You can also combine and display the contents of multiple files together in the Terminal using the cat command. To display multiple files simultaneously, use the following syntax:
Example 3: Copy the Output of One File to Another
It can also be utilized to copy the output of one file to another file. If not found, it first creates it. Otherwise, it overwrites the targeted file.
To copy the output of a source file to another file, use the following syntax:
An example of this is to copy the output of a testfile1 to another file named testfile_backup as follows:
This command first creates the testfile_backup file and then copies the contents of testfile1 to it.
Example 4: Append the Output of a File to Another File
Instead of overwriting the output of a targeted file in the pervious example, you can also make the cat command to append the output:
It creates the destination file if it does not already exist. Otherwise, it appends the output.
Copy Multiple Files to Another Text File/Concatenate the Files
The cat command combines several files into a single file. The following syntax can be used to concatenate file1, file2, and file3 and save them to another file named file4.txt:
For instance, we want to concatenate the output of /etc/hostname, /etc/resolv.conf, and the /etc/hosts file to another file named network.txt:
Example 5: Display the Line Numbers in File
To display the line numbers to the output of a file, simply use the -n flag as follows:
For instance, if you are viewing a file which contains the list of items, you can use the -n flag to display those items with a number. Remember that empty lines are also numbered as follows:
If you do not want to number the empty lines, use the -b flag as follows:
Example 6: Create a File
To create a file through the cat command, the following syntax can be used for the purpose:
After entering the previous command, enter the text that you want to store in the file. Once done, save and exit. After that, you can view the contents of your newly created file by executing the following command in the terminal:
Example 7: Sort the Output
You can also combine the sort with the cat command to sort the output alphabetically as follows:
Example 8: Remove the Consecutive Empty Lines
Sometimes, the file contains consecutive empty lines that you do not want to print. The cat command allows merging those consecutive empty lines and shows them as one empty line:
Use the following command syntax to remove the repeated empty lines:
For instance, we have the following file with consecutive empty lines:
Example 9: Display the Tab Characters
Sometimes, you have to remove the tabs from your files. Cat command can help you to find the tabs on your file using the -t flag as follows:
Example 10: Print the Output of a File
Another popular use of the cat command is in the printing contents of a document. For instance, to print the output of a file to a printing device named /dev/lp, the following syntax is used:
Conclusion
In this article, we explained through various examples on how you can use the cat command to manipulate the files in Linux. The cat command is popular among all users because of its simple syntax and the various options that it provides. Creating and viewing a file, merging, copying, and appending the file contents, printing, and a lot more can be handled with this single cat command.
About the author
Syed Minhal Abbas
I hold a master’s degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.
cat >> file in one line
If it is only a small addition to the file, it would be good to save the extra keystrokes, pressing enter, ctrl-D How do you do this in one line, just adding a few characters to your text file and pressing only enter afterwards?
3 Answers 3
addnotes () < echo "$*" >> /path/to/notes.txt >
and then just add your notes as command line arguments:
$ addnotes better not forget this
Is interesting to see what power users do.. Can see if you add this to .bashrc it is always there to use
A simple bash script (or function) that handles either a single line or multiple lines:
note() < # If we have text on the line use it, otherwise read from stdin < [[ $# -gt 0 ]] && printf "%s\n" "$*" || cat; >| # Write current date/time, then append collected text indented by two spaces < date; sed 's/^/ /'; echo; >>> "$HOME/note.txt" >
This allows either a simple reminder
Or a more complex paragraph
note Remember this and this too oh and this
The reminders are written to the file $HOME/note.txt , with each one prefixed by the current date/time
cat ~/note.txt 10 Dec 2020 16:03:59 remember this 10 Dec 2020 16:04:05 Remember this and this too oh and this
If you don’t want the date and indentation, just remove that entire | < . >segment:
Or as a script (remember to make it executable and put it somewhere in your $PATH ):