Linux handbook
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 lscpuArchitecture: x86_64CPU op-mode(s): 32-bit, 64-bitByte Order: Little EndianAddress sizes: 36 bits physical, 48 bits virtualCPU(s): 4On-line CPU(s) list: 0-3Thread(s) per core: 2Core(s) per socket: 2Socket(s): 1NUMA node(s): 1Vendor ID: GenuineIntelCPU family: 6Model: 58Model name: Intel(R) Core(TM) i3-3120M CPU @ 2.50GHzStepping: 9CPU MHz: 1200.000CPU max MHz: 2500,0000CPU min MHz: 1200,0000BogoMIPS: 4988.79Virtualization: VT-xL1d cache: 64 KiBL1i cache: 64 KiBL2 cache: 512 KiBL3 cache: 3 MiBNUMA 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 freetotal used free shared buff/cache availableMem: 5960092 2623072 519340 476752 2817680 2583180Swap: 0 0 0
command with the -h option
edo@ubuntu ~/Playground free -htotal used free shared buff/cache availableMem: 5,7Gi 2,5Gi 481Mi 462Mi 2,7Gi 2,4GiSwap: 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
NAMEhier - description of the filesystem hierarchyDESCRIPTIONA 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 modeand to bring the system up or repair it./boot Contains static files for the boot loader. This directory holds only the fileswhich are needed during the boot process. The map installer and configurationfiles 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 forthese 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:
- You can update the last modification and access times of existing files.
- 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:
Key | What it does |
---|---|
Ctrl+O | Saves the current file (write out) |
Ctrl+K | Cuts the current line and stores it in the buffer |
Ctrl+U | Pastes the line stored in the buffer |
Ctrl+W | Searches for a string (word) in the file |
Ctrl+\ | Replaces a string (word) in the file with another string |
Ctrl+R | Reads another file |
Ctrl+G | Views help information on how to use nano |
Ctrl+V | Moves to the next page |
Ctrl+Y | Moves to the previous page |
Ctrl+X | Exits 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:
- insert mode enables you to insert text into a file
- 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:
Key | What it does |
---|---|
i | Inserts text before the current cursor position. |
I | Inserts text at the beginning of the current line. |
a | Appends text after the current cursor position. |
A | Appends text after the end of the current line. |
o | Creates a new line below the current line. |
O | Creates 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:
Command | What it does |
---|---|
yy | Copy (yank) the current line |
3yy | Copy (yank) three lines (starting with the current line) |
yw | Copy (yank) one word starting at the cursor position |
2yw | Copy (yank) two words starting at the cursor position |
p | Paste after the current cursor position. |
P | Paste before the current cursor position. |
dd | Cut (delete) the current line |
4dd | Cut (delete) four lines (starting with the current line). |
dw | Cut (delete) one word starting at the cursor position. |
x | Delete the character at the cursor position. |
u | Undo the last change. |
U | Undo all changes to the line. |
/red | Search for the word red in the file. |
:%s/bad/good | Replace the word bad with good. |
:set number | Show line numbers. |
:set nonumber | Hide line numbers. |
:7 | Go to line number 7. |
G | Jump to the end of the file. |
gg | Jump 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
Command | What it does |
---|---|
:w | Save the file but do not quit vi. |
:wq | Save the file and quit vi. |
ZZ | Save the file and quit vi (same as :wq, just faster!). |
:x | Save the file and quit vi (same as :wq or ZZ). |
:q | Quit vi without saving. |
:q! | Forcefully quit vi without saving. |
achieve the same result:
- :wq
- :x
- 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 pwdpwd is a shell builtinedo@ubuntu:~$ type lsls 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/rmedo@ubuntu:~$ which reboot/sbin/reboot
whatis command to get a brief description of what a command does
edo@ubuntu:~$ whatis freefree (3) - allocate and free dynamic memoryfree (1) - Display amount of free and used memory in the systemedo@ubuntu:~$ whatis dfdf (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
keys | What it does |
---|---|
Space | Scrolls forward one page |
Ctrl+F | Scrolls forward one page (same as space) |
Ctrl+B | Scrolls backward one page |
/word | Will search for a word (pattern) in the man page. For example, /access will search for the word access in the man page |
q | Will quit the man page |
n | After you search for a word, you can use n to look for the next occurrence of the word in the man page |
N | After 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
Links
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:
- ls -i file
- stat file
soft links
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.
hard links
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...