If you are looking for the manual in Polish, you will find it here.
-
From GUI to Terminal:
-
From terminal to GUI:
ls xdg-open .
-
Explore your home directory:
# ls # $HOME stores # path to your home directory ls $HOME echo $HOME
-
Survival 1 - where am i? Why I get
No such file or directory
orPermission denided
? Twese two commands will help you:pwd ls
-
Survival 2 - how can I come back to my home directory?
# main directory in linux is: / the same as C: in Windows cd / pwd # come back home with one command cd # let's do it again cd / pwd ls # ~ means my home directory # change the directory to ~ cd ~
-
Before learning how to work with directories, let's install
tree
:sudo apt-get update sudo apt install tree
Sudo? We have xkcd for that!.
-
Working with the directories:
mkdir poland cd poland mkdir cities cd cities mkdir gdansk ls mkdir warszawa mkdir wroclaw mkdir krakow ls cd .. pwd cd .. tree tree ../poland
You should see:
poland \- cities/ |- gdansk/ |- warszawa/ |- wroclaw/ \- krakow/
-
Traverse the directories
cd PATH
:# go in pwd cd cities/krakow pwd # go out cd ../..
-
Notice:
ls cities ls /home/ ls ~
-
Please add a directory
villages
that contains directories for small settlements in Poland:poland |- cities/ | |- gdansk/ | |- krakow/ | |- warszawa/ | \- wroclaw/ | \- villages/ |- nowawies \- starawies
-
Survival 3 - vim:
-
install vim:
sudo apt-get install vim
-
go back to your home dir:
cd
-
let's start vim:
vim learning_vim.txt
-
How to exit:
- ESC ESC
- :q
- ENTER
-
iii! I added a text (insert mode:
ESC ESC i
), but I just want to exit the vim without saving the content!- ESC ESC
- :q!
- ENTER
-
ok. Now I would like to add something to my file:
-
vim learning_vim.txt
-
ESC ESC
-
i
-
Please add sth, e.g.,
I love vim!
-
ESC ESC
-
:wq
-
ENTER
-
Let's see whether we saved the content:
cat learning_vim.txt
-
-
-
Survival 4 - emacs / nano:
nano
Notice,
^
means CTL/CONTROL. -
If you look for sth like notebook:
gedit p.txt
- If the command does not print anything, it means it succeeded
- Use:
TAB
TAB
to get your commands autocompleted
-
Let's go back to your home dir and create a dedicated directory for the exercises:
cd mkdir workspace cd workspace
-
Creating files with
touch
(to remember it) and writing to them withecho
:touch myfile.txt # create an empty file echo "Ubuntu 18.04" > myfile.txt echo "Ubuntu 20.04" >> myfile.txt cat myfile.txt echo "Mint" >> myfile.txt echo "Debian" >> myfile.txt echo "Redhat" >> myfile.txt echo "Linux" >> myfile.txt cat myfile.txt
-
Ofc, you can use the graphical editor or vim to change the file content:
atom myfile.txt
-
grep
is a basic CLI tool for the automation. Notice it is case sensitive:grep Linux myfile.txt grep Ubuntu myfile.txt # compare with: grep ubuntu myfile.txt
grep -i ubuntu myfile.txt
-
A common use case, we have logs to analyze:
grep -i Error /var/log/*log # alternative to `wc -l` grep -c -i Error /var/log/*log
-
How to copy files, use
cp
:cp myfile.txt myfile2.txt ls # we can `grep` all files # that ends with `.txt` grep Linux *.txt
-
How to move file (or rename file) from one directory to another?
mkdir mydirectory mv myfile2.txt mydirectory ls mydirectory tree ../ # let's move the file back # to the current directory `.` $ mv mydirectory/myfile2 .
-
We also use
mv
to rename files, let's go back to our directories for Polish cities and villages:cd ~ cd poland ls # sb asked us to use Polish word for cities # - miasta mv cities miasta ls tree ../poland
-
What if we need to have both, a directory with Polish name and English:
cp -r miasta cities cd .. tree poland
You should see
miasta
andcities
. -
Use
rm
to remove a file:cd cd workspace rm myfile2.txt
-
... or to delete a directory with content:
cd ls poland rmdir poland/cities/warszawa # does it work? rmdir poland/cities # if not, try this command: rm -r poland/cities
-
To close this section, create in your home directory (
*.txt
are files):biology/ |- trees | |- deciduous.txt | \- coniferous.txt | \- animals |- predators | \- cats.txt \- herbivorous \- cows.txt
Tasks (use
tree
to check whether you completed the task):- Move files
cats.txt
andcows.txt
underanimals/
, - Copy
coniferous.txt
to your home directory, check withls ~
whether you succeeded, - Delete the
animals
directory, - Delete the
biology
directory.
- Move files
-
We have in Linux hidden files. The hidden files starts with
.
:cd ls -a ls -la ls -la | grep bash
-
Environment variables, we use in every automation. Let's find out what the value is for the
HOME
andLANG
variables:printenv printenv | grep HOME= printenv | grep LANG= echo $HOME
-
Printing environment variables:
# compare $ echo "$HOME" $ echo '$HOME' # recommendation: # always with { and } $ echo "${HOME}"
-
Printing env variable to file:
cd ~ mkdir tmp cd tmp echo "$HOME" > home.txt cat home.txt
-
Adapt your command line interface with $HOME/.bashrc
atom $HOME/.bashrc
Add at the end of the
~/.bashrc
:echo "Hi ${USER}!" export MY_PHONE=9999
Open another terminal window or run
bash
in the exiting terminal window.# notice TAB TAB works echo $MY_PHONE
-
You can define a new env variable ad hoc in your terminal:
export SHOP_USER=natalia echo "$SHOP_USER"
-
Create your first bash script
my_script.sh
with the following content:#!/bin/bash echo "Hi ${USER}! Nice to meet you!" echo "Your home directory is ${HOME}" echo "Your configured language is ${LANG}"
and run it:
bash my_script.sh
cd
find . -iname *.txt
# ensure we get the file:
find . -iname *.txt -type f
# find in the home directory `~`
# a directory `-type d`
# with name `poland`
find ~ -iname poland -type d
-
You got an export from your ecommerce shop
koszyk1.txt
:milk,10,1zl bread,1,4zl
Using
cut
:cut -d',' -f1 koszyk1.txt cat koszyk1.txt | cut -d',' -f1
extract:
1zl 4zl
Bonus task:
- Print the user names from
/etc/passwd
- Extract month and days from
/var/log/syslog
- Print the user names from
-
Another day, another weird export from an online shop
koszyk2.txt
:product=mleko price=1 product=chleb price=4
With help of
cut
andpaste
, print on the screen:mleko 1 chleb 4
Hint:
cat koszyk2.txt | paste - -
-
Let's learn how to replace strings, assuming we have
koszyk3.txt
with the following content:product_category: mleczne
Our task is to replace
mleczne
(Polish word for the diary products) with the category IDM0
:product_category: M0
Hint:
sed 's/category/cat/g'
-
tr
is very useful when you need to replace a single character:cat koszyk3.txt | tr ':' '='
or remove it:
cat koszyk3.txt | tr -d ':'
-
Please find out what
head
,tail
, andless
do:less /var/log/syslog tail /var/log/syslog head /var/log/syslog head -n 10 /var/log/syslog
-
Given the following basket export structure (notice
*.txt
are files):basket |- chocolates | \- products.txt | wedel,2 | goplana,3 | |- dairy \- products.txt mlekovita,5 zimnemleko,4
-
With one command print the price for each of the item:
2 3 5 4
-
With one command (find it with help of Google on stackoverflow), calculate the basket value:
14
Notice how easy is to find a solution for a common problem in bash.
-
Install one from these two:
- ohmybash / bash-it if you are on Linux
- ohmyzsh if you are on Macos (a blog post for MacOS) or you are on Linux (blog post)
There are many plugins that gives you super powers in your terminal, you can check the top plugins on this page.