TECHY360
Everything You Need To Know About Tech

Linux Bash Shell – Beginner Cheat Sheet

0 9

This cheat sheet covers the following topics: introduction to the shell, navigation, basic commands, environment variables, connectors, pipelines, input/output redirection, access rights, and key combinations. 

Bash Shell: Introduction

A shell, or shell, is a program, in our case called “bash,” which is short for Bourne Again Shell. The shell accepts your commands and transfers them to the operating system. Terminals are used to interact with the system, such as gnome-terminaleterm, nxterm etc.

Navigation

In Linux, files and directories have a hierarchical organization, that is, there is a starting directory called the root directory. It contains files and subdirectories, which in turn contain files and their own subdirectories.

pwd

The command  pwd, short for a print working directory, display the current location in the directory structure.

cd

The command  cd allows you to move to a new directory.

SyntaxExplanation
cdMoving to the home directory
cd ~Moving to the home directory
cd ..Move up one level
cd -Move to previous directory
cd Directory1Move to Directory1
cd Directory1/Directory2Move to Directory2 at the specified path

mkdir

The command  mkdir creates a new directory in the current directory.

Basic commands

man

The command  man displays command guides. For example, the following command will display all the information about the command  cat:

$ man cat

cat

The command  cat reads the file passed as an argument and displays its contents on the standard output channel. Passing multiple files as an argument will output the concatenated contents of all files.

echo

The command  echo outputs its arguments on the standard output channel.

$ echo Hello World
  Hello World

If called  echo without arguments, an empty string will be displayed.

head

The command  head reads the first 10 lines of any transmitted text and outputs them through the standard channel. The number of lines displayed can be changed:

$ head -50 test.txt

tail

The command  tail works similarly to the command  head, but reads lines from the end:

$ tail -50 test.txt

You can also view the lines added to the file in real time using the flag  -f:

$ tail -f test.txt

less

The command  less allows you to move through the transferred file or a piece of text, and in both directions.

$ less test.txt
$ ps aux | less

Read more about the purpose of the symbol | will be explained below in the command section  history.

Regular shortcutsDescription
GMove to end of file
gMoves to the beginning of the file.
:50Moves to 50 line file
qExit from less
/searchtermSearch for a string matching searchterm below the current string
/Moves to the next matching search result.
?searchtermSearch for a string matching searchterm above the current string
?Moves to the next matching search result.
upMoves up one line
downMoves one line down.
pageupMoves up one page
pagedownMoves one page down.

true

The command  true always returns zero as output status to indicate success.

false

The command  false always returns a non-zero as output status to indicate failure.

$?

$? – this is a variable that contains the output status of the last running command The status is usually understood as the return code of the program. 0 means successful program execution, any value greater than 0 reflects the fact that during the execution some errors occurred. By the way, this is why in bash, true (true) is considered 0, and everything that is not 0 is false (false):

$ true
$ echo $?
  0
$ false
$ echo $?
  1

grep

The team  grep searches for the transferred string in the specified file:

$ cat users.txt
  user:student password:123
  user:teacher password:321
$ grep 'student` file1.txt
  user: student password:123

grep It can also accept multiple files and regular expressions to refine the text format.

Ordinary flagsDescription
-iDisable case sensitivity
-rRecursive Directory Search
-wSearch for whole words only
-cDisplay the number of items found
-nDisplay the entire line containing the query
-vInverted match output

sed

A command  sed is a stream editor that converts input text data. It is usually used to replace expressions like:  s/regexp/replacement/g. For example, the following code will replace all the words “Hello” with “Hi”:

$ cat test.txt
  Hello World
$ sed 's/Hello/Hi/g' test.txt
  Hi World

history

The command  history displays the history of the command line. Usually it is used together with the team  grep to find a specific team. For example, the following code will find all commands containing a string  g++:

$ history | grep g++
  155  g++ file1.txt
  159  g++ file2.txt

It also uses the symbol | – a so-called pipeline (pipe). Thanks to him, you can redirect the output of one command to the input of another – thus, in the example above, all the history that is normally output by the command history directly to the terminal output will be redirected to grep as input. We will not see the output of the command  history, but we will see the output of the command  grep.

It can be quite difficult to understand without practice, however, experiment independently, for example with the commands  ls,  history,  ps(described below) to command output in  grep,  sedor  lessexample.

Related Posts
1 of 8

export

The command  export sets environment variables to be passed to child processes. For example, you can pass a variable namewith the value student:

$ export name=student

ps

The command  ps displays information about running processes.

$ ps
  PID TTY          TIME CMD
  35346 pts/2    00:00:00 bash

Four items are displayed:

  • Process ID (PID),
  • terminal type (TTY)
  • process time (TIME),
  • The name of the command that started the process (CMD).

