The material, the translation of which we publish today, is intended for those who want to master the Linux command line. The ability to use this tool effectively can save a lot of time. In particular, we will talk about the Bash shell and 21 useful commands here. We'll also talk about how to use command flags and Bash aliases to speed up the typing of long instructions.
β
terms
As you learn to work with the Linux command line, you will encounter many concepts that are helpful to navigate. Some of them, like "Linux" and "Unix", or "shell" and "terminal", are sometimes confused. Let's talk about these and other important terms.
Now that we've covered the important terms, I want to point out that I'll use the terms "Bash", "shell" and "command line" interchangeably here, as well as the terms "directory" and "folder".
Standard stdin
), standard output (standard output, stdout
) and standard error output (standard error, stderr
).
If in the example commands that will be given below, you will find something like my_whatever
- this means that this fragment needs to be replaced with something of yours. For example, the name of a file.
Now, before proceeding with the analysis of the commands that this material is dedicated to, let's take a look at their list and their brief descriptions.
21 Bash commands
βGetting information
man
: Displays the user guide (help) for the command.pwd
: displays information about the working directory.ls
: displays the contents of a directory.ps
: Allows you to view information about running processes.
βFile system manipulation
cd
: change working directory.touch
: create a file.mkdir
: create a directory.cp
: Copy a file.mv
: Move or delete a file.ln
: create a link.
βI/O redirection and pipelines
<
: redirectstdin
.>
: redirectstdout
.|
: piped the output of one command to the input of another command.
βReading files
head
: read the beginning of the file.tail
: read end of file.cat
: Read a file and print its contents to the screen, or concatenate files.
βDeleting files, stopping processes
rm
: Delete a file.kill
: stop the process.
βSearch
grep
: search for information.ag
: advanced command for searching.
βArchiving
tar
: creating archives and working with them.
Let's talk about these commands in more detail.
Team Details
To begin with, let's deal with the commands, the results of which are issued in the form stdout
. Usually these results appear in a terminal window.
βGetting information
man command_name
: display the command guide, i.e. help 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 in the system he is.
ls
: display the contents of a directory. This command is also used quite often.
ls -a
: show hidden files. flag applied here -a
teams ls
. The use of flags helps to customize the behavior of the commands.
ls -l
: Display detailed information about files.
Note that flags can be combined. For example - like this: ls -al
.
ps
: View running processes.
ps -e
: Display information about all running processes, not just those associated with the current user shell. This command is often used in this form.
βFile system manipulation
cd my_directory
: change working directory to my_directory
. To move up one level in the directory tree, use my_directory
relative path ../
.
cd command
touch my_file
: file creation my_file
along the given path.
mkdir my_directory
: create a folder my_directory
along the given path.
mv my_file target_directory
: move file my_file
to folder target_directory
. When specifying the target directory, you need to use the absolute path to it (and not a construction like ../
).
The team mv
can also be used to rename files or folders. For example, it might look like this:
mv my_old_file_name.jpg my_new_file_name.jpg
: create a copy of a file
cp my_source_file target_directorymy_source_file
and put it in a folder target_directory
.
ln -s my_source_file my_target_file
: create a symbolic link my_target_file
per file my_source_file
. If you change the link, the original file will also change.
If file my_source_file
will be deleted, then my_target_file
will remain. Flag -s
teams ln
allows you to create links for directories.
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
) per file my_file
. This can be useful if the command is waiting for some input from the keyboard, and this data is already saved in a file.
my_command > my_file
: redirects the results of the command, i.e. what would normally go into stdout
and output to the screen, to a 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 >
use the construction >>
, then, provided that the file to which the output of the command is redirected exists, this file will not be overwritten. The data will be added to the end of this file.
Now let's take a look at data pipeline processing.
The output of one command is fed into the input of another command. It's like connecting one pipe to another
first_command | second_command
: conveyor symbol, |
, is used to send the output of one command to another command. What the command on the left side of the described structure sends to stdout
, Fall into stdin
command to the right of the pipeline symbol.
On Linux, data can be pipelined using just about any well-formed command. It is often said that everything in Linux is a pipeline.
You can chain multiple commands using the pipeline symbol. It looks like this:
first_command | second_command | third_command
A pipeline of several commands can be compared to a pipeline
Note that when the command to the left of the symbol |
, outputs something to stdout
, what she outputs is immediately available as stdin
second team. 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 read
Now let's talk about reading data from files and displaying them on the screen.
βReading files
head my_file
: reads lines from the beginning of a file and prints them to the screen. You can read not only the contents of the files, but also what the commands output in stdin
using this command as part of the pipeline.
tail my_file
: reads lines from the end of the file. This command can also be used in a pipeline.
Head (head) is in front, and tail (tail) is behind
If you are working with data using the pandas library, then the commands head
ΠΈ tail
should be familiar to you. If this is not the case, take a look at the above figure, and you will easily remember them.
Consider other ways to read files, let's talk about the command cat
.
Team cat
either prints the contents of a file to the screen, or concatenates multiple files. It depends on how many files are passed 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 it two files or more files, then it behaves differently.
cat my_file1.txt my_file2.txt
: having received several files as input, this command concatenates their contents and displays what happened in stdout
.
If the result of 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.
βDeleting files, stopping processes
rm my_file
: delete file my_file
.
rm -r my_folder
: deletes a folder my_folder
and all the files and folders it contains. Flag -r
indicates that the command will run in recursive mode.
To prevent the system from asking for confirmation each time a file or folder is deleted, use the flag -f
.
kill 012345
: Stops the specified running process, giving it time to gracefully shut down.
kill -9 012345
: Forcibly terminates the specified running process. View flag -s SIGKILL
means the same as the flag -9
.
βSearch
You can use different commands to search for data. In particular - grep
, ag
ΠΈ ack
. Let's start our acquaintance with these commands with grep
. This is a time-tested, reliable command, which, however, is slower than others and not as convenient to use as they are.
grep command
grep my_regex my_file
: searches my_regex
Π² my_file
. If a match is found, the entire string is returned, for each match. Default my_regex
treated as a regular expression.
grep -i my_regex my_file
: The search is performed in a case-insensitive manner.
grep -v my_regex my_file
: returns all rows that do not contain my_regex
. Flag -v
means inversion, it resembles the operator NOT
, found in many programming languages.
grep -c my_regex my_file
: Returns information about the number of matches found in the file for the search pattern.
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 team ag
. She came later grep
, it is faster, it is more convenient to work with it.
ag command
ag my_regex my_file
: returns information about line numbers, and the lines themselves, in which matches were found with my_regex
.
ag -i my_regex my_file
: The search is performed in a case-insensitive manner.
Team ag
automatically process the file .gitignore
and excludes from the output what is found in the folders or files listed in that file. It is very comfortable.
ag my_regex my_file -- skip-vcs-ignores
: contents of automatic version control files (like .gitignore
) is not taken into account in the search.
In addition, in order to tell the team ag
on which file paths you want to exclude from the search, you can create a file .agignore
.
At the beginning of this section, we mentioned the command ack
. Teams ack
ΠΈ ag
very similar, we can say that they are 99% interchangeable. However, the team ag
works faster, that's why I described it.
Now let's talk about working with archives.
βArchiving
tar my_source_directory
: concatenates files from a folder my_source_directory
into a single tarball file. Such files are useful for transferring large sets of files to someone.
tar command
The tarball files generated by this command are files with the extension .tar
(Tape ARchive). The fact that the word "tape" (tape) is hidden in the name of the command and in the extension of the names of the files it creates indicates how long this command has existed.
tar -cf my_file.tar my_source_directory
: creates a tarball file named my_file.tar
with folder contents my_source_directory
. Flag -c
stands for "create" (creation), and the flag -f
as "file" (file).
To extract files from .tar
-file, use the command tar
with flags -x
("extract", extraction) and -f
("file", file).
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 flag -z
("zip", compression algorithm) indicates that the algorithm should be used to compress files gzip
(GNUzip). File compression saves disk space when storing such files. If the files are planned, for example, to be transferred to other users, this contributes to faster download of such files.
Unzip file .tar.gz
you can add a flag -z
to the extract content command .tar
-files, which we discussed above. It looks like this:
tar -xfz my_file.tar.gz
It should be noted that the team tar
There are many more useful flags.
Bash aliases
Bash aliases (also called aliases or abbreviations) are designed to create abbreviated names of commands or their sequences, the use of which instead of regular commands speeds up work. If you have an alias bu
, which hides the command python setup.py sdist bdist_wheel
, then to call this command, it is enough to use this alias.
To create such an alias, just add the following command to the file ~/.bash_profile
:
alias bu="python setup.py sdist bdist_wheel"
If your system does not have the file ~/.bash_profile
, then you can create it yourself using the command touch
. After creating the alias, restart the terminal, after which you can use this alias. In this case, the input of two characters replaces the input of more than three dozen characters of the command, which is intended for
Π ~/.bash_profile
you can add aliases for any frequently used commands.
βResults
In this post, we've covered 21 popular Bash commands and talked about creating command aliases. If you are interested in this topic -
Dear Readers, What commands that are useful for beginners would you add to those that were discussed in this article?
β
Source: habr.com