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.
UNIX representation
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:
case-sensitive
Everything written in the terminal is case-sensitive. When the command is ls, neither Ls, lS nor LS will work.
Files and directories are also case-sensitive, eg foobar.txt and 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 \ :
Copy/paste
To copy or paste on the terminal, ctrl+c and ctrl+v won’t work.
Instead, we must use ctrl+shift+c and ctrl+shift+v.
:warning: ctrl+c is used to terminate the program
Suspending processes
ctrl+z will suspend the current process.
Using fg %1 will resume the job in foreground whereas bg %1 will resume in background.
To list all suspended jobs, just call jobs.
man the hell up
Use man whenever you aren’t sure about a command or its options…
Shortcuts
tab
Always use the tab button to autocomplete your command. It’s really useful to prevent any typos.
ctrl+r
“Reverse-i-search” is a shortcut to display a list of commands you have already used. It’s based on history.
!!
Re-execute last command:
!$
Execute last command’s value:
ctrl+q
“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.
ctr+x+e
Edit a command you’re currently typing. Useful if it’s a long command.
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:
read = 4
write = 2
execute = 1
which means:
7 = read + write + execute
6 = read + write
5 = read + execute
3 = write + execute
:warning: Please, just don’t do a chmod -R 777 *… for obvious security issues…
Stdin/Stout
There are three main file descriptors:
stdin is the input from the keyboard
stdout is output
stderr is the error output
Type
Symbol
stdin
0<
stdout
1>
stderr
2>
stdin
Allows you take standard input from a file:
stdout
stderr
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.
Mixing everything
Appending to file
Pipes
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
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,
ie /path/to/script.sh, or if it’s in the current directory ./script.sh.
:warning: 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:
Exit
The standard way to exit a script file is by returning the number 0:
If the script exists with anything other than 0 (a number between 1 and 255), that means there was an error.
Functions
To declare a function, all you need is to declare like this:
Variables
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: