In Linux, nearly every object (usually a text) is a file, and for instance, all configuration files in Linux are text files. So to reconfigure an application, you simply open the configuration file, change the text, save the file, and then restart the application—your reconfiguration is complete. For illustrative purposes, we will use files from various programs within Linux.
Viewing Files
cat: Concatenating Files
The most basic text display command is “cat”, which is a short form of concatenate. We can use it to show the configuration file for FUSE, the Filesystem in Userspace (FUSE) application.
head: View the Beginning of a File
In order to view the beginning of a file, you can use the head command. The “head” command displays the first 10 lines of a file. In this next example, we use the “head” command on the oinkmaster.conf file, a script that will help you update and manage Snort rules.
tail: View the end of a file
nl: Inserting Line Numbers
grep: Filtering Text
sed:Find and Replace
The sed command lets you search for occurrences of a word or a text pat- tern and then perform some action on it. The name of the command is a contraction of stream editor, because it follows the same concept as a stream editor. In its most basic form, sed operates like the Find and Replace function in Windows. Let’s use it to search for the term “multiple” in the oinkmaster.conf file, replace it with the word “MULTIPLE” and save it as a new file.
As you can see, after running the sed command:
“sed s/multiple/MULTIPLE/g oinkmaster.conf >oinkmaster.conf.COPY”
The grep command does not find any instances of multiple in the oinkmaster.conf.COPY file, but it found multiple instances of “MULTIPLE” in the file.
Breaking Down the sed Command
The “s” command performs the search: you give the search term “multiple” and the term you want to replace it with “MULTIPLE”, separated by a slash (/). The “g” command tells Linux that you want the replacement performed globally. Then the result is then output to a new file, oinkmaster.conf.COPY
Now, when you use grep with oinkmaster.conf.COPY to search for “multiple”, you’ll see that no instances were found, but when you search for “MULTIPLE”, you find a number of occurrences.
Getting Super Specific with sed
Viewing Files with more and less
Controlling the Display with more
Displaying and Filtering with less
The “less” command is a more comprehensive command than “more” With less, you can:
- Scroll through a file; and
- Filter it for terms.
If you are looking for more advanced text analysis and manipulation techniques, check out Secur’s article on “How to Search and Analyze Text in Linux“.