Bash Basics

Introduction

$ in the prompt means that you are running Bash % means that you are using Zsh

~ before $ means that you are in your home directory

To run a command you need to press Enter

The ls command shows the contents of the current directory

ls

docker Documents Music notes.txt Pictures python snap Templates yandex-tank Desktop docker-lab Downloads node_modules Public shell_scripts ssh-com Videos yarn.lock

With the cd command you can change directory

cd Documents

To return to the home directory, it is enough to run cd without parameters

To repeat the previous command, it is enough to press the up arrow

SSH

To connect to a remote computer over SSH run

ssh user_name@computer_name

ssh andrei@andrei-ubuntu

To close the ssh session run

exit

Read more about working with SSH in Linux in the article « SSH Basics »

Automatic completion

When using cd, writing the full name of the directory is not necessary.

If from the home directory you want to go to Documents, it is enough to type Doc and press Tab.

If you type just D and press Tab, Bash will not understand where you want to go - to Desktop, Documents or to Downloads, so nothing will happen (in Zsh a hint will appear at once).

If you press Tab once more - a hint will appear, namely a list of all directories whose name starts with D

cd D

Desktop/ Documents/ Downloads/

To go to the parent directory run

cd ..

To find out the full path to the current directory run

pwd

Arguments

Let us consider the command

cd Documents

cd is a command, Documents is an argument (argument).

To see the hidden files in a directory run ls -a

Where a means All

Hidden files have a dot as the first character in their name

ls -a

. .bashrc Desktop docker-lab .gnupg Music Public snap .sudo_as_admin_successful .Xauthority .. .bash_history .cache .docker Documents .local node_modules Pictures python .ssh Templates yandex-tank .bash_logout .config docker Downloads .mozilla notes.txt .profile shell_scripts ssh-com Videos yarn.lock

-a is a special kind of argument which is called an option (option).

Options add extra functionality to commands.

Options start with the symbol -

After - you can list several options in a row

To see additional information about files run ls -l, l means long format

ls -l

total 96 drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Desktop drwxrwxr-x 3 andrei andrei 4096 syys 17 19:09 docker drwxrwxr-x 4 andrei andrei 4096 heinä 19 21:46 docker-lab drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Documents drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Downloads drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Music drwxr-xr-x 2 root root 4096 heinä 18 22:11 node_modules -rw-rw-r-- 1 andrei andrei 1 syys 19 21:40 notes.txt drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Pictures drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Public drwxrwxr-x 2 andrei andrei 4096 syys 19 12:54 python drwxrwxr-x 2 andrei andrei 4096 syys 19 22:01 shell_scripts drwxr-xr-x 3 andrei andrei 4096 syys 15 19:58 snap drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Templates drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Videos drwxrwxr-x 3 andrei andrei 4096 heinä 16 16:55 yandex-tank -rw-r--r-- 1 root root 86 heinä 18 22:11 yarn.lock

To find out what other options the ls command has, run

man ls

The result of this command will be a detailed description of all the options. The list is quite long, to move down the list press the space bar, to go back up press b

To search by a word type /word. To go to the next search result press n

To close man press q

Read more about all the options of ls in the article «man ls»

To see additional information about files, including hidden ones and sort the output by modification time run ls -lat

ls -lat

