start
  
  

Linux handbook

April 12, 2020

What is a Terminal ?

A Terminal is a program that emulates a physical Terminal (Console). The Terminal interacts with the Shell (the Command Line Interface).

What is a Shell ?

The Shell is a command-line interpreter, that is to say, it is a program that processes and executes commands.


A few simple commands

date is a command that prints the current date and time

cal display the calendar of the current month

lscpu command description

edo@ubuntu ~/Playground lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 36 bits physical, 48 bits virtual
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 58
Model name: Intel(R) Core(TM) i3-3120M CPU @ 2.50GHz
Stepping: 9
CPU MHz: 1200.000
CPU max MHz: 2500,0000
CPU min MHz: 1200,0000
BogoMIPS: 4988.79
Virtualization: VT-x
L1d cache: 64 KiB
L1i cache: 64 KiB
L2 cache: 512 KiB
L3 cache: 3 MiB
NUMA node0 CPU(s): 0-3

uptime command to check how long your system has been running

clear command to clear the Terminal screen

reboot command to restart your system

pwd command to print the name of your current working directory

edo@ubuntu ~/Playground pwd
/home/edo/Playground

ls command to list the contents of your current working directory

passwd command to change your password

hostname command to display your system's hostname

free command to display the amount of free and used memory on your system

edo@ubuntu ~/Playground free
total used free shared buff/cache available
Mem: 5960092 2623072 519340 476752 2817680 2583180
Swap: 0 0 0

command with the -h option

edo@ubuntu ~/Playground free -h
total used free shared buff/cache available
Mem: 5,7Gi 2,5Gi 481Mi 462Mi 2,7Gi 2,4Gi
Swap: 0B 0B 0B

df command to display the amount of disk space available on your system (-h to display a nicer format)

history command display all the commands that you ran in chronological order

uname command to display your system's kernel information:

  • -r option to print the current kernel release information
  • -a option to print all the information of your current kernel at once

lsb_release -a command to display the Ubuntu version you are currently running


Filesystem

cd command is short for Change Directory and is one of the most used commands in Linux. You can't move around in Linux without it.

ls command to view the contents of the current directory:

  • -l Long and detailed listing of files.
  • -a List the hidden files.
  • -d List directories themselves, not their contents.
  • -t Sort files by modification times.
  • -u When used with -l, it shows access times instead of modification times. When used with -lt, it will sort by, and show, access times.
  • -r Will reverse listing order.
  • -R List subdirectories recursively.

man hier command to read more about the Linux filesystem hierarchy

NAME
hier - description of the filesystem hierarchy
DESCRIPTION
A typical Linux system has, among others, the following directories:
/ This is the root directory. This is where the whole tree starts.
/bin This directory contains executable programs which are needed in single user mode
and to bring the system up or repair it.
/boot Contains static files for the boot loader. This directory holds only the files
which are needed during the boot process. The map installer and configuration
files should go to /sbin and /etc. The operating system kernel (initrd for ex‐
ample) must be located in either / or /boot.
/dev Special or device files, which refer to physical devices. See mknod(1).
/etc Contains configuration files which are local to the machine. Some larger soft‐
ware packages, like X11, can have their own subdirectories below /etc. Site-
wide configuration files may be placed here or in /usr/etc. Nevertheless, pro‐
grams should always look for these files in /etc and you may have links for
these files to /usr/etc.

whoami command prints the name of the currently logged-in user

su command to switch to any user on your system

cd ~ command will land you straight to your home directory

touch command:

  1. You can update the last modification and access times of existing files.
  2. You can create new empty files.

mkdir command is short for make directory is used to create directories:

  • -p option to create a whole path of directories

Editors

nano editor is a very popular and easy-to-use command-line editor.

shortcuts:

KeyWhat it does
Ctrl+OSaves the current file (write out)
Ctrl+KCuts the current line and stores it in the buffer
Ctrl+UPastes the line stored in the buffer
Ctrl+WSearches for a string (word) in the file
Ctrl+\ Replaces a string (word) in the file with another string
Ctrl+RReads another file
Ctrl+GViews help information on how to use nano
Ctrl+VMoves to the next page
Ctrl+YMoves to the previous page
Ctrl+XExits the nano editor

vi editor is a more advanced Linux editor with tons of features and is by far the most popular editor among advanced Linux users.

works in two different modes:

  1. insert mode enables you to insert text into a file
  2. command mode allows you to do things like copying, pasting, and deleting text

insert mode

To insert text, you need to switch to insert mode. There are several ways you can use to change to insert mode:

KeyWhat it does
iInserts text before the current cursor position.
IInserts text at the beginning of the current line.
aAppends text after the current cursor position.
AAppends text after the end of the current line.
oCreates a new line below the current line.
OCreates a new line above the current line.

command mode

To switch back to command mode, you need to press the Esc key. Most popular commands list:

