Unix terminal survival kit
Do you have to manage a server?
Do you need to perform some actions in bulk?
Do you want impress your peers with a “hacking” skills?
Whatever the reason, it can be much faster to complete some tasks using a Terminal than with graphical applications and menus. Another benefit is allowing access to many more commands and scripts.
In UNIX everything is represented by a process or file. A process is an executing program. Files are collections of data organized by a directory structure.
Files can be identified by absolute or relative paths. For example:
Everything written in the terminal is case-sensitive. When the command is
LS will work.
Files and directories are also case-sensitive, eg
FoObAr.txt are two different files, even if they are in the same directory.
Beware of blank spaces
If you want to create/access/delete a file or directory that has a space in its filename, you can either put the whole filename in quotation mark
or escape the space using the backslash
To copy or paste on the terminal,
ctrl+v won’t work.
Instead, we must use
ctrl+c is used to terminate the program
ctrl+z will suspend the current process.
fg %1 will resume the job in foreground whereas
bg %1 will resume in background.
To list all suspended jobs, just call
man the hell up
man whenever you aren’t sure about a command or its options…
Always use the
tab button to autocomplete your command. It’s really useful to prevent any typos.
“Reverse-i-search” is a shortcut to display a list of commands you have already used. It’s based on
Re-execute last command:
Execute last command’s value:
“Parks” the current command you’re currently typing. Useful if you forgot to perform another command before executing another.
It may only be available on zsh.
Edit a command you’re currently typing. Useful if it’s a long command.
Credit to Clément Chastagnol.
Of course, bear in mind these keyboard shortcuts depends on your unix distribution, your shell, your configuration, …
Super user VS sudo
There are two ways to run administrative applications:
- run as “super user” (root) with the
- take advantage of
sudo(Substitute User DO)
sudo allows an user to run a program as another user (most often as the root user).
File and folder navigation
File and directory handling
File and folder permissions
Permissions are managed in three distinct scopes or classes:
Files and directories are owned by a user. The owner determines the file’s user class. Distinct permissions apply to the owner.
Files and directories are assigned a group, which define the file’s group class. Distinct permissions apply to members of the file’s group. The owner may be a member of the file’s group.
Users who are not the owner, nor a member of the group, comprise a file’s others class. Distinct permissions apply to others.
The effective permissions are determined based on the first class the user falls within in the order of user, group then others. For example, the user who is the owner of the file will have the permissions given to the user class regardless of the permissions assigned to the group class or others class.
Unix-like systems implement three specific permissions that apply to each class:
- The read permission grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory, but not to find out any further information about them such as contents, file type, size, ownership, permissions.
- The write permission grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files.
- The execute permission grants the ability to execute a file. This permission must be set for executable programs, in order to allow the operating system to run them. When set for a directory, the execute permission is interpreted as the search permission: it grants the ability to access file contents and meta-information if its name is known, but not list files inside the directory, unless read is set also.
Another method for representing Unix permissions is an octal notation. This notation consists of at least 3 digits.
Each digit represent a different component of the permission: owner, group and others.
Each digit is the sum of its component bit in the binary numeral system:
- 7 = read + write + execute
- 6 = read + write
- 5 = read + execute
- 3 = write + execute
⚠️ Please, just don’t do a
chmod -R 777 *… for obvious security issues…
There are three main file descriptors:
stdinis the input from the keyboard
stderris the error output
Allows you take standard input from a file:
There is a special file on the Linux system called
/dev/null which can be considered as the “Bin”,
but once information has gone to this file, it’s gone forever.
Appending to file
You can connect two commands together so that the ouput from one program becomes the input of the next program
by using the
Executing a command within a command
When editing a file in a linux server, you do not have the choice of your favorite GUI notepad or IDE.
You will have to stick with a terminal editor, like vi, vim, nano,…
I have my preference on vim so I will share just a few tips on how to use it without being lost.
First of all, vim has 3 modes:
insertmode: you write text as if in normal text editor
- use the
ibutton to switch to this mode
- use the
abutton to switch to this mode and move the cursor to the next character
- use the
obutton to switch to this mode and add a newline
- use the
normalmode: provides efficient ways to navigate and manipulate texts
- use the
Escbutton to switch to this mode
- use the
visualmode: select text using movement keys before deciding what to do with it
- use the
vbutton to switch to this mode
- use the
shift+vbuttons to select lines instead of characters
- use the
normal mode, you can navigate by using the arrow buttons, but vim has been thought for productivity.
So instead of using the arrow buttons, and thus moving your right hand, vim has a set a buttons for which
you won’t need to move your palms and be able to reach every functionnalities vim has to offer.
So use the following to navigate in your file:
h: move left
j: move down
k: move up
l: move right
You can also move by words:
w: moves to the start of the next word
e: moves to the end of the word
b: moves to the beginning of the word
If you want to navigate quickly in your file:
ctrl+d: page down
ctrl+u: page up
gg: go to the first line
shift+g: go to the last line
<number>g: go to line
5g: go to line 5
10g: go to line 10
0: move cursor to the beginning of the line
$: move cursor to the end of the line
*: find next occurrence of the word under the cursor
#: find previous occurrence of the word under the cursor
%: go to matching parenthesis/brackets
You don’t have to be in
insert mode to edit text. You can also edit portion of your file in
x: delete the character under the cursor
X: delete the character to the left of the cursor
r: replace only one character under the cursor
cw: remove character until the end the word and switch to
c$: remove character until the end of the line and switch to
dw: delete the first word on the right side of the cursor and copies the content
dd: delete the line and copies the content
yw: copy word
yy: copy line
p: paste the copied content
.: repeat the previous command
Search and replace
/: search from top to bottom
?: search from bottom to top
N to search next and previous occurrence, respectively.
To replace, you can use the same syntax as
:s/wordtoreplace/replacedword/: only replace the first instance of “wordtoreplace” of the line where the cursor is
:%s/wordtoreplace/replacedword/: apply to the entire file
:%s/wordtoreplace/replacedword/g: apply to all instances
Other VIM commands
:w: save file
:q: quit file edition
:q!: force quit
:wq: save and quit file edition
:x: save and quit the file edition
:set invnumber: display/hide line numbers
:set invpaste: format/do not format copied content (useful when copy pasting formatted code)
- press the
You can undo the last action by pressing the
u button in
If you want to undo the undo, you can press the
Going further with VIM
I did not cover everything, only what’s needed to be able to perform basic edition on files using VIM.
There are lot of tutorials out there:
Writing a SH script
Linux doesn’t care about extension. So if your script file name is
foobar.txt, you can still execute it.
However, it’s considered as best practice to have the extension
.sh for script files.
To run a script file, you cannot just type
script.sh, you will need to precede the script by the PATH to the script,
/path/to/script.sh, or if it’s in the current directory
⚠️ Don’t forget to add the
execute permission to your file:
chmod +x /path/to/script.sh
Shebang or hashpling #!
We need to tell the script what shell it’s going to run under as the user that will execute the script may not use the shell needed to execute the script.
For example, if we want to force bash, we need to add the following at the first line of the script:
If we want to use another shell, like Korn shell:
The standard way to exit a script file is by returning the number
If the script exists with anything other than 0 (a number between 1 and 255), that means there was an error.
To declare a function, all you need is to declare like this:
You can define variables simply like this:
If you want to add a parameter in your function:
$#: represents the number of parameters
$0: represents the script filename
$1: represents the first parameter
$2: represents the second parameter
$3: represents the third parameter
Here a sample script that will display a help message if there are no parameter provided:
There are tools like Bash Automated Testing System (or bats) that can help you test your scripts.
Customizing your terminal
You can customize your terminal by editing your
.zshrc, … to:
- add aliases
- add environment variables
- add plugins
There are lot of resources out there:
- oh-my-zsh is a good start to customize your terminal
- dotfiles on github list a set of setups you can inspire from for your computer setup
- my dotfiles
Your terminal can also feel like $HOME