Ubuntu command line: basic bash commands
To work in Ubuntu, the user can choose one of two possible interface options: graphical or command line interface. The graphical user interface (GUI) is familiar to most users and requires various elements of the desktop to perform actions. It has its advantages, which are expressed mainly in a convenient and familiar format for presenting information, as well as in the absence of the need to know any commands.
However, many Linux users prefer to work from the command line(CLI, Command Line Interface). As a rule, these users are somehow related to programming or system administration, however, knowledge of the basics of bash will be useful to all users. The command line allows you to execute commands much faster than using the GUI, while saving resources. Moreover, commands often do not have their counterparts in the graphical interface and, thanks to this, give considerable freedom to users in their actions. At the same time, the command line interface is built into the kernel of the operating system, which means that it is available even when you cannot use the graphical shell. The commands themselves are the same for all Linux-based operating systems, be it Ubuntu, CentOs, Fedora or any other.
On this page
Bash stands for Bourne-Again SHell, the name is borrowed in part from an earlier version of the UNIX shell, which was called simply Bourne shell (“Bourne shell” - after the name of the developer Stephen Bourne).
Bash allows you to interact with your computer interactively by typing and responding to specific commands. Also, this command processor allows executing scripts (commands from a file), can perform auto-completion of names of files and directories, allows the use of variables, branch and loop operators.
Further in the article, the most simple and necessary commands for working in bash will be given, which will help you navigate and start using the command line yourself for your tasks, but first you need to go to the command line interface.
Using Ubuntu as your operating system, you can get to the command line in two ways, namely through the console or through the terminal.
If you want to enter the command line through the terminal, then for this you need to press Ctrl+Alt+F (1-6) - depending on the selected number (F1, F2 ...), you will be taken to the first, second or other virtual console ( they are independent and run at Ubuntu boot), each of which has a command line interface. If you press Ctrl+Alt+F7, you will go to the seventh virtual console, which has a graphical interface.
As for the terminal, it is a graphical program that emulates the console. You can find it in the menu by choosing Applications, then Accessories and then Terminal, or by pressing the Ctrl+Alt+T keyboard shortcut. You will be able to execute commands while still in the GUI.
There is also an option when you need to connect to the server on which Ubuntu is installed. In this case, you need to use a special program (for example, puTTy): type the desired ip address, and then enter the username and password.
Note - When you enter a password in the command line, it is not displayed - you just need to type the password and press Enter. If the password is entered correctly, you will be able to continue working at the command line.
Using bash, you can easily get any technical information you need about users and the system. If you want to identify which user are currently logged in, you need to enter the following command at the command line:
If you need to find out not only about yours, but also about other users currently on this operating system, use the commands:
Unlike whoami, these commands show more detailed information: in addition to the username, you will also find out which terminal line is in use, the session start time, IP address, and some other data.
As for the system data, it can be viewed using the uname command. By itself, this command will not give you much information - just the name of the system. However, if you use keys, you can learn a lot more.
Kwarg are special arguments that are separated by a space after the command and start with one or two hyphens. They define the parameters that will be applied to the command. As a rule, you can find out about the available keys by writing a command and --help (or -help): for example,
For the uname command, you can specify the -a (or --all) switch, and then all information about the operating system will be displayed on the screen:
If you are interested in specific information about the kernel, then type the following command:
In this case, you will learn about the release and version of the operating system kernel. You can get information about all processes using this command:
And the pstree command will display information in the form of a tree of processes.
You can find out the network name of the machine using the hostname command.
In this section, you will learn the basic bash commands that allow you to do the simplest things on the command line.
The cd command allows you to change from one directory to another. Moreover, if you use the command without switches, you will be taken to the home directory; in order to go to any specific directory, you just need to register its path, for example:
You can also specify cd command with specific keys:
cd ../.. - move to two directories above;
cd - - go to the previous directory (where you were before moving to the current one).
The pwd command will indicate the path of the directory in which you are currently located.
Use the mkdir command to create a directory. Don't forget to specify the name:
The new directory will be created in the directory where you are at the moment, but you can also write some other path to create a new directory:
To create a file, you need to enter the touch command and the name of the future file:
In this case, it is also possible to specify the path where you need to create the file:
To move directories and files, use the mv command, after which you write the name of the directory or file that you want to move, and then the place where you want to move:
mv file2 /home/users/dir2
mv dir1 /home/users/dir3
Note that the mv command is also used for renaming, so if you don't have dir2, you write this command:
mv dir1 dir2
Then the dir1 directory will be renamed to the dir2 directory. This example is also true for files that you can give other names to using the mv command.
To delete files, use the rm command, and to delete directories rmdir. If you want to delete a directory, use the rm command with the -rf keys:
rm -rf /home/users/
Another action that you can use is copying using the cp command. The syntax of this command is the same as the previous ones - you just write what and where you want to copy. To copy a directory, you must use the -r key, and if the destination directory (dir2) does not exist, it will be created when copying:
cp -r dir1 dir2
The ls command is used to display information about the contents of a directory. If you use it without keys, then it will simply display information about the files and directories available in this (or specified) directory. The main keys that are often used with this command are:
-a - you will be shown all the files in the directory, including those that start with . (dots);
-s - request information about the size of files (or directories);
-l - data about files, in particular the name of the user who created them, access rights and creation time;
-h - translate the file size into a human-readable format (KB, MB, etc.).
The keys can also be combined, so usually the command is used to display the size of the content:
Another command that will help you get the information you need is the du command. For example, by entering this command:
du -sk *
you will get information about the sizes and names of the files.
You can get information about running processes using the df command. It is best to use it right away with the -h switch to display the data in a more readable format:
The find command is used to find files in bash. Its syntax is as follows: find + desired directory (path) + key (or several) + key criteria + (optional) subsequent action on the found files.
You can familiarize yourself with the many options for this command, which greatly simplify and narrow the search, by entering into the command line:
For example, the following command will allow you to find all files that are located in the dir1 directory and have the name file:
find /home/users/dir1 -name ‘file*’
To search, it is most convenient to use some regular expressions, namely:
* - denotes any number of characters, including their absence (for example, the file* query will find all files: file, file1, file829, fileabc, etc.);
? - denotes one character (on request file? files with one character after the word file will be found: file1, file9, filea, etc.; on request file?? files with two characters will be found: file12, file34, file92, etc.);
 - denotes a subset of values that a single character can take (by request file [1-9] the following files will be found: file1, file2, file3, and so on up to file9; by request file [ad], files filea, fileb, filec and filed).
