Section | Topic |
---|---|
Lesson 1 | basic terminal commands |
Lesson 2 | file permissions & groups |
Lesson 3 | ps command |
Lesson 4 | kill & ping |
Lesson 5 | foreground and background |
Research 1 | NI vs PRI & daemon |
Lesson 6 | nice and renice |
- ls - list directory contents
ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).- ls: displays files in a bare format
$ ls README.md
- ls -l: long format, displaying Unix file types, permissions, number of hard links, owner, group, size, last-modified date and filename
$ ls -l total 0 -rw-r--r-- 1 hassan hassan 0 Aug 13 23:55 README.md
- pwd - print name of current/working directory
pwd [OPTION]...
Print the full filename of the current working directory.$ pwd /home/hassan/os-lab-cheatsheet
- cd - change directory
cd DIRECTORY...
Change the current working directory to given directory (home directory by default). - mkdir - make directories
mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.$ mkdir test $ ls README.md test
- rm - remove files or directories
rm [OPTION]... [FILE]...
rm removes each specified file. By default, it does not remove directories.- rm -r: remove directories and their contents recursively
- rm -f: ignore nonexistent files and arguments, never prompt
- rm -rf: remove directories and their contents recursively, by force
- touch - change file timestamps
touch [OPTION]... FILE...- touch: a FILE argument that does not exist is created empty
$ touch file.txt
- cp - copy files and directories
cp [OPTION]... [-T] SOURCE DEST
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.$ cp README.md test
- You should use
sudo
before your commad when you want to run the command under the root permission. Also switch to root user withsudo -i
. Useexit
orCTRL+D
to exit from root shell. - In terminal,
.
means current working directory and..
means parent of current directory.$ pwd /home/hassan/os-lab-cheatsheet $ cd ../.. $ pwd /home
-
File permissions in Linux/Unix:
The first set of three permissions, after the place for d, applies to the owner of the file
The second set of three permissions applies to the all users who are members of the group of the file
The third set of three permissions applies to others
- Read: This permission give you the authority to open and read a file. Read permission on a directory gives you the ability to lists its content.
- Write: The write permission gives you the authority to modify the contents of a file. The write permission on a directory gives you the authority to add, remove and rename files stored in the directory.
- Execute: In Unix/Linux, you cannot run a program unless the execute permission is set. If the execute permission is not set, you might still be able to see/modify the program code(provided read & write permissions are set), but not run it.
-
Number of each permission:
Number Permission Type Symbol 0 No Permission --- 1 Execute --x 2 Write -w- 3 Execute + Write -wx 4 Read r-- 5 Read + Execute r-x 6 Read + Write rw- 7 Read + Write + Execute rwx -
chmod - change file mode bits
chmod [OPTION]... MODE[,MODE]... FILE...
chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits.$ chmod 755 README.md
-
groupmod - modify a group definition on the system
groupmod [options] GROUP
The groupmod command modifies the definition of the specified GROUP by modifying the appropriate entry in the group database.$ groupadd new_group
-
groups - print the groups a user is in
groups [OPTION]... [USERNAME]...
Print group memberships for each USERNAME or, if no USERNAME is specified, for the current process (which may differ if the groups database has changed).$ groups hassan hassan : hassan adm cdrom sudo dip plugdev lpadmin sambashare
-
useradd - create a new user or update default new user information
useradd [options] LOGIN useradd is a low level utility for adding users. On Debian, administrators should usually use adduser(8) instead. -
adduser, addgroup - add a user or group to the system
adduser [options] user group
adduser and addgroup add users and groups to the system according to command line options and configuration information in /etc/adduser.conf. They are friendlier front ends to the low level tools like useradd, groupadd and usermod programs.$ adduser new_user
-
usermod - modify a user account
usermod [options] LOGIN
The usermod command modifies the system account files to reflect the changes that are specified on the command line.- usermod -G GROUP1[,GROUP2,...[,GROUPN]]]: new list of supplementary GROUPS
- usermod -a USER: add the USER to the supplementary group(s). Use only with the -G option
$ usermod -G new_group -a new_user
- ps - report a snapshot of the current processes.
ps [options]
ps displays information about a selection of the active processes.$ ps PID TTY TIME CMD 18920 pts/3 00:00:00 zsh 18968 pts/3 00:00:00 ps
- ps -l: long format
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 S 1000 19749 19738 0 80 0 - 6679 sigsus pts/2 00:00:00 zsh 4 R 1000 19962 19749 0 80 0 - 5709 - pts/2 00:00:00 ps
- ps -f: full-format, including command lines
$ ps -f UID PID PPID C STIME TTY TIME CMD hassan 18920 18534 7 16:04 pts/3 00:00:00 zsh hassan 18956 18920 0 16:04 pts/3 00:00:00 ps -f
- ps -t: all processes on this terminal
$ ps -t PID TTY STAT TIME COMMAND 18920 pts/3 Ss 0:00 zsh 19193 pts/3 R+ 0:00 ps -t
- ps -r: only running processes
$ ps -r PID TTY STAT TIME COMMAND 19245 pts/3 R+ 0:00 ps -r
- ps -e: all processes
- ps aux:
a = show processes for all users
u = display the process's user/owner
x = also show processes not attached to a terminal
use grep for searching from command output
$ ps aux | grep zoom hassan 13232 33.9 3.9 5151328 484560 tty2 SLl+ 14:41 43:40 /opt/zoom/zoom hassan 22290 0.0 0.0 20476 2900 pts/3 S+ 16:50 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn zoom
- kill - send a signal to a process
kill [options] [...]
The default signal for kill is TERM. Use -l or -L to list available signals.$ kill -l HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS
- kill PID: terminate a process with PID
$ kill 13232
- killall PNAME: terminate all processes with PNAME
$ killall zoom
- xkill - kill a client by its X resource
xkill is a utility used for force-quitting GUI apps. It is handy when some app isn't responding or is causing your system to work abnormally. - pstree - display a tree of processes
pstree shows running processes as a tree. - ping - send ICMP ECHO_REQUEST to network hosts
ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway.$ ping api.telegram.org PING api.telegram.org (149.154.167.220) 56(84) bytes of data. 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=1 ttl=50 time=12.9 ms 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=2 ttl=50 time=13.6 ms 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=3 ttl=50 time=13.1 ms 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=4 ttl=50 time=12.10 ms 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=5 ttl=50 time=13.1 ms 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=6 ttl=50 time=12.10 ms
- Foreground and Background Processes:
Foreground processes refer to applications you are running that you are currently interacting with, and which applies equally to graphical user interfaces as it does to the command line. Background processes refer to applications that are running but not being interacted with by the user. - nano - Nano's ANOther editor, an enhanced free Pico clone
nano is a small and friendly editor.- nano FILE: open FILE with nano (create the FILE if is not exists)
$ nano script.sh
- cat - concatenate files and print on the standard output
cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.$ cat script.sh ping google.com > res.out
- jobs - display the status of jobs in the current shell
$ jobs [1] + suspended ./script.sh
- bg - move jobs to the background
- bg %jobID
$ bg %1 [1] + 28975 continued ./script.sh
- fg - move jobs to the foreground
- fg %jobID
$ fg %1 [1] + 28975 running ./script.sh
- You can suspend a job with
CTRL+Z
- You can end a job with
CTRL+C
- Nice Value and Priority
PRI is the priority level. The lower the PRI, the higher the priority of the process will be.
PRI is calculated as follows:- for normal processes:
PRI = 20 + NI
(NI is nice and ranges from -20 to 19) - for real time processes:
PRI = - 1 - real_time_priority
(real_time_priority ranges from 1 to 99)
The nice value is a "global" mechanism, whereas priority is relevant for the task switcher right now.
- for normal processes:
- Daemon Process
A daemon process is a background process that is not under the direct control of the user. This process is usually started when the system is bootstrapped and it terminated with the system shut down.
In Unix, the names of daemons conventionally end with the letter d, for clarification that the process is in fact a daemon, and for differentiation between a daemon and a normal program.
-
top - display Linux processes
The top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel.
Useq
orCTRL+C
to exit from top. -
nice - run a program with modified scheduling priority
nice [OPTION] [COMMAND [ARG]...]- nice -n: add integer N to the niceness (default 10)
$ nice -n 15 nautilus
$ sudo nice -n -8 nautilus
-
renice - alter priority of running processes
renice [-n] priority [-g|-p|-u] identifier...
renice alters the scheduling priority of one or more running processes. The first argument is the priority value to be used. The other arguments are interpreted as process IDs (by default), process group IDs, user IDs, or user names.$ renice 2 14862 14862 (process ID) old priority 0, new priority 2