Here will be presented a few tools and commands which help one to handle files and texts in Linux. All tools here were tested in Ubuntu 18.04 and 20.04. Please note that this is supposed to be a quick guide with hopefully some meaningful information for Linux new users.
Printing out a file
If you wish simply to visualize the content of a file,
theFile, for instance, simply run:
Reading big files
more command is used for reading big files. It does provide scroll down, but not scroll up functionality. It is really intended for reading a text from top to bottom.
In order to run it, showing shortcuts to the user, execute the line below for reading the file
more -d theFile
In a latter section, the command
less will be presented, which provide more functionalities, such as scroll up (using the arrow key) and text search.
Looking for things in files
If you wish to find a text, let's say,
myString in the system (in this case, really, all files),
grep is your friend, as shown below:
Now, maybe you wish to find the text
yourString in the files within the current directory
grep -nr 'yourString' .
nshow relative line number
.directory for search - in our case the current one
Continuing the text search within files: something a little more interesting is to find the text
some pattern in the directory
/path/to/search, however excluding the directory
grep -rn --exclude-dir=node_modules 'some pattern' /path/to/search
nshow relative line number
Interestingly enough, we can find the
tomcat processes in all running ones, by issuing the following command:
px aux | grep tomcat
ashow processes for all users
udisplay the process's user/owner
xalso show processes not attached to a terminal
Note what is happening here: the
ps command is executed, and with the
| command, the resulting stream is then processed by the command
grep tomcat, where the string
tomcat is searched.
Analyzing log files
Reading log files, is obviously quite a useful task. Indeed, doing so from the terminal two things are important:
- use a simple reader application where one can move through the text, and search for content;
- monitor log streams, for new things which are logged;
less is very lightweight, usually comes installed and is quite easy to use. To read, for example, the
And here are some useful shortcuts:
/– search for a pattern which will take you to the next occurrence.
n– for next match in forward
N– for previous match in backward
?– search for a pattern which will take you to the previous occurrence.
n– for next match in backward direction
N– for previous match in forward direction
G– go to the end of file
g– go to the start of file
For the other task, let's say we wish to watch the last 30 lines of the file
tail tool is used as follows:
tail -f -n 30 catalina.out
f- stands for
follow, which monitors the file stream displaying the changes
n- states how many lines (at the end) of the stream are to be displayed
The tools used here
Here you find small descriptions provided by other sites about the tools and commands used here.
cat is most commonly used to display the contents of one or multiple text files, combine files by appending the contents of one file to the end of another file, and create new files.
more command is used to view the text files in the command prompt, displaying one screen at a time in case the file is large (For example log files). The more command also allows the user do scroll up and down through the page. The syntax along with options and command is as follows. Another application of more is to use it with some other command after a pipe. When the output is large, we can use more command to see output one by one.
Grep is a command-line tool that allows you to find a string in a file or stream. It can be used with a regular expression to be more flexible at finding strings.
The command “ps” will show the processes status as snapshot. In contrast with Microsoft Windows which shows processes status in live view. In Linux, if we want a live view of the processes we need to use the command top which we won’t explain in this tutorial limited to ps.
Less is a command line utility that displays the contents of a file or a command output, one page at a time. It is similar to more, but has more advanced features and allows you to navigate both forward and backward through the file.
tail is the complementary of head command.The tail command, as the name implies, print the last N number of data of the given input. By default it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is precedes by its file name.
Here a few, yet powerful tools were shown. The idea was to present a quick and meaningful set of commands to help new Linux users handle search and file reading in the terminal.
- Unix Less Command: 10 Tips for Effective Navigation - thegeekstuff.com/2010/02/unix-less-command-..
- grep documentation - help.ubuntu.com/community/grep
- Tail command in Linux with examples - geeksforgeeks.org/tail-command-linux-examples
- Using ps command on linux - linuxhint.com/ps_command_linux-2
- more command in Linux with Examples - geeksforgeeks.org/more-command-in-linux-wit..
- Cat Command in Linux - linuxize.com/post/linux-cat-command/#:~:tex...