[in bookmarks] Bash for Beginners: 21 Useful Commands

The material we are publishing today is intended for those who want to master the Linux command line. Effectively utilizing this tool can save you a significant amount of time. Specifically, we will talk about the Bash shell and 21 useful commands. We will also discuss how to use command flags and Bash aliases, which can speed up the input of long instructions.

[in bookmarks] Bash for Beginners: 21 Useful Commands

Also, read our blog series on bash scripts.

Terms

In the process of mastering the Linux command line, you may encounter many concepts that will be useful to understand. Some of them, like "Linux" and "Unix", or "shell" and "terminal", are sometimes confused. Let's discuss these and other important terms.

Unix is a popular operating system that was developed by Bell Labs in the 1970s. Its code was proprietary.

Linux is the most popular Unix-like operating system today. It is used on many devices, including computers.

Terminal (terminal), or terminal emulator, is a program that provides access to the operating system. Multiple terminal windows can be opened simultaneously.

Shell (shell) is a program that allows you to send commands to the operating system written in a specific language.

Bash stands for Bourne Again SHell. It is the most widely used command line language for interacting with the operating system. Additionally, the Bash shell is the default in macOS.

Command Line Interface (Command Line Interface, CLI) is a way for a person to interact with a computer, where the user inputs commands via the keyboard, and the computer executes these commands, outputting messages in text form for the user. CLI is mainly used to obtain fresh information about certain entities, such as files, and to work with files. The command line interface should be distinguished from the graphical user interface (Graphical User Interface, GUI), which primarily utilizes the mouse. The command line interface is often simply referred to as the command line.

Script (script) is a small program that contains a sequence of shell commands. Scripts are written to files and can be reused multiple times. When writing scripts, you can use variables, conditional structures, loops, functions, and other capabilities.

Now that we have discussed 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" and "folder."

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

If you see something like my_whatever in the command examples provided below, it means that this segment should be replaced with something of your own. For example — a file name.

Now, before we start analyzing 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 manipulations

  • cd: changes the working directory.
  • touch: creates a file.
  • mkdir: creates a directory.
  • cp: copies a file.
  • mv: moves or deletes a file.
  • ln: creates a link.

▍Input-output redirection and pipelines

  • <: redirection stdin.
  • >: redirection stdout.
  • |: redirects using the output pipeline of one command to the input of another command.

▍Reading files

  • head: reads the beginning of a file.
  • tail: reads the end of a file.
  • cat: reads a file and outputs its contents to the screen or concatenates files.

▍Deleting files, stopping processes

  • rm: deletes a file.
  • kill: stops a process.

▍Searching

  • grep: searches for information.
  • ag: advanced command for searching.

▍Archiving

  • tar: creates archives and works with them.

Let's talk about these commands in more detail.

Details about the commands

First, let's understand the commands whose output is presented in the form of stdout. Usually, these results appear in the terminal window.

▍Getting information

man command_name: displays the manual for the command, that is — reference information.

pwd: displays the path to the current working directory. When working with the command line, users often need to know exactly where in the system they are.

ls: displays the contents of a directory. This command is also used quite frequently.

ls -a: displays hidden files. The flag is applied here. -a commands ls. Using flags helps to customize the behavior of commands.

ls -l: displays detailed information about files.

Note that flags can be combined. For example, like this: ls -al.

ps: viewing running processes.

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

▍File system manipulations

cd my_directory: changes the working directory to my_directory. To go up one level in the directory tree, use the my_directory relative path. ../.

[in bookmarks] Bash for Beginners: 21 Useful Commands
The cd command

touch my_file: creates a file my_file at the specified path.

mkdir my_directory: creates a folder my_directory at the specified path.

mv my_file target_directory: moves the file my_file in the folder target_directory. When specifying the target directory, you should use the absolute path to it (not something like ../).

The command mv, additionally, it can 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
cp my_source_file target_directory
: creates a copy of the file my_source_file and places it in the folder target_directory.

ln -s my_source_file my_target_file: creates a symbolic link my_target_file to the file my_source_file. If you change the link, the original file will change as well.

If the file my_source_file is deleted, then my_target_file will remain. The flag -s commands ln allows creating links for directories as well.

Now let's talk about input-output redirection and pipes.

▍Input-output 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 expects some input from the keyboard, and this input has been previously saved in a file.

my_command > my_file: redirects the results of the command, that is, what normally goes to stdout and displays on the screen, into the file my_file. If the file my_file does not exist, it will be created. If the file exists, it will be 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 > you use the construct >>, provided that a file exists to which the command's output is redirected, this file will not be overwritten. The data will be appended to the end of this file.

Now let's look at data pipeline processing.

[in bookmarks] Bash for Beginners: 21 Useful Commands
What the output of one command generates is passed as input to another command. This is akin to connecting one pipe to another.

first_command | second_command: the pipe symbol, |, is used to send the results of one command to another command. What the command on the left side of the described construct sends to stdout, is received by stdin the command that is on the right side of the pipe symbol.

In Linux, data pipeline processing can be organized using virtually any properly formed command. It is often said that everything in Linux is a pipeline.

Using the pipe symbol allows you to chain multiple commands together. It looks like this:

first_command | second_command | third_command

[in bookmarks] Bash for Beginners: 21 Useful Commands
A pipeline of several commands can be compared to a pipeline.

Note that when a command on the left side of the symbol |, outputs something to stdout, what it outputs immediately becomes available to stdin the second command. This means that by using a pipeline, we are dealing with the parallel execution of commands. Sometimes this can lead to unexpected results. You can read more about this here.

Now let's talk about reading data from files and outputting it to the screen.

▍Reading files

head my_file: reads lines from the beginning of the file and outputs them to the screen. You can read not only the contents of files but also what commands 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 a pipeline.

[in bookmarks] Bash for Beginners: 21 Useful Commands
Head is at the front, and tail is at the back.

If you're working with data using the pandas library, then the commands head and tail should be familiar to you. If not, take a look at the diagram above, and you'll easily memorize them.

Let's consider other ways to read files and talk about the command cat.

The command cat either outputs 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.

[in bookmarks] Bash for Beginners: 21 Useful Commands
The cat command

cat my_one_file.txt: when this command is given one file, it outputs it. stdout.

If you pass it two files or more, it behaves differently.

cat my_file1.txt my_file2.txt: when receiving multiple files as input, this command concatenates their contents and outputs the result in stdout.

If you need to save the concatenated result 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 deleting files and stopping processes.

▍Deleting files, stopping processes

rm my_file: deletes the file my_file.

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

To prevent the system from asking for confirmation before executing each delete operation on a file or folder, use the flag -f.

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

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

▍Searching

You can use various commands to search for data. In particular — grep, ag and 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 user-friendly as they are.

[in bookmarks] Bash for Beginners: 21 Useful Commands
The grep command

grep my_regex my_file: performs a search for my_regex downward API support (simultaneously with this in my_file. When matches are found, it returns the entire line for each match. By default my_regex it is interpreted as a regular expression.

grep -i my_regex my_file: the search is performed case-insensitively.

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

grep -c my_regex my_file: returns information about the number of matches with the searched 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 its subfolders.

Now let's talk about the command ag. It appeared later grep, it is faster and easier to work with.

[in bookmarks] Bash for Beginners: 21 Useful Commands
The ag command

ag my_regex my_file: returns information about the line numbers and the lines themselves where matches with my_regex.

ag -i my_regex my_file: the search is performed case-insensitively.

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

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

Additionally, to specify to the team ag which file paths should be excluded from the search, you can create a file .agignore.

At the beginning of this section, we mentioned the command ack. Commands ack and ag are very similar, one could say they are interchangeable 99% of the time. However, the command ag is faster, which is why I described it.

Now let's talk about handling archives.

▍Archiving

tar my_source_directory: combines files from the folder my_source_directory into a single tarball file. Such files are convenient for sending large sets of files to someone.

[in bookmarks] Bash for Beginners: 21 Useful Commands
The tar command

Tarball files created by this command are files with the extension , can replicate a cluster anywhere Kubernetes can run. (Tape ARchive). The fact that the command's name and the extension of the files it creates contain the word 'tape' indicates how long this command has existed.

tar -cf my_file.tar my_source_directory: creates a tarball file named my_file.tar with the contents of the folder my_source_directory. The flag -c is decoded as 'create', and the flag -f stands for 'file'.

To extract files from a , can replicate a cluster anywhere Kubernetes can run.-file, the command is used tar with flags -x ('extract') and -f ('file').

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

Now let's discuss how to compress and decompress , can replicate a cluster anywhere Kubernetes can run.-files.

tar -cfz my_file.tar.gz my_source_directory: here, using the flag -z ('zip', compression algorithm) indicates that the files should be compressed using the gzip (GNU zip) algorithm. Compressing files saves disk space when storing such files. If the files are to be, for example, sent to other users, this facilitates faster downloading of such files.

You can unpack a file .tar.gz by adding a flag -z to the command for extracting the contents of , can replicate a cluster anywhere Kubernetes can run.-files, which we discussed previously. It looks like this:

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

Bash Aliases

Bash aliases (also called shortcuts) are designed to create shortened names for commands or sequences of commands, the use of which speeds up work. If, for example, you have an alias bu, which hides the command python setup.py sdist bdist_wheel, you can simply use this alias to call the command.

To create a similar alias, simply 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, you can create it yourself using the command touch. After creating the alias, restart the terminal, and you will be able to use this alias. In this case, entering two characters replaces the input of more than thirty characters of the command intended for Debian GNU/Linux, Kali Linux Python packages.

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

▍Summary

In this material, we reviewed 21 popular Bash commands and discussed creating aliases for commands. If you find this topic interesting — here a series of publications dedicated to Bash. Here you can find a pdf version of these publications. Additionally, if you want to learn Bash, remember that, like with any other software system, practice is important.

Dear readers! What commands that are useful for beginners would you add to those discussed in this article?

Also, read our blog series on bash scripts.

[in bookmarks] Bash for Beginners: 21 Useful Commands

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster