Skip to content

Linux

In this page you'll find Linux commands and other useful information.


CommandWhat does it do?
cat /etc/*release*Check system version
ls -ltrh /dev/video*List all video devices picked up by the kernel
lsusbList all devices attached with USB
lspciList all devices attached with PCI
topDisplays real time view of performance related data of all running processes in a system
htopGraphical top. Shows usage of CPU and Ram
uptimeShows the uptime and the load-average of the system
sudo pkill <service>Kill the service with the name or PID
clearClear commandline
which python3Which will tell where the file/program is in the system. If it Exist
historyShows the history of the commands in the command line
!40Rerun the command with ID 40 without the need to rewrite the command
!-2Rerun the 2-nd previous command
!pRerun the command that starts with the letter (python)
!!Rerun the previous command
sudo !!Rerun the previous command with sudo
^helllo^helloRerun the command with fixed grammar
systemctl status apache2Checking the services that are running on the machine
sudo systemctl disable apache2Removing the "Loaded" so the process does not start with the machine
sudo systemctl enable apache2Adding "Loaded" so the process does starts with the machine
sudo systemctl stop apache2This command will stop the process
sudo systemctl start apache2This command will run the process
sudo systemctl restart apache2This command will restart the process
tail -n 4 /var/log/syslogViewing only the last 4 rows of logs
head -n 4 /var/log/syslogViewing only the first 4 rows of logs
jouurnalctl -u apache2View the logs only of the apache2
jouurnalctl -fu apache2View the logs in real time only of the apache2
<command> > file.txtOverwrites the output to the file
<command> >> file.txtAppends the output to the file
ls -l > file.txtSaving the ls -l command output to the file.txt
``
`ls -lgrep file`
`cat file.txtsort
grepgrep limits the search criteria
grep -v file.txtDon't grep rows that contain the word file.txt ( -v is the same as not in python)

status can be:

  • Loaded: Enabled #Start when the machine is started.
  • Active (running) #Telling us that the process is running.
  • Active Time #We can check when did the process started

2. Resource Management Commands


CommandWhat does it do?
freeTells us the amount of Memory used by the system
free -mThe same but in MB
dfMeans disc free. Tells us how much diskspace is being used
df -hMakes it better and easier to read. "h" is human-readible.

"free" memory is the memory that is not used at all by the system. "available" memory is the memory that exist but reserved for caches/system logs and stuff.

It's weird but Linux has a finite number of files. So we can have free disk space but if the nr.files is full we can't add more. These are called i-nodes.


3. File System Commands


CommandWhat does it do?
pwdPrint Working Directory. Tell where you are
.Current Directory
lsList Files and Folders in the working Dir
ls -lGive Long Listing
ls -aList Everything. Even Hidden Files ("." before the name)
cd <dir_name>Change Directory to the specified one
cd ..Go Above 1 Dir
cd -Go to the Previous Dir
cd /Go to Root Folder
cd /home/<user_name>Go to the Home Directory. Where Everything is. Like C-Drive
cd ~Go to the Home Directory. Same as the above command
mkdir <folder_name>Make a new Directory
touch <file_name>.<extention>Create a file If it doesn't Exist
cat > <file_name>.<extention>Create a file from cat and write to it. CTR + C for closing it
echo "" > <file_name>.<extention>Create a file
cat <file_name>.<extention>Open file to check it
cp <file_name>.<extention> <file_name_2>.<extention>cp stands for copy
rm test.txtrm stands for remove. If you use rm it will remove the file from the system. You can't get it back
mv test.txt inimv stands for move. File to the Directory
mv *txt iniMove all the files ending with txt to the Directory
mv *txt ..Move all the txt files to the Dir above
mv ../test.txt .2 dots = parent Directory. 1 dot = current Directory. Move the File from the parent to the current Dir
mv test1.txt test2.txmv is still used to rename files. Be careful with this. May overwrite without permission
scp * <user>@<ip_address>:~/Secure copy all files from a Dir to another device at it's Home Dir
scp -P <port_nr> <user>@ip_address:~/Secure copy files from a device to another at remote specified port
mv <file_name>.<extention> <dir_path>Move files from a directory to another
mv <file_name>.<extention> <file_name_2>.<extention>Rename files
nano test.txtOpen nano = text editor to edit the test.txt file. Press "control + o" to save the edit
cat test.txtWill show the content of the file
`cat test.txthead - 2`
echo $?Special variable. Returns 0 if the last command was succesful or not 0 if the last command was with error
find / -name *.logFind everyfile in the root-folder. Search by name that ends with .log
find tes*Find files that start with "tes"
find *tes*Find files that has "tes" somewhere in the name

4. Network Commands


CommandWhat does it do?
ssh <user>@<ip_address>Connect with SSH client with specified user
`sudo lsof -i -P -ngrep LISTEN`
`sudo netstat -apgrep <port_nr>`
ping -I wlan0 time.google.comPing with defined network interface. Here wlan0

5. Shortcut Commands


CommandWhat does it do?
CTRL + ZShunts the process into the background, suspended
CTRL + CInterrupt (kill) the current foreground process running in in the terminal
CTRL + DClose the bash shell. Similar to exit
CTRL + LClear the screen. This is similar to running the “clear” command
CTRL + A or HomeGo to the beginning of the line
CTRL + E or EndGo to the end of the line
CTRL + ALT + (UP or Down)Go to the other Workspace in Ubuntu

6. User management


Root is the user with the most proviledge in the System. Permission string that we get when we press ls -l

BeginningGroupsMeaning
-rwxrwxr-x4 Sections(groups of 3 letters)d = directory or - =file
rwx = user. Which user has the file
rwx = group
r-x = the others that are not in the group
r = read access
w = write access
x = execute access
- = No access/permission

This command shows us all the users:

sh
~$ cat /etc/passwd
:shows ini : x : 1000: 1000 : /home/ini: /bin/bash   #X is for the password.
                                                     #The numbers are the group ID and user ID
CommandWhat does it do?
sudo cat /etc/shadowShow the password-Hash saved in the system
sudo !!!! are used to rewrite the code that was used before
groupsshows all the groups
groups inishows the group ini is part of
adduser batmanAdding the user with the name batman
sudo - batmanLogging in into the new user
logoutLogging out from the user
sudo su - batmanLogging in into the new user without the need for the password
passwdChanging the password of the user
sudo su -Change to root. Do what ever you want
sudo userdel -r batmanRemoving the user with the name batman
sudo groupadd heroesCreating a group in the system
sudo usermod -aG heroes iniAdding the used ini to the group heroes. (logout and login to work)
sudo gpasswd - d ini heroesRemoving a user from a group
sudo groupdel heroesRemoving a group

7. MISC

spd-say command can be a nifty tool.

Use this tool, for the system to say something or to inform you that a task finished:

sh
~$ sudo apt-get update; spd-say "Task Finished"

install gnome-tweak tool [sudo apt install gnome-tweak-tool] you can actually choose between coherent or independent behavior of your multiple monitors across workspaces. Once installed, launch tweak and then click on workspaces. then choose " Workspaces span displays" (instead of the default choice "Workspaces on primary display only" !)

\time #time option doesn't work #time is not a builtin; I mean it's not treated as a command; it's a keyword. #That's why tricks like \time

Streams in Linux: Input, Output and Error Streams

stdin (standart input) Input: stdout (standart output) Output: Every thing that returns info from the terminal is a standart Output Stream stderr (standart error) Error : >> ls ini3 #cannot access. #This is Error stream

Bash Alias

To create Bash aliases we do the following

sh
~$ alias <alias_name>="<command>"

#Examples

~$ alias ll="ls -lh"                              #Better List 
~$ alias lt="ls -h --size -1 -S --classify"       #List files by size
~$ alias myip="curl ipinfo.io/ip"                 #Get Public IP

To make this alias permanent we need to modify the ./bashrc file.

sh
~$ nano ~/.bashrc

After adding the alias in the file and saving the changes we close the file and run:

sh
~$ source ~/.bashrc

Extra Tools and Programs:

vim

vim is a text editor like nano. vim works with "modes". Normally is the command mode.

CommandWhat does it do?
:qExit vim
:q!Exit vim without saving changes
:xExit vim saving changes
:wqExit vim saving changes, same as x
iEnter the Insert Mode
:wSave the file
:w test.txtSave a created file
SHIFT + aEnter Insert Mode and jump to the end of the Line
oCreate a new line
0Go to the beginning of the line
$Go to the end of the line

When we are in Command Mode

CommandWhat does it do?
xDelete a Characters one at a time
ddDelete a Line
3ddDelete 3 Lines
.Redo the same command u used before
/textFind the first use of text string in the file
nCheck the next time it was used
Shift + nCheck the previous used case of the string
:%s/string/string2Replace the string with the other string
uUndo the last change
u then CTRL + rUndo the previous changes
30 then SHIFT + GGo to line number 30

Screen

If you run a long running process or task on a machine that you are connected with SSH, and the connection drops, we lose the console we were connected to and the work with it. To be able to leave the task running, even if we lose connection, we can use the tool screen. Screen uses "virtual separate terminals". Even if we lose connection we can still attach to the session terminal.

To install screen we run:

sh
~$ sudo apt-get install screen

To use screen, the most useful commands are:

sh
~$ screen -S <name_screen_session>   # Create a new screen terminal with a given name
~$ screen -ls                        # List all the screen terminals currently running
< CTRL + a + d on_keyboard >         # Detach from screen terminal. Leave it running on background
~$ screen -r <id_screen>             # Reattach to the screen terminal specified
~$ screen -d <id_screen>             # Detach to the screen terminal specified

watch

watch is a very useful command in Linux to check the result of a command for regular interval times. We use it when we want to check the command output and how it changes with time. In order to not write the command ourselfs many time, we use this tool.

sh
~$ watch -n 1 <linux_command>

The -n tag is used to set the time interval that the command will be run. Default is 2 seconds. In order to use watch with commands that have a pipe | we need to put all the command in brackets

sh
~$ watch -n 1 "<linux_command> | <linux_command>"

wondershaper

wondershaper is a tool that is used to limit the bandwidth on a specific network card. Install wondershaper from Github. Do not install it with sudo apt-get because it will install an older not maintained version.

CommandWhat does it do?
sudo wondershaper -c -a eth0Clean the limits from adaptor. Needs to be done everytime
sudo wondershaper -a eth0 -u 4096 -d 50000Limit Downlink and Uplink Bandwidth of Client

iperf3

iperf3 is a tool used to measure the maximum bandwidth of a link. See Iperf Documentation for more information. This tool is a CPU intensive one and the results we get are always lower than theoretical maximum speed of a network link.

CommandWhat does it do?
iperf3 -sIperf Server
iperf3 -s -DIperf Server in background as a Daemon
iperf3 -c 192.168.1.8Iperf client
iperf3 -c 192.168.1.8 -t 10Time in seconds to transmit
iperf3 -c -u 192.168.1.8Use UDP instead of TCP. (Check Documentation)
iperf3 -c 192.168.1.8 -R-R. Client in reverse test mode – Server sends, client receives

crontab

crontab is a tool used to schedule jobs in fixed time intervals. It is used to automate commands either time based or on system reboot See Crontab Documentation for more information.

To undertand more use this cool online tool Crontab Guru

Commands that need admin rights should be added to the root crontab file, that can be accessed with sudo:

sh
 ~$ crontab -e
 ~$ sudo crontab -e
# .---------------- minute       (0 - 59)
# |  .------------- hour         (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month        (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week  (0 -  6) (Sunday= 0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
CommandWhat does it do?
* * * * *Every Minute
1 * * * *At Minute 1 each Hour Day etc
5 0 * * *At 00:05 each Day etc
* * 1 * *At each Minute each Day of month 1 (January)
15 14 1 * *At 14:15 each day of January
0 22 * * 1-5At 22:00 for Monday till Friday
23 0-23/2 * * *At 00:23 01:23 02:23 each Day
5 4 * * sunAt 04:05 each Sunday
0,20,40 * * * *Every 20 minute
*/20 * * * *Every 20 minute. Same as Above

To add a new route in the route table on reboot we write the following command in the crontab file:

@reboot sleep 60 && route add -net <ip_address>/<CIDR> gw <gateway_ip> dev <device_name>

References

crontab


Created using VitePress