DEV Community

Rubens Barbosa
Rubens Barbosa

Posted on

A Bunch of Linux Commands

πŸš€ Summary

  • Linux Overview
  • System Information
  • Files & Directory
  • Compress & Extract Files
  • Process Management
  • File Permission
  • Network

πŸ–₯ Linux Overview

username@system_name:~$

The tilde (~) symbol stands for your home directory
Directory Function
/ begins the file system, called root
/home contains users home directory
/bin all the standard commands and utility programs i.e. executable binaries such as cat, cp, ls, mv, ps, rm
/usr holds those files and commands used by the system
/var files that are expected to change in size and content (var stands for variable), such as mailbox files
/dev file interfaces for devices such as the terminals and printers
/etc is the home for system configuration files and any other system files
/boot contains the few essential files needed to boot the system
/lib contains libraries (common code shared by applications and needed for them to run)
/mnt it has been used since the early days of UNIX for temporarily mounting filesystems
/opt optional application software packages
/tmp temporary files; on some distributions erased across a reboot and/or may actually be a ramdisk in memory
/sys virtual pseudo-filesystem giving information about the system and the hardware. Can be used to alter system parameters and for debugging purposes

creating user account

$ sudo useradd -m -c "Rubens Barbosa" -s /bin/bash rubnsbarbosa
$ sudo passwd rubnsbarbosa
Enter fullscreen mode Exit fullscreen mode

look if it was well created

$ grep rubnsbarbosa /etc/passwd /etc/group
Enter fullscreen mode Exit fullscreen mode

connect to the new user

$ shh rubnsbarbosa@localhost
Enter fullscreen mode Exit fullscreen mode

remove the new user

$ sudo userdel -r rubnsbarbosa
Enter fullscreen mode Exit fullscreen mode

to check all users

$ ls -l /home
Enter fullscreen mode Exit fullscreen mode

Keyboard Shortcuts

keyboard shortcut task
tab auto-completes files, directories, and binaries
ctrl + l clear the screen
ctrl + a goes to the beginning of the line
ctrl + e goes to the end of the line
ctrl + d exits the current shell
ctrl + z puts the current process into suspended background
ctrl + c kill the current process
ctrl + h works the same as backspace
ctrl + w deletes the word before the cursor
ctrl + u deletes from beginning of line to cursor position

🧐 System Information

the man pages, which are manuals for linux commands available from the Command Line Interface CLI

$ man ls  
$ man mkdir  
$ man rm
$ man grep
$ man patch
$ man diff
Enter fullscreen mode Exit fullscreen mode

display one line manual page description of a command

$ whatis top
$ whatis mv
$ whatis nice
Enter fullscreen mode Exit fullscreen mode

display full path of commands where given commands reside

$ which top
$ which grep
$ which nice
Enter fullscreen mode Exit fullscreen mode

locate the binary, source, and manual page files for a command

$ whereis top
$ whereis grep
$ whereis nice
Enter fullscreen mode Exit fullscreen mode

tell us your username

$ whoami
Enter fullscreen mode Exit fullscreen mode

show the current date and time

$ date
Enter fullscreen mode Exit fullscreen mode

show this month's calendar

$ cal
Enter fullscreen mode Exit fullscreen mode

show current uptime

$ uptime
Enter fullscreen mode Exit fullscreen mode

tell us detailed information about the machine name, operating system and kernel

$ uname -a
Enter fullscreen mode Exit fullscreen mode

display CPU information

$ cat /proc/cpuinfo
Enter fullscreen mode Exit fullscreen mode

display memory information

$ cat /proc/meminfo
Enter fullscreen mode Exit fullscreen mode

show the disk usage

$ df
Enter fullscreen mode Exit fullscreen mode

show directory usage space

$ du
Enter fullscreen mode Exit fullscreen mode

display memory and swap usage

$ free
Enter fullscreen mode Exit fullscreen mode

display memory and swap usage in human format

$ free -h
Enter fullscreen mode Exit fullscreen mode

βš’οΈ Files & Directory

clear terminal