total 176 drwxr-xr-x 31 andrei andrei 4096 syys 20 10:20 . -rw------- 1 andrei andrei 34 syys 20 10:20 .lesshst -rw------- 1 andrei andrei 236 syys 20 10:16 .Xauthority -rw------- 1 andrei andrei 17546 syys 19 23:40 .bash_history drwxrwxr-x 2 andrei andrei 4096 syys 19 22:01 shell_scripts -rw-rw-r-- 1 andrei andrei 1 syys 19 21:40 notes.txt drwxrwxr-x 2 andrei andrei 4096 syys 19 12:54 python drwx------ 2 andrei andrei 4096 syys 17 19:19 .ssh drwxrwxr-x 3 andrei andrei 4096 syys 17 19:09 docker drwx------ 3 andrei andrei 4096 syys 15 20:04 .gnupg drwxr-xr-x 3 andrei andrei 4096 syys 15 19:58 snap drwxr-xr-x 15 andrei andrei 4096 syys 15 19:58 .cache drwxr-xr-x 12 andrei andrei 4096 syys 15 19:58 .config drwxrwxr-x 4 andrei andrei 4096 heinä 19 21:46 docker-lab drwx------ 2 andrei andrei 4096 heinä 18 23:06 .docker drwxr-xr-x 2 root root 4096 heinä 18 22:11 node_modules -rw-r--r-- 1 root root 86 heinä 18 22:11 yarn.lock drwxrwxr-x 3 andrei andrei 4096 heinä 16 16:55 yandex-tank drwxr-xr-x 4 andrei andrei 4096 heinä 16 15:08 .local -rw-r--r-- 1 andrei andrei 0 heinä 16 12:27 .sudo_as_admin_successful drwx------ 5 andrei andrei 4096 heinä 16 12:12 .mozilla drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Desktop drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Documents drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Downloads drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Music drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Pictures drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Public drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Templates drwxr-xr-x 2 andrei andrei 4096 heinä 16 12:11 Videos -rw-r--r-- 1 andrei andrei 220 heinä 16 11:53 .bash_logout -rw-r--r-- 1 andrei andrei 3771 heinä 16 11:53 .bashrc -rw-r--r-- 1 andrei andrei 807 heinä 16 11:53 .profile drwxr-xr-x 3 root root 4096 heinä 16 11:53 ..

Read about how to work with command-line arguments in your scripts here

$(): Use the output of another command

To see which terminal you are connected to run

tty

/dev/pts/31

To make a listing of this file you can copy the output and paste it after ls

ls -l /dev/pts/31

crw--w---- 1 andrei tty 136, 31 мая 23 13:48 /dev/pts/31

To get the same result faster

ls -l $(tty)

crw--w---- 1 andrei tty 136, 31 мая 23 13:48 /dev/pts/31

Viewing files

To open the contents of a text file run cat file_name or less file_name

cat textfile
less book1

cat will output the contents to the console output. less will open the file in the whole window.

Navigation through a file opened with less is done in the same way as through man.

Space down
b up
/ search
n next search result
q close

If you try to open a binary file with less, you may get a warning

"file1" may be a binary file. See it anyway?

cat will open such a file without a warning

To continue press y. Most likely the result will not please you - you will get a hard-to-read set of characters.

