You are on page 1of 29

HM LINUX SERIES 2020

25 TIPS

©HM
HM LINUX SERIES 2020 25 TIPS
Table of Contents
1. sudo !! command...............................................................................................3
2. free -m command..............................................................................................4
3. touch command.................................................................................................5
4. killall command..................................................................................................6
5. last command....................................................................................................7
6. which command................................................................................................8
7. blkid command..................................................................................................9
8. systemd-analyze command.............................................................................10
9. df command.....................................................................................................12
10. tee command.................................................................................................13
11. lsof command................................................................................................14
12. fsck command................................................................................................15
13. chmod command...........................................................................................16
14. ps command..................................................................................................18
15. journal command..........................................................................................19
16. find command................................................................................................20
17. lspci command...............................................................................................21
18. curl ifconfig command...................................................................................22
19. ss command...................................................................................................23
20. Ctrl + L Key Combination...............................................................................24
21. ping command...............................................................................................25
22. uname command...........................................................................................26
23. rfkill command...............................................................................................27
24. systemctl command.......................................................................................28
25. dd command..................................................................................................29

©HM 2
HM LINUX SERIES 2020 25 TIPS

1. sudo !! command
If you forget to type sudo before a command that requires root privileges, just type
the following command:
$ sudo !!
Example:
$ apt update
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

$ sudo !!
More information about sudo on terminal
$ man sudo
$ sudo --help
Advanced information
Execute a specific command from the history using !n
If you’ve executed a command earlier, instead of re-typing it again, you can quickly execute it by
using the corresponding number of the command in the history.
For example, you executed 6 commands. To execute command #4, do the following. This will
display command #4 from the history, and execute it immediately.
$ !4

To execute a command that was typed 2 commands back, do the following.


$ !-2

To execute the previous command, do any one of the following:


$ !!

$ !-1

You can also press <Ctrl>-P (if you are in the default emacs mode) to get to the previous command.
If you’ve enabled vi style editing for the command line using ‘set -o vi’, use <Esc>-k to get to the
previous command.

©HM 3
HM LINUX SERIES 2020 25 TIPS

2. free -m command
To know the available amount of memory in megabytes, at any time, just type the
following command:
$ free -m
Example:$ free -m

More information about free on terminal


$ man free
$ free --help
Advanced information
Most useful options:
● -b, – -bytes : It displays the memory in bytes.
● -k, – -kilo : It displays the amount of memory in kilobytes(default).
● -m, – -mega : It displays the amount of memory in megabytes.
● -g, – -giga : It displays the amount of memory in gigabytes.
● – – tera : It displays the amount of memory in terabytes.
● -h, – -human : It shows all output columns automatically scaled to the shortest three digit
unit and display the units also of print out. The units used are B(bytes), K(kilos), M(megas),
G(gigas), and T(teras).
● -c, – -count : It displays the output c number of times and this option actually works with -s
option.
● -l, – -lohi : It shows the detailed low and high memory statistics
● -o, – -old : This option disables the display of the buffer adjusted line.
● -s, – -seconds : This option allows you to display the output continuously after s seconds
delay.
● -t, – -total : It adds an additional line in the output showing the column totals.
● -V, – -version : It displays version info and exit.

©HM 4
HM LINUX SERIES 2020 25 TIPS

3. touch command
To create a new empty file, just type the following command:
$ touch nameofthefile
Example:$ touch overview.txt
The file overview.txt will be created in the default directory (/home/nameoftheuser)
You can verify this by typing $ ls.
More information about touch on terminal
$ man touch
$ touch --help
Advanced information
How to change access/modification time
Let's take an existing file as an example. Suppose you have created overview.txt last week.
By using the stat command:
$ stat overview.txt you will get the information about the timestamps: the last time the file was
accessed/opened by some command or application, the last time the file’s content was modified
and the last time the file’s attribute or content was changed. The attribute includes file
permissions, file ownership or file location.
Now, type $ touch overview.txt
By running $ stat overview.txt again, you will notice that the timestamps are updated to the
current time.

How to change only access or modification time


By default, the touch command changes both access and modification times of the input file.
However, if you want, you can limit this behavior to any one of these timestamps. This means that
you can either have the access time changed or the modification timestamp.
In case you want to only change the access time, use the -a command line option.
$ touch -a [filename]

Similarly, if the requirement is to only change the modification time, use the -
m command line option.

$ touch -m [filename]

©HM 5
HM LINUX SERIES 2020 25 TIPS

