📜 ⬆️ ⬇️

[bookmarks] Bash for beginners: 21 useful commands

The material, the translation of which we publish today, is intended for those who want to master the Linux command line. The ability to effectively use this tool saves a lot of time. In particular, it will focus on the command shell Bash and 21 useful team. We will also talk about how to use the command flags and aliases for Bash, which allow you to speed up the entry of long instructions.



→ Also read the cycle of publications about bash scripts in our blog.

Terms


In the course of mastering work on the Linux command line, you may encounter many concepts in which it will be useful to navigate. Some of them, like “Linux” and “Unix”, or “command shell” and “terminal”, are sometimes confused. Let's talk about these and other important terms.
')
Unix is a popular operating system that was developed by Bell Labs in the 1970s. Her code was closed.

Linux is the most popular Unix-like operating system. It is now used on a variety of devices, including - on computers.

A terminal (terminal) or terminal emulator is a program that gives access to the operating system. You can open several terminal windows at the same time.

A shell is a program that allows you to send commands to an operating system written in a special language.

Bash stands for Bourne Again SHell. This is the most common command shell language used to interact with the operating system. In addition, the default Bash shell is used in macOS.

The command line interface (Command Line Interface, CLI) is a method of interaction between a person and a computer, using which the user enters commands from the keyboard, and the computer, executing these commands, displays, in text form, messages for the user. CLI is mainly used to obtain up-to-date information about certain entities, for example, about files, and for working with files. The command line interface should be distinguished from the graphical user interface (Graphical User Interface, GUI), which mainly uses the mouse. The command line interface is often simply called the command line.

A script is a small program that contains a sequence of shell commands. Scripts are written to files, they can be used many times. When writing scripts, you can use variables, conditional constructions, cycles, functions, and other features.

Now that we have considered important terms, I want to note that here I will use the terms "Bash", "shell" and "command line" interchangeably, as well as the concepts of "directory" (directory) and "folder" (folder).

Standard streams that we will use here are standard input (standard input, stdin ), standard output (standard output, stdout ) and standard error output (standard error, stderr ).

If you see something like my_whatever in the example commands below, this means that this snippet needs to be replaced with something yours. For example - the file name.

Now, before proceeding with the analysis of the commands to which this material is dedicated, let's take a look at their list and their brief descriptions.

21 bash team


â–ŤReceiving information



File system manipulations



I / O redirection and pipelines



â–ŤRead files



â–ŤDelete files, stop processes



â–ŤSearch



â–ŤArchive



Let's talk about these commands in more detail.

Details about the teams


To begin, let's deal with the commands, the results of which are issued in the form of stdout . Usually these results appear in a terminal window.

â–ŤReceiving information


man command_name : display a manual for a command, that is, reference information.

pwd : display the path to the current working directory. In the course of working with the command line, the user often needs to find out exactly where he is in the system.

ls : output directory contents. This command is also used quite often.

ls -a : show hidden files. The ls -a flag is used here. Using flags helps customize the behavior of commands.

ls -l : display detailed information about files.

Please note that the flags can be combined. For example - like this: ls -al .

ps : view running processes.

ps -e : displays information about all running processes, and not just those related to the current user shell. This command is often used in this form.

File system manipulations


cd my_directory : change working directory to my_directory . To go one level higher in the directory tree, use the relative path ../ as my_directory .


Cd command

touch my_file : create a file my_file in a specified path.

mkdir my_directory : create a folder my_directory in the specified path.

mv my_file target_directory : moves the file my_file to the target_directory folder. When specifying the target directory, you must use the absolute path to it (and not a structure like ../ ).

The mv command can also be used to rename files or folders. For example, it may look like this:

mv my_old_file_name.jpg my_new_file_name.jpg
cp my_source_file target_directory
mv my_old_file_name.jpg my_new_file_name.jpg
cp my_source_file target_directory
: create a copy of the my_source_file file and place it in the target_directory folder.

ln -s my_source_file my_target_file : create a symbolic link my_target_file to the file my_source_file . If you change the link, the source file will also change.

If the file my_source_file is deleted, then my_target_file will remain. The -s flag of the ln command allows you to create links for directories as well.

Now let's talk about I / O redirection and pipelines.

I / O redirection and pipelines


my_command < my_file : replaces the standard input file descriptor ( stdin ) with the file my_file . This can be useful if the command is waiting for some data to be entered from the keyboard, and this data is stored in the file in advance.

my_command > my_file : redirects the results of the command, that is, what usually gets into stdout and is displayed on the screen, in the file my_file . If the file my_file does not exist, it is created. If the file exists, it is overwritten.

For example, after executing the command ls > my_folder_contents.txt , a text file will be created containing a list of what is in the current working directory.

If instead of the > symbol we use the construction >> , then, provided that there exists a file to which the output of the command is redirected, this file will not be overwritten. Data will be added to the end of this file.

Now let's take a look at pipelining.


The fact that one command displays, is fed to the input of another command. It’s like connecting one pipe to another.

first_command | second_command first_command | second_command : pipeline symbol, | , is used to send the results of one team to another team. The fact that the command in the left part of the construction described sends to stdout gets into the command stdin , which is located to the right of the conveyor symbol.

In Linux, pipelining of data can be organized using almost any correctly composed command. It is often said that everything in Linux is a pipeline.

With the help of the pipeline symbol, several commands can be chained together. It looks like this:

 first_command | second_command | third_command 


A pipeline of several teams can be compared to a pipeline.

Note that when the command to the left of the symbol | , prints something to stdout , that which it output is immediately available as stdin second command. That is, it turns out that using the pipeline, we are dealing with parallel execution of commands. Sometimes this can lead to unexpected results. Details about this can be found here .