$ clear  
$ ctrl + l
Enter fullscreen mode Exit fullscreen mode

list of files and/or directories contents

$ ls
Enter fullscreen mode Exit fullscreen mode

list content of current directory (display attributes such as owner, group owner, permissions)

$ ls -l
Enter fullscreen mode Exit fullscreen mode

list hidden files and/or directories (hidden file begins with . dot sign)

$ ls -a
Enter fullscreen mode Exit fullscreen mode

list everything of current directory (attributes such as owner, group owner, permissions) + hidden files

$ ls -la
$ ls -al
Enter fullscreen mode Exit fullscreen mode

to know in which directory you're located (pwd stands for "print working directory")

$ pwd
Enter fullscreen mode Exit fullscreen mode

to make directories e.g. mkdir foo will create a new directory or folder called "foo"

$ mkdir foo
Enter fullscreen mode Exit fullscreen mode

to create any number of directories/folders simultaneously

$ mkdir foo bar foobar
Enter fullscreen mode Exit fullscreen mode

to remove or delete an empty directory/folder

$ rmdir foo
Enter fullscreen mode Exit fullscreen mode

to remove or delete a file in your directory entries

$ rm
Enter fullscreen mode Exit fullscreen mode

to remove or delete a directory and all of its contents recursively [f = force]

$ rm -r foo
$ rm -rf foo 
Enter fullscreen mode Exit fullscreen mode

to change directories (moving through the file system)

$ cd
Enter fullscreen mode Exit fullscreen mode

to navigate into the root directory

$ cd /
Enter fullscreen mode Exit fullscreen mode

to navigate to your home directory, use "cd"

$ cd
$ cd ~
$ cd $HOME
$ cd /home_path/
Enter fullscreen mode Exit fullscreen mode

to navigate up one directory level

$ cd ..
Enter fullscreen mode Exit fullscreen mode

to navigate into the documents directory

$ cd Documents/
Enter fullscreen mode Exit fullscreen mode

to navigate into the documents directory and see which files and/or directories exists there

$ cd Documents/{press tab twice}
Enter fullscreen mode Exit fullscreen mode

to navigate into the directory which contains space in their names

$ cd 'best songs ever'
$ cd best\ songs\ ever
Enter fullscreen mode Exit fullscreen mode

to create a new empty file

$ touch file_name.txt
Enter fullscreen mode Exit fullscreen mode

to create multiples empty files

$ touch foo.txt bar.txt foobar.txt
Enter fullscreen mode Exit fullscreen mode

to execute several commands on the same line by separating them with a semicolon ;

$ ls ; date
Enter fullscreen mode Exit fullscreen mode

to see the contents of a file (-n parameter shows the number of lines in file)

$ cat main.py
$ cat passwd.txt
$ cat -n song.txt 
Enter fullscreen mode Exit fullscreen mode

displays only the first 10 lines of the file

$ head foo.txt
Enter fullscreen mode Exit fullscreen mode

prints the first -n 'num' lines instead of first 10 lines

$ head -n 5 foo.txt
Enter fullscreen mode Exit fullscreen mode

display only the last 10 lines of the file

$ tail foo.txt
Enter fullscreen mode Exit fullscreen mode

prints the last -n 'num' lines instead of last 10 lines

$ tail -n 3 foo.txt
Enter fullscreen mode Exit fullscreen mode

to move a file to a different location

$ mv [source-file] [destination-file]

$ mv foo.txt /home/ubuntu/script/
Enter fullscreen mode Exit fullscreen mode

to move a file to home directory (the terminal use the ~ shortcut to your home directory)

$ mv foo.txt ~ 
$ mv foo.txt /home/ubuntu/
Enter fullscreen mode Exit fullscreen mode

to copy a file from the current directory to a different one, the command below will make an exact copy of "foo.txt" file

$ cp [source-file] [destination-file]

$ cp foo.txt /home/ubuntu/script/
Enter fullscreen mode Exit fullscreen mode

to copy a directory, use "cp -r directory name" (-r recursively = to copy the directory and all its files and subdirectories and all their files and so on)