<C3>&NU_p<C6>x<E6><AC>^N<B4>E<EA>^B<D1>E^U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6<95><96>3<87>^<BC><E0><F7><AE>wB<B3><B9><BB><F8><96><FE>"<B6>i^SH_^O<C3>f<C1><F2>^D<B3><99>Y<86>A<EE><8A>1<9F><FA>hk<9F><F8>U<E2>ESCESCmCR<D3>C <C9>5֩<E2><AD>NH^@<88><FF>^@<A9>^D<BE><E2>F@^_(^Y$f<93><8A>Acд<BF>^QZj>#<D4><F4>(!<B9>^W^Zg<97><F6><96>h<F0><83>z<EE>\6y<E3>=<AB>d<F4><AF>1<B2><D5>WE<F8><8D><E3>k<B9>a<95><CD><CC><FA>]<AD><A8>򘇑<90><C7><D8>t ^E<81>'<D0>^Z<EE><FC>;<AD><DA>k<BA>y<BF><B1>Y<C4>"Y"<FD><F4>E^X<94>m<A4><80>{d^\^^<F4><9C>l<AE>I<A6>("<8A>*U<80>Lr^O<S<87>ZJ*<A2><80>u^T<99><A0>V<9C>ɈSH:<D0>h^T<80>O<D4>PzQ<C6>x<E3>ڑ<BD><E9>1<86>=M!<CE>^? <AD>)<C7>zi<F4>&<A4>^C<9A>(<FC>h<A0>

To close the window with these characters, try pressing q or entering the command reset

In order not to get into such a situation, you can check the file type before opening it. This can be done with the command file

file file1

file1: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, baseline, precision 8, 325x500, components 3

It turns out that file1 is not text but an image in JPEG format

You can try to open the image from the terminal with the command xdg-open, it is not a fact that it will work on your system, on my Ubuntu 20.4 it worked.

xdg-open file1

Editing text files

To edit a text file you need to open it in one of the editors.

Most likely you already have either nano or vi

in nano everything is fairly obvious, about vi I advise you to read a separate article here

You can create a new file with the command touch

touch new_file

You can delete a file with the command rm

rm new_file

Create several files at once:

touch file1 file2 file3 file4

To create a file with spaces in the name you need to put the name in quotes:

touch 'file with spaces in name'

You can create a new directory with the command mkdir

mkdir stands for Make Directory

mkdir new_folder

If you need to create nested directories - use the flag -p (parent)

mkdir -p city/district/street

To create a directory and immediately go there use

mkdir example && cd $_

To exit the current process press CTRL + C

It is better not to use the following characters for the names of files and folders:

' " ` { } ( ) < > [ ] ! ? & ] : ; \ ^ $ @ ~ * #

Try to open a file with cat -vet

cat -vet somefile

You can see that at the end of the line there is a $ as well as the encoding of Cyrillic characters.

Redirecting output

The output can be redirected to a file using > or >>

> will delete the previous entries from the file

If you perform the redirection twice into the same file. Only the last one will remain

echo "Great laptops here" > log.txt
echo "Books with delivery across Russia here" > log.txt
vi log.txt

Books with delivery across Russia here

As you can see, the entry about the laptops was not saved

How to append a line to the end of a file in Bash

In order not to overwrite but to add a new line you need to use >>

echo "Great laptops here" >> log.txt
echo "Books with delivery across Russia here" >> log.txt
vi log.txt

Great laptops here
Books with delivery across Russia here

The result of the command can be sent to another command.

If you make a listing of a directory with a large number of files with the ls command, for example ls /usr/bin the result will not fit on one screen.

It may be convenient to send this output straight to less

ls /usr/bin | less

To find in a file the lines with a mention of the needed word you need to use the command grep

Suppose you have a list of site partners eth1.ru , which is called partners.txt .

If you run

less partners.txt

The list will turn out to be very long

Aviasales Travel
Booking.com Hotels
Hotellook Hotels
TopBicycle.ru Bicycles
Velodrive Bicycles
HeiHei.ru Travel
Xiaomi Android
Samsung Android
Book24 Books
GeekBrains Education
Netology Education
TestSetup.ru Education
Pluralsight Education
SovcomInsurance Insurance
Polis 812 Insurance
Polis 812 Insurance
Vivo Android
Reg.ru Hosting
…

After the name comes the category to which the partner belongs.

Suppose you want to find all partners of the Android category

grep Android partners.txt

Xiaomi Android
Samsung Android
Vivo Android

This list can be sorted in alphabetical order and saved to the file android_partners.txt

grep Android partners.txt | sort > android_partnets.txt
cat android_partners.txt

Samsung Android
Vivo Android
Xiaomi Android

Let us count how many partners of the Education category are in this list.

cut -f 2 partners.txt | grep Education | wc -l

4

cut -f 2 leaves only the second column

grep Education leaves only Education

wc -l counts lines (word count -lines)

Indeed, there are four platforms in the education category GeekBrains, Netology, TestSetup.ru and Pluralsight

PATH

View PATH

echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Add the directory new_bin to PATH

export PATH=$PATH~/new_bin

Default programs

To set nano as the default editor you can use the command

export EDITOR=nano

To make this choice permanent, you need to add the corresponding entry to .profile (bash) or .zshenv (zsh)

There you can also write export PATH=$PATH~/new_bin

export EDITOR=nano export PATH=$PATH~/new_bin

*

To work with a large number of files it is often convenient to use patterns.

Suppose you want to make a listing of only .pdf files

ls *pdf

Find all lines in the file filename in which the letters a r g appear in a row.

grep *arg* filename

Restart the terminal

To restart the terminal without leaving it, run

exec bash

CTRL R: search past commands

To search by a keyword among the commands run earlier you can use

CTRL + r

Article author: Andrei Olegovich