Now let's talk about reading data from files and about displaying them on the screen.

â–ŤRead files


head my_file : reads lines from the beginning of the file and displays them on the screen. You can read not only the contents of the files, but also the fact that the commands are output to stdin , using this command as an element of the pipeline.

tail my_file : reads lines from the end of the file. This command can also be used in the pipeline.


Head (head) is in front and tail (tail) - behind

If you work with data using the pandas library, then the head and tail commands should be familiar to you. If this is not the case, take a look at the picture above and you can easily remember them.

Consider other ways to read files, let's talk about the cat .

The cat either prints the contents of the file to the screen, or it concatenates several files. It depends on how many files are transferred to this command when called.


Cat command

cat my_one_file.txt : when a single file is passed to this command, it outputs it to stdout .

If you give her two files or more files, then she behaves differently.

cat my_file1.txt my_file2.txt : receiving several files as input, this command concatenates their contents and prints what happened in stdout .

If the result of the file concatenation needs to be saved as a new file, you can use the > operator:

 cat my_file1.txt my_file2.txt > my_new_file.txt 

Now let's talk about how to delete files and stop processes.

â–ŤDelete files, stop processes


rm my_file : deletes the file my_file .

rm -r my_folder : deletes the my_folder folder and all the files and folders it contains. The -r flag indicates that the command will work in recursive mode.

To ensure that the system does not request confirmation when performing each operation to delete a file or folder, use the -f flag.

kill 012345 : stops the specified running process, giving it time to kill 012345 correctly.

kill -9 012345 : forcibly terminates the specified running process. The -s SIGKILL flag is the same as the -9 flag.

â–ŤSearch


You can use different commands to search for data. In particular - grep , ag and ack . Let's begin our acquaintance with these teams with grep . This is a time-tested, reliable team, which, however, is slower than the others and not the way they are, convenient to use.


Grep command

grep my_regex my_file : performs a search for my_regex in my_file . When a match is found, for each one of them, the entire string is returned. By default, my_regex treated as a regular expression.

grep -i my_regex my_file : the search is case-insensitive.

grep -v my_regex my_file : returns all lines that do not contain my_regex . The -v flag means inversion, it resembles the NOT operator available in many programming languages.

grep -c my_regex my_file : returns information about the number of matches with the search pattern found in the file.

grep -R my_regex my_folder : performs a recursive search in all files located in the specified folder and in the folders nested in it.

Now let's talk about the ag team. She appeared later grep , it is faster, it is more convenient to work with it.


Ag team

ag my_regex my_file : returns information about line numbers, and the lines themselves, in which matches are found with my_regex .

ag -i my_regex my_file : search is case insensitive.

The ag command automatically processes the .gitignore file and excludes from the output what is found in the folders or files listed in this file. It is very convenient.

ag my_regex my_file -- skip-vcs-ignores : the contents of files of automatic version control systems (like .gitignore ) are not taken into account during the search.

In addition, in order to tell the ag command which file paths to exclude from the search, you can create an .agignore file.

At the beginning of this section, we mentioned the ack command. The ack and ag commands are very similar, one can say that they are 99% interchangeable. However, the ag command is faster, so I just described it.

Now let's talk about working with archives.

â–ŤArchive


tar my_source_directory : merges files from the my_source_directory folder into a single tarball file. Such files are conveniently used to transfer large sets of files to someone.


Tar command

The tarball files created by this command are .tar files (Tape ARchive). The fact that the word “tape” is hidden in the name of the command and in the extension of the names of the files created by it indicates how long this command has existed.

tar -cf my_file.tar my_source_directory : creates a tarball file called my_file.tar with the contents of the folder my_source_directory . The -c flag stands for “create” (creation), and the -f flag as “file” (file).

To extract files in a .tar file, use the tar command with the -x (“extract”, extract) and -f (“file”, file) flags.

tar -xf my_file.tar : extracts files from my_file.tar to the current working directory.

Now let's talk about how to compress and decompress .tar files.

tar -cfz my_file.tar.gz my_source_directory : here, using the -z flag ("zip", compression algorithm), it is indicated that the gzip algorithm (GNU zip) should be used to compress files. Compressing files saves disk space when storing such files. If the files are planned, for example, to transfer to other users, this contributes to a faster download of such files.

You can unpack the .tar.gz file by adding the -z flag to the command for extracting the contents of .tar files, which we discussed above. It looks like this:

tar -xfz my_file.tar.gz
It should be noted that the tar command has many more useful flags.

Bash aliases


Bash pseudonyms (also called aliases or abbreviations) are designed to create abbreviated command names or their sequences, the use of which, instead of the usual commands, speeds up work. If you suppose you have an alias bu , behind which is the python setup.py sdist bdist_wheel , then to call this command, it is enough to use this alias.

To create such an alias, it is enough to add the following command to the ~/.bash_profile file:

 alias bu="python setup.py sdist bdist_wheel" 

If there is no ~/.bash_profile file on your system, you can create it yourself using the touch command. After creating the nickname, restart the terminal, after which you can use this nickname. In this case, the input of two characters replaces the input of more than three dozen characters of the command that is intended to build Python packages.

You can add aliases to ~/.bash_profile for any frequently used commands.

Results


In this article, we looked at 21 popular Bash teams and talked about creating aliases for teams. If you are interested in this topic - here is the cycle of publications dedicated to Bash. Here you can find the pdf-version of these publications. In addition, if you want to learn Bash, remember that here, as in the study of any other software system, practice is important.

Dear readers! What commands that would be helpful for beginners would you add to those that were covered in this article?

→ Also read the cycle of publications about bash scripts in our blog.

Source: https://habr.com/ru/post/445270/


All Articles