$ cp -r bar /home/ubuntu/script/
$ cp -r 'best songs ever' /home/ubuntu/music/
Enter fullscreen mode Exit fullscreen mode

to copy or move all your C or Python source code files to a given directory

$ cp *.c algorithms
$ mv *.py algorithms
Enter fullscreen mode Exit fullscreen mode

to find a file in a current working directory

$ find . -name hello_world.py
$ find . -name hello_world.py -print
Enter fullscreen mode Exit fullscreen mode

to find all files with the .py extension in the script directory

$ find script -name '*.py' -ls
Enter fullscreen mode Exit fullscreen mode

to locate other directories e.g. the command below will locate the script directory

$ find /home/ubuntu -name script -type d -print
Enter fullscreen mode Exit fullscreen mode

to find text in a file i.e. the command will search through the file to find a piece of text which you are looking for

$ grep 'Hello' hello_world.py
Enter fullscreen mode Exit fullscreen mode

to print the history of a long list of executed commands in the terminal

$ history
Enter fullscreen mode Exit fullscreen mode

reboots the system

$ reboot
Enter fullscreen mode Exit fullscreen mode

shuts down the system

$ shutdown
Enter fullscreen mode Exit fullscreen mode

shuts down the system by powering off

$ poweroff
Enter fullscreen mode Exit fullscreen mode

brings the system down immediately

$ halt
Enter fullscreen mode Exit fullscreen mode

reboots the system by shutting it down completely and then restarting it

$ init 6
Enter fullscreen mode Exit fullscreen mode

powers off the system using predefined scripts to synchronize and clean up the system prior to shutting down

$ init 0
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Compress & Extract Files

Tar utility creates archives for files and directories, and was originally designed to create archieves on tapes (the term "tar" stands for tape archive). The tar utility is ideal for making backups of your files, which can then be transferred over the Internet

Archives using tar

Syntax:

tar [options] [archive-name.tar] [directory-or-file-name]

Options Function
-c creates a new archive
-x extract the archive
-f specify an archive filename
-v verbosely display the .tar progress in the terminal
-t lists files in archived file
-r appends files to an archive
-u updates an archive with new files
-w waits for a confirmation from the user before archiving each file
-z creates archived file using gzip
-j creates archived file using bzip

create a tar archive using option -cvf

$ tar -cvf foo-archive.tar foo.txt
Enter fullscreen mode Exit fullscreen mode

extract file from archive using option -xvf

$ tar -xvf foo-archive.tar
Enter fullscreen mode Exit fullscreen mode

create a gzip tar archive using option -cvzf

$ tar -cvzf foo-archive.tar.gz foo.txt
Enter fullscreen mode Exit fullscreen mode

extract a gzip tar archive using option -xvzf

$ tar -xvzf foo-archive.tar.gz
Enter fullscreen mode Exit fullscreen mode

create a gzip tar archive with python files

$ tar -cvzf python-codes.tar.gz *.py
Enter fullscreen mode Exit fullscreen mode

create a gzip tar archive file for a directory

$ tar -cvzf images-august-2021.tar.gz /home/ubuntu/images/
Enter fullscreen mode Exit fullscreen mode

create tar with bzip2 compression

$ tar -cvjf foo-archive.tar.bz2 foo.txt
Enter fullscreen mode Exit fullscreen mode

extract a tar using bzip2

$ tar -xvjf foo-archive.tar.bz2
Enter fullscreen mode Exit fullscreen mode

compress to file.gz

$ gzip foo-archive
Enter fullscreen mode Exit fullscreen mode

decompress file.gz

$ gzip -d foo-archive.gz
Enter fullscreen mode Exit fullscreen mode

πŸ‘¨β€πŸ’» Process Management

A process, in simple terms, is an instance of a running program. Whenever we execute a command in Linux it starts, or creates a new process. The Linux kernel tracks processes through an ID number known as PID which means Process ID. The kernel is a part & core of the Operating System OS, it's closer to the hardware i.e. is the lowest level of the OS. The Operating System is the software package which contains applications like the user interface (shell, gui, tools, etc) and communicates directly to the hardware and our application. The kernel is the main part of the Operating System and is responsible for translating the command into something that can be understood by the computer. Basically the Kernel is the layer between hardware (devices which are available in computer) and software (applications like gedit). Only Kernel provides low level services such as:

  1. memory management
  2. network management
  3. device driver
  4. file management
  5. process management

