Learn basic Linux commands Part 2

Now, let's learn more and more commands that will be useful when working.

String search grep

The grep command is one of the most frequently used commands. grep will display the line if the text file contains the specified string.

grep String TextFile

For example, let's say the text file (foo.txt) looks like the following.

cat is animal
dog is animal
mouse is animal
It's cat.

If you want to extract the line containing cat from this text file, write as follows.

grep cat foo.txt

The output result is as follows.

cat is animal
It's cat.

Combine with other commands

Grep is very convenient when combined with other commands. For example, let's combine it with the ls command.

ls | grep log

Ls is a command to display a list of files. And the following | is called a pipe, which allows you to pass the output of ls to the input of the next command.

By executing the above command, only the files containing the file name log can be extracted from the file list.

It can be combined with other commands as well as ls.

If you specify a character string that includes spaces, enclose the character string in double quotes.

grep "cat is" foo.txt

Use regular expressions

In grep, you can specify a regular expression as an extraction condition.

grep "^cat" foo.txt

On the contrary, when searching by character string, the "-F" option can be used.

grep -F "cat" foo.txt

Search from all subdirectories

Grep has a very useful function. It is a function that searches recursively including subdirectories. Use the r option.

grep -r cat dir1

The line containing cat is displayed from the text files contained under the directory dir1.

It is also possible to specify multiple directories as shown below.

grep -r cat dir1 dir2

View environment variables

Use env to see the environment variables.

env

Environment variables are variables for storing settings used in applications.

HOSTNAME=www345u.sakura.ne.jp
SHELL=/bin/bash
OLDPWD=/home/kimoto/labo
USER=kimoto

Environment variable settings env

To set environment variables, do the following.

export APP_NAME=myapp

Environment variables are generally uppercase. You can give it any name you like. The environment variables you set are valid only in the current shell. In other words, once you log out or quit the terminal, the settings will disappear.

If you want to set environment variables as the default settings, you need to define them in the shell configuration file. The shell config file is .bashrc in your home directory.

vi ~/.bashrc

Environment variable settings and commands can be described in this configuration file.

Check the command path which

Use which to find out the command path. For example, the command perl can tell you what you're actually doing.

which perl

Then, the path of the application that is actually running will be displayed as shown below.

~/perl5/perlbrew/perls/perl-5.14.2/bin/perl

Output the contents of the file

Use cat to output the contents of the file.

cat File

View text file from the beginning more

Use more if you want to see only the beginning of the text file.

more TextFile

Press Enter to advance the page. Press q when you want to finish.

See the end of the text file tail

Use tail to see the end of the text file.

tail TextFile

There is a convenient option in tail, which has a f option that monitors the file and keeps displaying it if a string is added to the end of the file. Useful for monitoring logs.

tail -f development.log

Associated Information