yet another dev blog

yet another dev blog

Useful Linux commands to move around files

Useful Linux commands to move around files

Featured on Hashnode

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:

cat theFile

Reading big files

The 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 theFile.

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:

grep myString

Now, maybe you wish to find the text yourString in the files within the current directory

grep -nr 'yourString' .

where

  • r recursive
  • n show 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 node_modules within /path/to/search.

grep -rn --exclude-dir=node_modules 'some pattern' /path/to/search

where

  • r recursive
  • n show relative line number

Interestingly enough, we can find the tomcat processes in all running ones, by issuing the following command:

px aux | grep tomcat
  • a show processes for all users
  • u display the process's user/owner
  • x also 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;

The editor less is very lightweight, usually comes installed and is quite easy to use. To read, for example, the logfile.log execute:

less logfile.log

And here are some useful shortcuts:

forward search

  • / – search for a pattern which will take you to the next occurrence.
  • n – for next match in forward
  • N – for previous match in backward

backwards search

  • ? – 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

navigation

  • 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 catalina.out. The tail tool is used as follows:

tail -f -n 30  catalina.out

where

  • 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

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

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

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.

ps

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

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

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.

Final words

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.

References

#linux#linux-for-beginners#terminal#command-line
 
Share this