Types of Processes

When we create a new process (run a command), there are two types:

  • Foreground Processes: They run on the screen and need input from the user. For example Office Programs
  • Background Processes: They run in the background and usually do not need user input. For example Antivirus.

to display the currently working processes

$ ps
Enter fullscreen mode Exit fullscreen mode

all the currently running processes

$ ps -A
Enter fullscreen mode Exit fullscreen mode

processes associated with the current terminal session

$ ps -T
Enter fullscreen mode Exit fullscreen mode

to check all the processes associated with a particular User

$ ps -u rubnsbarbosa
Enter fullscreen mode Exit fullscreen mode

to check all the processes running under a user

$ ps ux
Enter fullscreen mode Exit fullscreen mode

to check all the processes associated with a particular user group

$ ps -fG root
Enter fullscreen mode Exit fullscreen mode

to display the processes running with full information

$ ps -f
Enter fullscreen mode Exit fullscreen mode

to display the processes running on the system in the form of a tree

$ pstree
Enter fullscreen mode Exit fullscreen mode

description of all the fields displayed by ps -f command

Column Description
UID user id that this process belongs to
PID process id
PPID parent process id (the id of the process that started it
C CPU utilization process
STIME process start time
TIY terminal type associated with the process
TIME CPU time taken by the process
CMD The command that started this process

display all running Linux processes

$ top
Enter fullscreen mode Exit fullscreen mode

interactive process viewer

$ htop
Enter fullscreen mode Exit fullscreen mode

display list of kill

$ kill -l
Enter fullscreen mode Exit fullscreen mode

kill the process with given pid (for example: I want to kill this 217956 PID)

$ kill 217956
Enter fullscreen mode Exit fullscreen mode

if a process ignore a regular kill command, we can use kill -9 followed by the PID

$ kill -9 217956
Enter fullscreen mode Exit fullscreen mode

to find the PID of a process

$ pidof Photoshop.exe
Enter fullscreen mode Exit fullscreen mode

kill all the process named proc

$ killal proc
Enter fullscreen mode Exit fullscreen mode

will kill all processes matching the pattern

pkill pattern
Enter fullscreen mode Exit fullscreen mode

to check the nice value of a process (example: I'd like to find terminal value name)

$ ps -el | grep terminal
Enter fullscreen mode Exit fullscreen mode

run a program with modified scheduling priority i.e. set processes CPU priority. Kernel will allocate more CPU time to that process

$ nice -10 gnome-terminal
Enter fullscreen mode Exit fullscreen mode

changing priority of running processes with PID 77982

$ renice -n 15 -p 77982
Enter fullscreen mode Exit fullscreen mode

change the priority of all programs of a specific group

$ renice -n 10 -g 4
Enter fullscreen mode Exit fullscreen mode

suspend process running in foreground

$ ctrl+z
Enter fullscreen mode Exit fullscreen mode

list jobs table

$ jobs
Enter fullscreen mode Exit fullscreen mode

send stopped process to background

$ bg [job-num]
Enter fullscreen mode Exit fullscreen mode

brings process to foreground

$ fg [job-num]
Enter fullscreen mode Exit fullscreen mode

πŸ“ File Permission

Every file or directory within Linux has a set of permissions that control who may read, write and execute the contents. In Linux Linux, a directory is just a special type of file. File ownership is an important component of Unix which provides a secure method for storing files. Every file in Linux has the following category:

  • Owner βˆ’ the name of the user that owns the file/directory;
  • Group βˆ’ the name of the group that has permissions on the file/directory;
  • Other βˆ’ the name of all other users can perform on the file/directory.

Permissions

Every file and directory in your Linux has following 3 permissions defined for all the 3 category discussed above.

Permission Abbreviation File Directory Octal Value
read r able to view the contents of a file able to list the files within the directory 4
write w able to modify the contents of a file able to add/delete files to/from directory 2
execute x able to run the file as an executable able to cd into the directory and access files 1

Octal Notation Table

Octal Decimal Permission Representation
000 (0+0+0) = 0 no permission ---
001 (0+0+1) = 1 execute --x
010 (0+2+0) = 2 write -w-
011 (0+2+1) = 3 write + execute -wx
100 (4+0+0) = 4 read r--
101 (4+0+1) = 5 read + execute r-x
110 (4+2+0) = 6 read + write rw-
111 (4+2+1) = 7 read + write + execute rwx

Whenever using ls -l command, it display informations related to file permission as follows

  • The first character is called the file type. An ordinary file is represented by a dash (-) and a directory is represented by a d.

    Note: A dash (-) anywhere else in the permission set indicates no permission.

  • The 1st set of three characters are the users permissions in green.

  • The 2nd set of characters are the group permissions in cyan.

  • The 3rd set of characters are the permissions for all other users in red.

The File permissions that are set depend on the type of file e.g. a text file has different permissions to a shell script because a text file doesn’t need the executable permission but a shell script does.

examples of different types of permissions on files and directories:

-rwx------ this file is read/write/execute for the owner only
dr-xr-x--- this directory is read/execute for the owner and the group
-rwxr-xr-x this file is read/write/execute for the owner, and read/execute for the group and others
Enter fullscreen mode Exit fullscreen mode

Setting Permission

In order to change file permissions we use chmod command (change mode - changes permissions of a given file) followed by the octal values that reflect the permissions we want to set. To decide on the permissions:

  1. work out what you want each category of user to be able to do and the appropriate octal value for this (see Octal Notation Table);
  2. take these 3 octal values and put them together to form a set which will be the permissions for that file.

The example below shows that if we want a user to be able to read and write to a file but the group and other to only be able to read that file then the permissions for this file would need to be set to 644

category u g o
permission r w r r
value 4 + 2 4 4
total 6 4 4

more examples:

  • chmod 755 foo.txt (results in rwxr-xr-x)
  • chmod 666 foo.txt (results in rw-rw-rw-)
  • chmod 664 foo.txt (results in rw-rw-r--)
  • chmod 700 foo.txt (results in rwx------)
  • chmod 711 foo.txt (results in rwx--x--x)
  • chmod 754 foo.txt (results in rwxr-xr--)
  • chmod 755 foo.txt (results in rwxr-xr-x)
  • chmod 000 foo.txt (results in ---------)
  • chmod 777 foo.txt (results in rwxrwxrwx)
  • chmod g+r foo.txt (adds read to group)
  • chmod g-r foo.txt (removes read to group)
  • chmod o+r foo.txt (adds read to others)
  • chmod a-w foo.txt (removes write from all users)

Using chmod in symbolic mode

Chmod operator Description
+ adds a permission to a file/directory
- removes the permission from a file/directory
= sets the designated permission(s)

a shell script or any other which needs to be executable should have a permission 711

$ chmod 711 foobar.sh

owner - read, write and execute
group - execute
other - execute
Enter fullscreen mode Exit fullscreen mode

a text file doesn't need to be executable, it should have a permission 644

$ chmod 644 foo.txt

owner - read and write
group - read
other - read
Enter fullscreen mode Exit fullscreen mode

⚑️ Network

Let’s start with the most basic question: is our physical interface up? The ip link show command tells us

# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
link/ether 52:54:00:82:d6:6e brd ff:ff:ff:ff:ff:ff
Enter fullscreen mode Exit fullscreen mode

your interface might be disable, so before check cables you should bring the interface up

# ip link set eth0 up
Enter fullscreen mode Exit fullscreen mode

this prints output in a much more readable table format

# ip -br link show
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
eth0 UP 52:54:00:82:d6:6e <BROADCAST,MULTICAST,UP,LOWER_UP>
Enter fullscreen mode Exit fullscreen mode

we can use the -s flag with the ip command to print additional statistics about an interface

# ip -s link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 52:54:00:82:d6:6e brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
34107919 5808 0 6 0 0
TX: bytes packets errors dropped carrier collsns
434573 4487 0 0 0 0
Enter fullscreen mode Exit fullscreen mode

We can check the entries in our ARP table with the ip neighbor command:

# ip neighbor show
192.168.122.1 dev eth0 lladdr 52:54:00:11:23:84 REACHABLE
Enter fullscreen mode Exit fullscreen mode

Note that the gateway’s MAC address is populated (we’ll talk more about how to find your gateway in the next section). If there was a problem with ARP, then we would see a resolution failure:

# ip neighbor show
192.168.122.1 dev eth0 FAILED
Enter fullscreen mode Exit fullscreen mode

Linux caches the ARP entry for a period of time, so you may not be able to send traffic to your default gateway until the ARP entry for your gateway times out. For highly important systems, this result is undesirable. Luckily, you can manually delete an ARP entry, which will force a new ARP discovery process:

# ip neighbor show
192.168.122.170 dev eth0 lladdr 52:54:00:04:2c:5d REACHABLE
192.168.122.1 dev eth0 lladdr 52:54:00:11:23:84 REACHABLE
# ip neighbor delete 192.168.122.170 dev eth0
# ip neighbor show
192.168.122.1 dev eth0 lladdr 52:54:00:11:23:84 REACHABLE
Enter fullscreen mode Exit fullscreen mode
# ip -br address show
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0 UP 192.168.122.135/24 fe80::184e:a34d:1d37:441a/64 fe80::c52f:d96e:a4a2:743/64
Enter fullscreen mode Exit fullscreen mode
# ping www.google.com
PING www.google.com (172.217.165.4) 56(84) bytes of data.
64 bytes from yyz12s06-in-f4.1e100.net (172.217.165.4): icmp_seq=1 ttl=54 time=12.5 ms
64 bytes from yyz12s06-in-f4.1e100.net (172.217.165.4): icmp_seq=2 ttl=54 time=12.6 ms
64 bytes from yyz12s06-in-f4.1e100.net (172.217.165.4): icmp_seq=3 ttl=54 time=12.5 ms
^C
--- www.google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 12.527/12.567/12.615/0.036 ms
Enter fullscreen mode Exit fullscreen mode

We can print the routing table using the ip route show command:

# ip route show
default via 192.168.122.1 dev eth0 proto dhcp metric 100
192.168.122.0/24 dev eth0 proto kernel scope link src 192.168.122.135 metric 100
Enter fullscreen mode Exit fullscreen mode

SSH

$ ssh admin@192.168.1.113
$ ssh ubuntu@192.168.1.113
Enter fullscreen mode Exit fullscreen mode

ssh key

$ ssh-key
$ ssh-keygen -t rsa
Enter fullscreen mode Exit fullscreen mode

ssh authentication - you store your public key on your server and you have your private key with you, and the private key is the most important is the key that will allow you to authenticate successfully with the server. So you need to keep it secure, if you lose it you will lose access to the server.

copy file from remote server

$ scp foo.txt admin@192.168.1.113:/home/admin/
Enter fullscreen mode Exit fullscreen mode

display the current network interface configuration information

# ifconfig
Enter fullscreen mode Exit fullscreen mode

display IP addresses and property information

# id addr
Enter fullscreen mode Exit fullscreen mode

List all of the route entries in the kernel

# ip route
Enter fullscreen mode Exit fullscreen mode

display neighbour objects; also known as the ARP table for IPv4

# ip neigh
Enter fullscreen mode Exit fullscreen mode

lists all my connections and their DNS servers

# systemd-resolve --status
Enter fullscreen mode Exit fullscreen mode

allows you to test the IP-level connectivity of a given host on the network

# 192.168.2.32
Enter fullscreen mode Exit fullscreen mode

display the route that a packet takes to reach the host; also prints detail about all the hops that it visits

# traceroute google.com
# traceroute 172.217.26.206
Enter fullscreen mode Exit fullscreen mode

Top comments (0)