Redirecting the output of a command is specified by the > or >> symbol. The difference is that when you use >, the output of the command will be written over the contents of the file, and when you use >>, the output is appended to the end of the file.
This is very convenient to use for logging if you want to write any data to a file. For example, this command will write all files found according to the specified parameters to the log file (it will automatically appear in the directory in which you are working):
find /home/users -name ‘file*’ >> log
As far as the pipeline is concerned, this tool allows you to use the output of one command as the input of another command. To do this, use the | symbol:
find /home/users -name ‘file*’ | sort -r
If you enter this line, all files that match the condition will be found first, and then with the sort command they will be sorted in reverse order (due to the use of the -r key).
If you want to see the contents of a file, you can refer to the cat command - in response to the command, you will see everything contained in the file. If there is too much data, use a pipeline and the more command - it will allow you to view the content page by page:
cat file1 | more
If you need to not only view the contents of the file, but also edit it, then you need to use one of the utilities: nano or vim. These are text editors with which you can open a file, modify and save it. Despite the apparent similarities, these utilities have a number of differences; most users agree that nano is a simpler editor, while vim is more powerful.
There are many different commands and switches that can be used in bash. In this article, we have focused only on the most basic, basic commands with which you can perform operations and obtain the necessary information.
You can find the full documentation at this address: https://help.ubuntu.com/community/CommunityHelpWiki