awk

The command  awk finds and replaces text in files according to a specified pattern:awk 'pattern {action}' test.txt

wget

The command  wget downloads files from the web and places them in the current directory.

$ wget https://github.com/mikeizbicki/ucr-cs100

nc

The command  nc is a utility for debugging a network.

ping

The team is  ping testing the network connection.

$ ping google.com
  PING google.com (74.125.224.34) 56(84) bytes of data.
  64 bytes from lax17s01-in-f2.1e100.net (74.125.224.34): icmp_req=1 ttl=57 time=7.82 ms
  --- google.com ping statistics ---
  1 packets transmitted, 1 received, 0% packet loss, time 8ms
  rtt min/avg/max/mdev = 7.794/8.422/10.792/0.699 ms

Statistics at the end shows the number of connections made before the completion of the command and the time of their execution.

git

Git – This is a popular version control system.

Environment variables

Environment variables are named variables containing values ​​used by one or more applications.

The variable  PATH contains a list of directories in which the system searches for executable files.

The variable  HOME contains the path to the home directory of the current user.

Connectors

Connectors allow you to run multiple commands at the same time.

ConnectorDescription
&&The first command is always executed, the second – only in case of successful completion of the first
||The first command is always executed, the second – only in case of the failure of the first
;Commands are always executed.
$ true && echo Hello
  Hello
$ false || echo Hello
  Hello
$ echo Hello; ls
  Hello
  test.txt file1.txt file2.txt

Conveyors

Conveyors, or pipes, allow connecting input and output channels of various commands. In the following example, the output of the command ls will be transferred to head, and as a result, only the first 10 elements will be printed.

$ ls -l | head

I / O Redirection

Output redirection

The standard output redirection uses the > and  symbols  >>.

For example, this code will transfer the output  ls to a file, not to the screen:

$ ls > files.txt
$ cat files.txt
  file1.cpp sample.txt

If the file does not exist, it is created, and if it does, it is overwritten. To avoid overwriting, use a command  >> — it appends data to the end of the file.

Input redirection

For standard output redirection, the symbol is used  <. The following example  sort takes input from a file, not from the keyboard:

$ cat files.txt
  c
  b
$ sort < files.txt
  b
  c

The command  sort prints the contents of the file to the screen, since we did not redirect the output. This can be done like this:

$ sort < files.txt > files_sorted.txt

Advanced redirection

Adding  & to  > leads to redirection of both the standard output stream and the error stream. For example, the file  test.cpp will output the string stdoutin  cout and the string stderr in  cerr.

$ g++ test.cpp
$ ./a.out >&amp; test.txt
$ cat test.txt
  stdout
  stderr

If you want to output a specific file descriptor, you can assign its number to  >.

NameDescriptorDescription
stdin0Standard input stream
stdoutoneStandard output stream
stderr2Standard error output stream

For example, to redirect stderrto  test.txt do the following:

$ g++ test.cpp
$ ./a.out 2> test.txt
  stdout
$ cat test.txt
  stderr

Access rights

The command  ls -l displays a lot of information about access rights to each file:

$ ls -l test.txt
  -rw-rw-r--  1  user  group  1097374 January 26 2:48 test.txt
The output in the exampleDescription / possible conclusions
File type: 
- file 
d directory
rw-File owner permissions
rw-Access rights of members of the group that owns the file
r–Access rights for other users
userFile owner name
groupName of the group that owns the file

chmod

The command  chmod changes the file permissions. Here are typical combinations of flags for changing the rights of specific users:

LetterUser
uOwner
gGroup member
oOther users
aAll users

You can call up  chmod with a description of actions on a specific file. The symbol  - indicates the removal of rights, the symbol  + – the addition. The following example makes the file readable and writable by owner and group:

$ chmod ug+rw test.txt
$ ls -l test.txt
  -rw-rw----  1  user  group  1097374 January 26 2:48 test.txt

In addition,  chmod you can use with octal numbers, where  1 is the availability of rights, and the  0 absence of:

rwx = 111 = 7
rw- = 110 = 6
r-x = 101 = 5
r-- = 100 = 4

The following command works the same as the previous one:

$ chmod 660 test.txt

Keyboard shortcuts

CombinationDescription
CTRL-AMove the cursor to the beginning of the line.
CTRL-EMove the cursor to the end of the line.
CTRL-RSearch history
CTRL-WCut the last word
CTRL-UCut all to a cursor
CTRL-KCut everything after the cursor
CTRL-YReturn the last cut line
CTRL-_Cancel
CTRL-LTerminal screen cleaning

Get real time updates directly on you device, subscribe now.

Comments
Loading...

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More