11. chmod
-command to change file mode bits. This is used to assign or change permissions of files and directories. We need to understand first the file permission
Three types of permissions (can assign using the text(r,w,x) or using numeric (4,2,1)
4 – read(r)
2 – write(w)
1 – execute(x)
Three different roles or ownership
u – user or owner of the file
g – group
o – others
+ = means adding
– = means removing
Samples:
-Adding execute permission for all roles, use a+x (without a, it will only applies to user and group)
tux@freelinux:~$ ls -l file1
-rw-rw-r– 1 tux tux 0 Jan 17 15:18 file1
tux@freelinux:~$ chmod a+x file1
tux@freelinux:~$ ls -l
total 8
-rwxrwxr-x 1 tux tux 0 Jan 17 15:18 file1
-Adding execute permission for user role only, use u+x
tux@freelinux:~$ ls -l file2
-rw-rw-r– 1 tux tux 0 Jan 17 15:16 file2
tux@freelinux:~$ chmod u+x file2
tux@freelinux:~$ ls -l file2
-rwxrw-r– 1 tux tux 0 Jan 17 15:16 file2
– Removing write permission for multiple roles (u-w,g-w)
tux@freelinux:~$ ls -l file2
-rwxrw-r– 1 tux tux 0 Jan 17 15:16 file2
tux@freelinux:~$ chmod u-w,g-w file2
tux@freelinux:~$ ls -l file2
-r-xr–r– 1 tux tux 0 Jan 17 15:16 file2
-Using numeric to chmod, as mentioned 4=read,2=write,1=execute so can be broken down something like -421-421-421 in a file considering the u,g,o structure
rwx =7
r-x =5
r– =4
e.g. changing permission to all read for a file
tux@freelinux:~$ ls -l file3
-rw-rw-r– 1 tux tux 0 Jan 17 15:25 file3
tux@freelinux:~$ chmod 444 file3
tux@freelinux:~$ ls -l file3
-r–r–r– 1 tux tux 0 Jan 17 15:25 file3
e.g. changing all permissions for a file
tux@freelinux:~$ ls -l file4
-rw-rw-r– 1 tux tux 0 Jan 17 15:26 file4
tux@freelinux:~$ chmod 777 file4
tux@freelinux:~$ ls -l file4
-rwxrwxrwx 1 tux tux 0 Jan 17 15:26 file4
chmod -R –> to recursively change files and directories permissions
e.g. change user and group to all permission, and no write permission for others, recursively for sampledir1 folder
tux@freelinux:~/sampledir1/sampledir2$ ls -lR /home/tux/sampledir1/
/home/tux/sampledir1/:
total 4
drwxrwxr-x 2 tux tux 4096 Jan 17 15:29 sampledir2
/home/tux/sampledir1/sampledir2:
total 0
-rw-rw-r– 1 tux tux 0 Jan 17 15:29 ABCtux@freelinux:~$ chmod 775 -R /home/tux/sampledir1/
tux@freelinux:~$ ls -lR /home/tux/sampledir1/
/home/tux/sampledir1/:
total 4
drwxrwxr-x 2 tux tux 4096 Jan 17 15:29 sampledir2/home/tux/sampledir1/sampledir2:
total 0
-rwxrwxr-x 1 tux tux 0 Jan 17 15:29 ABC
12. chown
-command to change file owner and group
Syntax :
chown [OPTION]… [OWNER][:[GROUP]] FILE…
Change ownership of a file
root@freelinux:/home/tux# ls -l file1
-rw-rw-r– 1 tux tux 0 Jan 17 17:11 file1
root@freelinux:/home/tux# chown darwin file1
root@freelinux:/home/tux# ls -l file1
-rw-rw-r– 1 darwin tux 0 Jan 17 17:11 file1
Change the group ownership of a file
root@freelinux:/home/tux# ls -l file1
-rw-rw-r– 1 darwin tux 0 Jan 17 17:11 file1
root@freelinux:/home/tux# chown :admins file1
root@freelinux:/home/tux# ls -l file1
-rw-rw-r– 1 darwin admins 0 Jan 17 17:11 file1
Change user and group of a file
root@freelinux:/home/tux# ls -l file1
-rw-rw-r– 1 darwin admins 0 Jan 17 17:11 file1
root@freelinux:/home/tux# chown tux:tux file1
root@freelinux:/home/tux# ls -l file1
-rw-rw-r– 1 tux tux 0 Jan 17 17:11 file1
13. tail
-command to output the last part of files
Syntax:
tail [OPTION]… [FILE]…
Useful options with “tail”
tail -f –> to output appended data as file grows. It is useful in troubleshooting or debugging
e.g.
(Let say want to see your web server error logs in real-time)
root@freelinux:/var/log/apache2# tail -f /var/log/apache2/error.log
tail -n [N] –> to output the last [N] lines from file
tux@freelinux:~$ tail -n 2 /var/log/apache2/error.log
[Sun Jan 03 14:09:45.116780 2021] [mpm_prefork:notice] [pid 897] AH00163: Apache/2.4.27 (Ubuntu) configured — resuming normal operations
[Sun Jan 03 14:09:45.121054 2021] [core:notice] [pid 897] AH00094: Command line: ‘/usr/sbin/apache2’
tail -c [N] –> to output the last [N] bytes from file
tux@freelinux:~$ tail -c 100 /var/log/apache2/error.log
Sun Jan 03 14:09:45.121054 2021] [core:notice] [pid 897] AH00094: Command line: ‘/usr/sbin/apache2’
14. ps
– command to report a snapshot of the current processes
Syntax:
ps [options]
Useful options with “ps”
ps -ef –> e is to output all the process and f is to display in full listing format
tux@freelinux:~$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan28 ? 00:00:02 /sbin/init
root 2 0 0 Jan28 ? 00:00:00 [kthreadd]
root 4 2 0 Jan28 ? 00:00:00 [kworker/0:0H]
root 6 2 0 Jan28 ? 00:00:00 [mm_percpu_wq]
root 7 2 0 Jan28 ? 00:00:00 [ksoftirqd/0]
root 8 2 0 Jan28 ? 00:00:00 [rcu_sched]
“ps -aux” (BSD format) will give the same output as ps -ef
where -a to display all process from all users; -u to output in user-oriented format; -x list background processes and started upon system boot
ps -u [UID]–> show all processes running with specific user
tux@freelinux:~$ ps -u www-data
PID TTY TIME CMD
29015 ? 00:00:00 apache2
29016 ? 00:00:00 apache2
29017 ? 00:00:00 apache2
29018 ? 00:00:00 apache2
29019 ? 00:00:00 apache2
30187 ? 00:00:00 apache2
ps -p [PID] –> to select process ID, you can also use -q in quick mode
tux@freelinux:~$ ps -p 29015
PID TTY TIME CMD
29015 ? 00:00:00 apache2
Sample useful command for troubleshooting,e.g. sort by memory or cpu
ps -eo pid,cmd,%mem,%cpu –sort=-%mem | head
ps -eo pid,cmd,%mem,%cpu –sort=-%cpu | head
tux@freelinux:~$ ps -eo pid,cmd,%mem,%cpu –sort=-%mem | head
PID CMD %MEM %CPU
28672 qemu-system-x86_64 -enable- 16.7 204
8261 qemu-system-x86_64 -enable- 12.4 47.6
28321 ovs-vswitchd unix:/var/snap 0.1 1.6
tux@freelinux:~$ ps -eo pid,cmd,%mem,%cpu –sort=-%cpu | head
PID CMD %MEM %CPU
28672 qemu-system-x86_64 -enable- 16.7 204
8261 qemu-system-x86_64 -enable- 12.4 47.6
28321 ovs-vswitchd unix:/var/snap 0.1 1.6
15. top
-command to display Linux processes
Sample popular options:
top -u [UID]
tux@freelinux:~$ top -u www-data
Sample Output:
top – 12:58:54 up 429 days, 2:32, 2 users, load average: 6.32, 6.17, 6.16
Tasks: 467 total, 1 running, 466 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.8 us, 14.6 sy, 0.0 ni, 84.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 26394596+total, 17867691+free, 45759684 used, 39509372 buff/cache
KiB Swap: 26815590+total, 26815590+free, 0 used. 21245590+avail MemPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
29015 www-data 20 0 71648 4400 2996 S 0.0 0.0 0:00.00 apache2
29016 www-data 20 0 71448 4132 2896 S 0.0 0.0 0:00.00 apache2
29017 www-data 20 0 71648 4540 3124 S 0.0 0.0 0:00.00 apache2
Inside the top display, here’s some useful keys
(capital letters)
M – to sort by memory usage
P – to sort by CPU usage
N – to sort by process id
T – to sort by running time
16. cat
-command to concatenate files and print on standard output
Syntax:
cat [OPTION]… [FILE]…
tux@freelinux:~$ cat testfile1
This is a test file1
or multiple files:
tux@freelinux:~$ cat testfile1 testfile2
This is a test file1
This is a test file2
Some options with ‘cat’ command:
cat > [filename] –> use with > to create new file (Hold ‘Ctrl’ key and press d to exit and save changes
cat >>[filename] –> to append to the file
tux@freelinux:~$ cat >testfile3
This is a testfile3
tux@freelinux:~$ cat testfile3
This is a testfile3
tux@freelinux:~$ cat >>testfile3
2nd line
tux@freelinux:~$ cat testfile3
This is a testfile3
2nd line
cat [filename1] > [filename2] –> redirect contents from one file to another. Note: if file is existing, it will overwrite, so be careful.
tux@freelinux:~$ cat testfile3 > testfile4
tux@freelinux:~$ cat testfile4
This is a testfile3
2nd line
cat -n [filename] –> to display contents with line numbers
tux@freelinux:~$ cat -n testfile3
1 This is a testfile3
2 2nd line
17. mkdir
-command to make directories
Syntax:
mkdir [OPTION]… DIRECTORY…
mkdir -p –> to make parent directories, quite useful so for user not to create multiple sub-directories one by one
tux@freelinux:~$ mkdir -p /home/tux/dir1/dir2/dir3
mkdir -m –> to set custom file mode (as in chmod), default permission is 775 if not using this option
tux@freelinux:~$ mkdir -p -m 777 /home/tux/folder1/folder2/folder3
18. rmdir
-command to remove empty directories
Syntax:
rmdir [OPTION]… DIRECTORY…
rmdir -p –> to remove directory and its ancestors
tux@freelinux:~$ rmdir -p /home/tux/dir1
rmdir: failed to remove ‘/home/tux/dir1’: Directory not empty
tux@freelinux:~$ rmdir -p /home/tux/dir1/dir2/
rmdir: failed to remove ‘/home/tux/dir1/dir2/’: Directory not empty
tux@freelinux:~$ rmdir -p /home/tux/dir1/dir2/dir3/
rmdir: failed to remove directory ‘/home/tux’: Permission denied
tux@freelinux:~$ ls -l /home/tux/dir1
ls: cannot access ‘/home/tux/dir1’: No such file or directory
18. history
-command to show last commands that have been recently used. This is useful in case you forget what command was executed
19. clear
-command to clear the terminal screen
20. man
-command to show on-line reference manuals for commands. It will show details like syntax, description and options.
Sample:
tux@freelinux:~$ man kill
Sample output:
KILL(1) User Commands KILL(1)
NAME
kill – send a signal to a processSYNOPSIS
kill [options] <pid> […]
Honorable mentions:
more – command to display file or text files one screen at a time
less – command to display contents of a file one page at a time, similar to more but has advanced features of allowing user to navigate backward and forward through the file
df – command to report file system disk space usage
du – command to estimate file space usage