4. killall command
When an app freezes, you can use the following command:
$ killall nameoftheapp
Example:$ killall firefox

killall sends a signal to all processes running any of the specified commands.
If no signal name is specified, SIGTERM is sent and the app will close immediately.

In UNIX-like systems, the SIGTERM signal is used for terminating a program.

More information about free on terminal


$ man killall
$ killall --help
Advanced information
The killall command is case-sensitive!
It throws an error 'no process found' error if the user fails to write the process name in correct
case.

How to make killall ask before terminating process?


$ killall -i nameoftheapp

How to choose the signal killall sends to process?


By default, it's the SIGTERM signal that killall sends to processes. However, you can send other
signals as well. You can use the options -s, --signal, and -SIGNAL to send these other signals.
However, for this to work, you need to know what all signals the killall command can send. This
you can do using the -l command line option.
$ killall -l

How to kill all processes owned by a user?


If the requirement is to kill all processes that a specific user owns, then you can use the -u option
provided by killall. Needless to say, the option requires you to specify the username for the user as
its input.
$ killall -u nameoftheuser

©HM 6
HM LINUX SERIES 2020 25 TIPS

5. last command
To know who logged in your computer, and when, simply use the following
command:
# last
Example using the ‘number of lines to display’ option
# last -2

More information about last on terminal


$ man last
$ last --help
Advanced information
Most useful options

● -number : this option is used to specify the number of lines to display.


● -s, -t : It is used to display within a specific time period “ since (-s) and until (-t).
● -F : It is used to display the login and logout time including the dates.
● -w : it is used full user and domain names.

©HM 7
HM LINUX SERIES 2020 25 TIPS

6. which command
To know where an app is located, use the following command:
$ which nameoftheapp
Example:
$ which blender

More information about which on terminal


$ man which
$ which --help
Advanced information
How to locate multiple apps at a time:

$ which nameofapp1 nameofapp2 … nameofappN


Example: $ which firefox yaourt mega

Because the app mega doesn’t exist, the output of the third line gives all the possible bin locations
for apps, on this computer. So, using which with the name of an app that doesn’t exist, is a trick to
find out all possible bin locations on the computer.

©HM 8
HM LINUX SERIES 2020 25 TIPS

7. blkid command
To list all the drives of a computer with their UUID, use the following command:
# blkid
Example:
# blkid
/dev/sdb1: UUID="6e0acfe3-81ed-4f9f-8ab5-0d65ba1f0ef2" TYPE="ext2"
/dev/sdc1: UUID="aa82d7bb-ab2b-4739-935f-fd8a5c9a6cb0" TYPE="ext2"
/dev/sda1: UUID="187171ab-c9b8-43ec-b0bb-77c736ca22e0" TYPE="ext4" LABEL="/home"
/dev/sda2: UUID="1a225baa-7027-4619-aaa5-900e24c1fdff" TYPE="swap"
/dev/sdb3: UUID="2a294b33-eb61-40a3-b3fc-ad6eaf7f156f" TYPE="ext2"

More information about blkid on terminal


$ man blkid
$ blkid --help
Advanced information
Display additional information
The -p option will display additional information, as it will not use the cache, and directly use the
information from the superblock.
The following displays information about /dev/sdc. Please note that for this option, you should
pass a device name as an argument.
Examples:
# blkid -p /dev/sda

# blkid -p /dev/sda1

# blkid -p /dev/sda2

©HM 9
HM LINUX SERIES 2020 25 TIPS

8. systemd-analyze command
To check how fast your computer starts and find out why it boots slow, use the
following command:
$ systemd-analyze
Example:
$ systemd-analyze

$ systemd-analyze blame

More information about systemd-analyze on terminal


$ man systemd-analyze
$ systemd-analyze --help
Advanced information
With the command $ systemd-analyze critical-chain you will be able to print a tree of
the time critical chain of units.

Example on next page.

©HM 10
HM LINUX SERIES 2020 25 TIPS

©HM 11
HM LINUX SERIES 2020 25 TIPS

9. df command
To check the available space on disks, in human readable format, use the following
command:
$ df -h
Example:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 24G 2.5G 20G 11% /
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 2.0G 1.6M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
tmpfs 2.0G 0 2.0G 0% /tmp
tmpfs 396M 0 396M 0% /run/user/1000

More information about df on terminal


$ man df
$ df --help
Advanced information
Most useful options:

● -T : this causes a new column to be added to the output displaying the filesystem type.
● -t : this only shows specific file system types. Pass the -t option followed by the filesystem
type or types to be shown. This causes only the file system types to be shown. The -t
option accepts a list of types. Example: df -t ext4
● -x : this excludes specific file types. Pass the -x option followed by the filesystem type you
want to exclude. The -t option accepts a list of types. Example: df –x tmpfs

©HM 12
HM LINUX SERIES 2020 25 TIPS

10. tee command


To break the output of a command, so that it can be both displayed and saved in a
file use the following command:
$ command | tee filename.TXT
Example:
# blkid | tee UUID.txt
Now we have two outputs:
- the displayed file on the screen
- the created file, saved in the current directory (/home/nameoftheuser/

/dev/sda1: UUID="2C4D-5CBD" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="cd38710e-


5538-426c-a30b-80afbf1ba2d6"
/dev/sda2: UUID="d40d766d-894e-414e-bda4-d2fae785d374" BLOCK_SIZE="4096"
TYPE="ext4" PARTUUID="f574ad9b-297e-444b-9e9d-b545d222879c"
/dev/sdb1: LABEL="BU_drive" UUID="e689674c-6a7d-4a74-8182-550fbc0d8764"
BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="756e7877-01"

More information about tee on terminal


$ man tee
$ tee --help
Advanced information
Most useful options
● -a : with this option you can append information to an existing file.
$ command | tee -a nameofthefile
● To append information to multiple existing files:
$ command | tee -a nameofthefile1 nameofthefile2 … nameofthefileN

©HM 13
HM LINUX SERIES 2020 25 TIPS

11. lsof command


To check all opened network connections, use the following command:
$ lsof -i
Example:
$ lsof -i

More information about lsof on terminal


$ man lsof
$ lsof --help
Advanced information
Most useful option
To list all the processes listening on a specified port:
$ lsof -i: port number
Example: $ lsof -i :80

©HM 14
HM LINUX SERIES 2020 25 TIPS

12. fsck command


To check and repair a filesystem, use the following command:
# fsck /dev/sdletterofthedrivefigureofthepartition
Example:
# fsck /dev/sdc1
PAY ATTENTION NOT TO RUN THIS COMMAND WHILE
YOUR FILESYSTEM IS MOUNTED!

To check and repair a filesystem correctly, follow this procedure


If the filesystem is NOT the ROOT filesystem, unmount the filesystem and proceed.
Example:
# umount /dev/sdc1
# fsck /dev/sdc1
If the filesystem IS the ROOT filesystem, then use a live CD* to access your system
from outside. Do NOT mount the filesystem. Run the fsck command.
Example:
# fsck /dev/sda2
More information about fsck on terminal
$ man fsck
$ fsck --help
Advanced information
Most useful option
● -C : this will display progress bar for those filesystem checkers which support them.

* I recommend to read HM SERIES 2020_ARCHISO AS RESCUE TOOL available here

©HM 15
HM LINUX SERIES 2020 25 TIPS

13. chmod command


To check and/or set the file permissions flags on a file or folder, use the following
command:
$ chmod 3 digits (0,1,…..,7) filename or folder name
Examples:
$ chmod 755 file1.txt
$ chmod Documents
The flags define who can read, write to or execute the file. If you list files with the
ls -l (long format) command you’ll see a string of characters that look like:
-rwxrwxrwx
If the first character is a “-”, the item is a file, if it is a d, the item is a directory. The
rest of the string is three sets of three characters.
From the left, the first three represent the file permissions of the owner, the
middle three represent the file permissions of the group and the rightmost three
characters represent the permissions for others. In each set, an r stands for read, a
w stands for write, and an x stands for execute.
If the r, w, or x character is present that file permission is granted. If the letter is not
present and a “-” appears instead, that file permission is not granted.

One way to use chmod is to provide the permissions you wish to give to the owner,
group, and others as a 3-digit number. The leftmost digit represents the owner. The
middle digit represents the group. The rightmost digit represents the others. The
digits you can use and what they represent are listed here:

• 0: No permission
• 1: Execute permission
• 2: Write permission
• 3: Write and execute permissions
• 4: Read permission
• 5: Read and execute permissions
• 6: Read and write permissions
• 7: Read, write and execute permissions

©HM 16
HM LINUX SERIES 2020 25 TIPS

More information about chmod on terminal


$ man chmod
$ chmod --help
Advanced information
Practical Examples

chmod 400 mydoc.txt read by owner


chmod 040 mydoc.txt read by group
chmod 004 mydoc.txt read by anybody (other)
chmod 200 mydoc.txt write by owner
chmod 020 mydoc.txt write by group
chmod 002 mydoc.txt write by anybody
chmod 100 mydoc.txt execute by owner
chmod 010 mydoc.txt execute by group
chmod 001 mydoc.txt execute by anybody

©HM 17
HM LINUX SERIES 2020 25 TIPS

14. ps command
To view all the running processes , use the following command:
$ ps
To view all the running processes for an effective user ID or user name:
$ ps – user ID or user name

PID TTY          TIME CMD


   537 ?        00:00:00 systemd
   538 ?        00:00:00 (sd-pam)
   553 ?        00:00:00 startplasma-x11
   561 ?        00:00:09 dbus-daemon
   579 ?        00:00:00 start_kdeinit
   580 ?        00:00:00 kdeinit5
   581 ?        00:00:01 klauncher
   589 ?        00:00:12 kded5
   601 ?        00:00:01 kaccess
   609 ?        00:00:01 kglobalaccel5
   620 ?        00:00:00 dconf-service
   625 ?        00:00:02 kactivitymanage
   645 ?        00:00:05 mission-control
   647 ?        00:00:01 kscreen_backend
   665 ?        00:00:00 plasma_session

More information about ps on terminal


$ man ps
$ ps --help
Advanced information

https://www.tecmint.com/ps-command-examples-for-linux-process-monitoring/

©HM 18
HM LINUX SERIES 2020 25 TIPS

15. journal command


To check errors of the system, use the following command:
# journalctl -fp err
# journalctl -fp err

To check the journal for a given subject and for a given period, use options.
Example:
# journalctl -b -u NetworkManager – since=”2020-04-16 06:00:00”

More information about journalctl on terminal


$ man journalctl
$ journalctl --help
Advanced information
Most useful options
https://wiki.archlinux.org/index.php/Systemd/Journal

©HM 19
HM LINUX SERIES 2020 25 TIPS

16. find command


To find files, use the following command:
$ find -iname filename (“I” for case non-sensitive)
Example:
$ find -iname myfile.odt

If you don’t remember the name of the file but remember, for instance, that the file
is a .mp3 file with more than 10MB

$ find / -type f -name *.mp3 -size =10M

More information about find on terminal


$ man find
$ find --help
Advanced information
Most useful options
The document below gives 35 examples of what can be done with the command
find.

https://www.tecmint.com/35-practical-examples-of-linux-find-command/

©HM 20
HM LINUX SERIES 2020 25 TIPS

17. lspci command


To display information about all the hardware devices connected to your PCI or PCIe
bus, use the following command:
$ lspci
Examples:

To display information about your Network Card:


$ lspci -k

06:00.0 Network controller: Intel Corporation WiFi Link 5100


Subsystem: Intel Corporation WiFi Link 5100 AGN
Kernel driver in use: iwlwifi
Kernel modules: iwlwifi

For additional level of verbosity add -v, -vv or -vvv

Example: $ lspci -k -v what is actually the same than $ lspci -kv

To display information about your Video Card:


$ lspci | grep VGA or $ lspci | grep -e VGA -e 3D (“e” is a grep option and stands for
“extended”)

More information about lspci on terminal


$ man lspci
$ lspci --help
Advanced information
Most useful options
-k to display information about you Network Card
-v, -vv or -vvv to increase the verbosity (more detailed information)

©HM 21
HM LINUX SERIES 2020 25 TIPS

18. curl ifconfig command


To obtain your external IP-address, use the following command:
$ curl ifconfig.me
Example: $ curl ifconfig.me
91.127.181.86

More information about curl on terminal


$ man curl
$ curl --help
Advanced information
An external IP address is the address assigned to you by your ISP (Internet service
provider) that the Internet and all other computers outside your local network use
to identify you.

©HM 22
HM LINUX SERIES 2020 25 TIPS

19. ss command
To quickly check all ports in use, run the following command:
$ ss -a
Example: $ ss -a | less

Netid State     Recv-Q Send-Q                                                       Local Address:Port                                    Peer Address:Port   Process


nl    UNCONN    0      0                                                                     rtnl:avahi-daemon/451                                    *               
nl    UNCONN    0      0                                                                     rtnl:mission-control/706                                 *               
nl    UNCONN    0      0                                                                     rtnl:-402973449                                          *               
nl    UNCONN    0      0                                                                     rtnl:firefox/20866                                       *               
nl    UNCONN    0      0                                                                     rtnl:wpa_supplicant/455                                  *               
nl    UNCONN    0      0                                                                     rtnl:kernel                                              *               
nl    UNCONN    0      0                                                                     rtnl:NetworkManager/453                                  *               
nl    UNCONN    0      0                                                                     rtnl:firefox/20866                                       *               
nl    UNCONN    0      0                                                                     rtnl:mission-control/706                                 *               
nl    UNCONN    0      0                                                                     rtnl:-402973449                                          *               
nl    UNCONN    0      0                                                                     rtnl:wpa_supplicant/455                                  *               
nl    UNCONN    0      0                                                                     rtnl:NetworkManager/453                                  *               
nl    UNCONN    0      0                                                                     rtnl:avahi-daemon/451                                    *               
nl    UNCONN    768    0                                                                  tcpdiag:kernel                                              *               

nl    UNCONN    4352   0                                                                  tcpdiag:ss/23240                                            *              

More information about ss on terminal


$ man ss
$ ss --help
Advanced information
Most useful options:

https://www.howtoforge.com/linux-ss-command/

©HM 23
HM LINUX SERIES 2020 25 TIPS

20. Ctrl + L Key Combination


To quickly clear your terminal, use the key combination :
Ctrl + L

Advanced information
Other key combinations to use with the terminal:

https://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-
command-shell-for-ubuntu-debian-suse-redhat-linux-etc/

©HM 24
HM LINUX SERIES 2020 25 TIPS

21. ping command


To check whether your Ethernet or WiFi interface is working or not, use the
command:
$ ping ip-address or website -c n (n=1, 2, 3,...)
Examples:
$ ip 192.168.1.1 -c 3

$ ping archlinux.org – c 2

More information about ping on terminal


$ man ping
$ ping --help
Advanced information
Most useful options
https://www.geeksforgeeks.org/ping-command-in-linux-with-examples/

©HM 25
HM LINUX SERIES 2020 25 TIPS

22. uname command


To check the release and name of your kernel run the following command:
$ uname -rs
Example
$ uname -rs

More information about uname on terminal


$ man uname
$ uname --help
Advanced information
Most useful options
https://www.geeksforgeeks.org/uname-command-in-linux-with-examples/

©HM 26
HM LINUX SERIES 2020 25 TIPS

23. rfkill command


To list the current state of wireless devices on a laptop, run the following command:
$ rfkill
Example
$ rfkill

More information about rfkill on terminal


$ man rfkill
$ rfkill --help
Advanced information
The default output is subject to change. So whenever possible, you should avoid
using default outputs in your scripts. It’s recommended to always explicitly define
expected columns by using the --output option together with a columns list in
environments where a stable output is required.

©HM 27
HM LINUX SERIES 2020 25 TIPS

24. systemctl command


To enable, start or check a service, use the command:
$ systemctl
To check the status of a service, run $ systemctl status name of the service.service
(the “.service” is optional). You will be prompted to enter your password.
Example:
$ systemctl status ufw

To enable a service, run $ systemctl enable name of the service.service


(the “.service” is optional). You will be prompted to enter your password.
Example:
$ systemctl enable ufw

To start a service, run $ systemctl start name of the service.service


(the “.service” is optional). You will be prompted to enter your password.

TIP: to enable AND start a service, $ systemctl --now enable name of the
service.service (the “.service” is optional). You will be prompted to enter your
password.

More information about systemctl on terminal


$ man systemctl
$ systemctl--help
Advanced information
How to create a service?
https://medium.com/@benmorel/creating-a-linux-service-with-systemd-
611b5c8b91d6

©HM 28
HM LINUX SERIES 2020 25 TIPS

25. dd command
To quickly create a bootable flash-drive
$ sudo dd bs=4M if=/path/to/input.iso of=/dev/sd? oflag=sync
status=progress
Where:
input.iso is the input file (the .iso file you want to make bootable).
? is the letter of the USB flash drive you want to write on.
WARNING: run lsblk to view all drives in order to identify what ? Is the USB flash
drive you want to write on.

Example, assuming your USB flash drive is sdc and the iso file, archiso, is located in
your Downloads folder.
$ sudo dd bs=4M if ~/Downloads/archlinux-2020.05.01-x86_64.iso of=/dev/sdc
oflag=sync status=progress

More information about dd on terminal


$ man dd
$ dd--help
Advanced information
https://en.wikipedia.org/wiki/Dd_(Unix)

©HM 29

You might also like