CommandWhat it does
yyCopy (yank) the current line
3yyCopy (yank) three lines (starting with the current line)
ywCopy (yank) one word starting at the cursor position
2ywCopy (yank) two words starting at the cursor position
pPaste after the current cursor position.
PPaste before the current cursor position.
ddCut (delete) the current line
4ddCut (delete) four lines (starting with the current line).
dwCut (delete) one word starting at the cursor position.
xDelete the character at the cursor position.
uUndo the last change.
UUndo all changes to the line.
/redSearch for the word red in the file.
:%s/bad/goodReplace the word bad with good.
:set numberShow line numbers.
:set nonumberHide line numbers.
:7Go to line number 7.
GJump to the end of the file.
ggJump to the beginning of the file.

Tip:

You should know that :%s/old/new will only replace the first occurrence of the word old with new on all the lines. To replace all the occurrences of the word old with new on all the lines, you should use the global option :%s/old/new/g


saving and exiting

CommandWhat it does
:wSave the file but do not quit vi.
:wqSave the file and quit vi.
ZZSave the file and quit vi (same as :wq, just faster!).
:xSave the file and quit vi (same as :wq or ZZ).
:qQuit vi without saving.
:q!Forcefully quit vi without saving.

achieve the same result:

  1. :wq
  2. :x
  3. ZZ

file viewing commands

cat command is one of the most popular and frequently used commands in Linux. The cat (short for concatenate) command concatenates and prints files to the standard output (terminal).

tac command is basically cat written in reverse.

more command displays the content of a file one page at a time.

  • Enter > to scroll down one line.
  • Space Bar > to go to the next page.
  • b > to go back one page.
  • q > to quit.

less command is an improved version of the more. The advantage of less is that you can use the UP/DOWN arrow keys to navigate through the file. Also, less is faster than more.

head command displays the first few lines of a file

  • -n option to specify the number of lines you wish to view

tail command displays the last few lines of a file

  • -n option to specify the number of lines you wish to view

Copying, Moving, and Deleting Files

Copying

cp command to make a copy

  • -r option
cp source_file(s) destination

Moving

mv command to move a file (or a directory) to a different location. You can also use the mv command to rename files.

mv source_file(s) destination

Hiding

You can hide any file by renaming it to a name that starts with a dot.

mv file.txt .file.txt

Removing

rm command to remove (delete) files

  • -r option to remove directories

Useful commands

Determining a command's type

type command to determine the type (category) of a command

edo@ubuntu:~$ type pwd
pwd is a shell builtin
edo@ubuntu:~$ type ls
ls is an alias for ls --color=tty

Finding a command's location

which command to determine the location of an executable command

edo@ubuntu:~$ which rm
/bin/rm
edo@ubuntu:~$ which reboot
/sbin/reboot

whatis command to get a brief description of what a command does

edo@ubuntu:~$ whatis free
free (3) - allocate and free dynamic memory
free (1) - Display amount of free and used memory in the system
edo@ubuntu:~$ whatis df
df (1) - report file system disk space usage

The man page

man page is a manual page that has proper documentation to help you understand how to use a command

keysWhat it does
SpaceScrolls forward one page
Ctrl+FScrolls forward one page (same as space)
Ctrl+BScrolls backward one page
/wordWill search for a word (pattern) in the man page. For example, /access will search for the word access in the man page
qWill quit the man page
nAfter you search for a word, you can use n to look for the next occurrence of the word in the man page
NAfter you search for a word, you can use N to look for the previous occurrence of the word in the man page

The info page

info pages as an alternative documentation to the man pages

The very helpful apropos command

apropos command is one of the most helpful and yet underrated Linux commands

/usr/share/doc directory is another excellent place to look for help in Linux


File inodes

Every file on Linux has a set of attributes like:

  • File type
  • File size
  • File owner
  • File permissions
  • Number of hard links
  • File timestamp

These attributes are stored in a data structure called the inode (index node), and each inode is identified by a number (inode number).

What is an Inode?

An inode is simply a file data structure that stores file information (attributes), and every inode is uniquely identified by a number (inode number).

There are two commands you can use to view the inode number of a file:

  1. ls -i file
  2. stat file

is simply a file that points to another file

ln -s original_file soft_link

a soft link has the following properties:

  • The inode of a soft link is different from the original file.
  • A soft link becomes useless once the original file is deleted.
  • Any change to the soft link is actually a change in the original file.
  • You can create soft links to directories.

is simply an additional name for an existing file. It has the same inode of the original file, and hence, it's indistinguishable from the original file.

ln original_file hard_link

A hard link has the following properties:

  • A hard link has (shares) the same inode of the original file.
  • A hard link remains intact if the original file gets deleted.
  • Any change in the hard link is reflected in the original file.
  • You can't create hard links to directories.

Root

root is a Linux user that has permission to do anything on the system. root is also known as the superuser.

todo

TODO...

TODO

todo

TODO...
Edoardo Armandi

Edoardo Armandi

Full Stack Developer - Consultant

 

© 2012 - 2022 Matrizlab - Edoardo Armandi