Skip to content

Must Know Linux commands

Do you use Linux commands on a regular basis? If it is a yes, then good for you ! You can check quickly this doc, on the other hand if it is a no welcome to the amazing Linux world πŸ€—

We will look at some Linux commands you must know. The commands listed below are some of the most useful and most frequently used enjoy !

Note

Try all the command and please use as much as you can the man <some-command> to know more about the command, it will give you more details about <some-command>.

Some basics

As you know software engineering is not about knowing things by heart, nothing is more useless to know all the linux manual and commands, if have not used it once in real life. On the other hand Command-line tools often provide more speed power and flexibility than graphical interfaces by allowing quick navigation, file manipulations and others, making tasks more efficient.

Understanding the Linux world will help you troubleshoot issues, analyze system performance, and understand the deployment context of applications.

So let's review some basics linux command here πŸ₯·πŸΌ

ls

Lists the contents of a directory, very useful when you do not have access to a screen πŸ˜…

List all files (including hidden files) with detailed information:

ls -la

pwd

Prints the current working directory path. Where am I here ?

pwd

cd

Navigate through directories. Be carful it is very much CamelCase sensitive πŸ˜‚

Change to the home directory:

cd ~

Or change to a specific directory:

cd /var/www/html

If you do not want this problem try to install some oh my bash auto-completions plugin like a real terminal πŸ₯·πŸΌ

touch

Creates a new empty file or updates the timestamp of an existing file.

Example 1: Create a new file named example.txt :

touch example.txt

Update the timestamp of the file .env:

touch .env
Tip

All the files with a . are "masqued" files they are not visible unless you run the ls -a command

cat

Concatenates and displays the content of files. Very usefull when the file you want to manipulate very large files who are not fi tfor excel or other graphical software.

Display the contents of the file example.txt :

cat example.txt

Concatenate two files file1.txt and file2.txt and display the output on your terminal :

cat file1.txt file2.txt

echo

Displays a line of text/string that is passed as an argument.

Let's say I want to print "Hello, World!" to the terminal:

echo "Hello, World!"

Append "torch" to a file named requirements.txt :

echo "torch" >> requirements.txt

cp

Copies files and directories.

You have the right to ask why do we have to do this with command line and not a traditional graphical interface?

The answer is simple : it is faster 😎

Copy file1.txt to folder1:

cp file1.txt folder1

mkdir

Creates a new directory, cool and simple.

Create a directory named new_directory :

mkdir new_directory

Create a directory and its parent directories (if they don't exist):

mkdir -p path/to/new_directory

tree

List directories in a pretty way πŸ€“

$ tree -L 1
.
β”œβ”€β”€ dir1
β”œβ”€β”€ file1
β”œβ”€β”€ file2
└── file3

1 directory, 3 files

$ tree
.
β”œβ”€β”€ dir1
β”‚   └── file4
β”œβ”€β”€ file1
β”œβ”€β”€ file2
└── file3

1 directory, 4 files

rm

Removes files or directories, the equivalent in your graphical interface of the right click then delete πŸ–±

Delete a file named example.txt :

rm example.txt

Or recursively delete a directory and its contents files :

rm -rf directory_name

rmdir

Removes empty directories.

Remove an empty directory named empty_dir:

rmdir empty_dir

??? + note 🚧 Be carful with this command please, you can not restore deleted data after such a rookie mistake 🚧

clear

Simply clear the terminal display screen, keep it clean 🧹

ln

Creates links between files. By default, ln creates hard links. With the -s option, it creates symbolic links.

Create a hard link named link_to_file to original_file:

ln original_file link_to_file

Or create a symbolic link named symlink_to_dir to a directory original_dir :

ln -s original_dir symlink_to_dir
Tip

It can be useful when you have to work on multiple storage disks and you want to keep thing organize in one working folder πŸ€“

exit

Basically exits the current shell session πŸ˜‚

If you are in a nested shell (e.g., after using su or tmux), exit would return you to the parent shell session.

apt

The famous apt, for your information apt means Advanced Package Tool lol, it is used for managing packages on Debian and Ubuntu systems. if you have been on linux more than 1 hour you will know about updating the package list with sudo apt update

Or installing a random package, like vim (there is many situation where you will need to install vim such as in a docker container):

sudo apt install vim

System informations

Informations about your Linux system basically πŸ˜…

Note

I will not write all the outputs of every commands, because it will be very long, test it yourself 😎

lsb_release

You can check your Ubuntu version installed on your machine with the following command:

lsb_release -a

You should have this output if you are on the Ubuntu 22.04 LTS version :

No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.1 LTS
Release: 22.04
Codename: jammy

dmidecode

Extracts hardware information from the DMI (Desktop Management Interface) table. Let's check our RAM informations with the command below:

sudo dmidecode --type memory

Or list the infos about the BIOS :

sudo dmidecode -t bios

vmstat

Another great way to check your current virtual memory usage is to use the vmstat command.

vmstat 

You can also use the command cat /proc/meminfo to have access to the total amount of memory of your system, the free memory available, the memory available as well as buffers and page caches that may be used πŸ€“

lsmod

Show the status of modules in the Linux Kernel. Indeed there is situation where it is usefull, let's imagine 2s you are in a battle with nvidia driver in order to install properly you new GPU and you need to check which nvidia modules are in your kenel (yeah deep learning is fun, is for maths lovers they said πŸ˜‚)

lsmod | grep nvidia

top

Display all your Linux processes, aka which scripts are used and what is the hardware consumption of it βš™οΈ I prefer the graphical version htop

nvtop

Same thing as htop for nvidia GPUs with nice graphs in order to monitor the consumptions of your GPUs πŸ“ˆ

lscpu

To view information about your CPU, you can use the lscpu command as it shows information about your CPU architecture such as the number of CPUs, cores, CPU family model, CPU caches, threads, etc... from sysfs and /proc/cpuinfo.

lsusb

The lsusb command is used to report information about USB controllers and all the devices that are connected to them.

lspci

Check your PCI devices with the lspci command PCI devices may include usb ports, graphics cards, network adapters, etc. The lspci tool is used to generate information concerning all PCI controllers on your system plus the devices that are connected to them.

To print information about PCI devices run the following command.


Identity and Access Management (IAM)

Linux Identity and Access Management (IAM) refers to the processes and commands in Linux systems that are used to manage users and their permissions (who can do what?). It ensures that only authenticated and authorized individuals can access the resources and services within a Linux environment. This involves managing user identities, their authentication, authorization to access resources, and the policies and procedures for securely managing access to system and network resources. Some key components are :

  • User Accounts: Identifying and managing user profiles that determine what actions a user can perform on the system.
  • Authentication: Verifying the identity of a user or system, typically through passwords, keys, or multifactor authentication methods.
  • Authorization: Determining whether a user has permission to perform a specific action or access a particular resource, often implemented through file and directory permissions or access control lists (ACLs).
  • User Groups: Organizing users into groups to simplify the management of permissions and access rights.
  • PAM (Pluggable Authentication Modules): A flexible mechanism for authenticating users, allowing for a wide range of authentication technologies.
  • Sudo: A powerful utility that allows certain users to execute commands with the security privileges of another user, by default the superuser.
  • SELinux/AppArmor: Security modules that provide additional layers of security, controlling access through mandatory access control (MAC) policies.

whoami

Displays the username of the current user which is you normaly πŸ˜…

whoami

sudo

If you have been on Ubuntu, you have seen this command more than one time. Sudo is the command to executes others commands with superuser privileges more information on the official page.

Example : Update the package list

sudo apt update -y
Tip

The -y option flag stands for yes which allows the script to run without prompting the user for input. It's commonly used in scripts to automate the installation process, as it doesn't ask for confirmation or require you to accept the license agreement manually. Essentially, it assumes yes as an answer to any prompts the script might usually present. πŸ€“

useradd

Adds a new user to the system.

Create a new user named dumbo :

sudo useradd dumbo

You can also create a new user named dumbo with a home directory. The equivalent on your graphical interface of created differents sessions for your parent's computer for example πŸ§‘β€πŸŒΎ

sudo useradd -m newuser

groupadd

Similar to useradd, you can create a new user group with it. Groups are essential for managing permissions for multiple users efficiently.

Create a new group: > groupadd mygroup Create a new group with a specific GID: > groupadd -g 1010 mygroup Create a new system group: > groupadd -r sysgroup

gpasswd

Adds a user to a group. Managing group memberships is a key part of handling access permissions.

gpasswd -a username groupname

Add a user to a group: > gpasswd -a newuser mygroup Remove a user from a group: > gpasswd -d newuser mygroup Set the password for a group: > gpasswd mygroup

su

Switches the current user to another user.

Switch to user newuser :

su newuser

Or switch to the root user:

su root

chmod

Changes the file mode bits of each given file according to mode, which can be symbolic (e.g., u+rwx) or in numeric format which is the most common (e.g., 755). This command is vital for setting access permissions on files and directories.

chmod 755 file

chowwn

Changes the owner and/or group of specified files. This command is used to control who can access specific files or directories. For example you can change the owner and group of a file:

chown newowner:newgroup myfile

Or recursively change the owner of a directory and its contents :

chown -R newowner /mydirectory

passwd

Sets or changes the password for a user account. This is pretty crucial for securing user accounts with strong authentication πŸ˜…

Set a password for the user newuser :

passwd newuser

Or lock a user's password :

passwd -l newuser

semanage

Bonus point, as we mentionned earlier SELinux policy management. SELinux (Security-Enhanced Linux) adds an additional layer of security controls, and managing its policies is key for securing access to system resources.

List all SELinux user mappings:

semanage user -l

Add a port type for SELinux:

semanage port -a -t http_port_t -p tcp 8080

Editors

Text editors are very important in the Linux game, back in time people did not had graphical editors like vscode, pycharm and others... All the code was inside the terminal and every devs had they own favourite, shortcut, graphical preferences 😎

You can check this in detail inside the shells part of this course πŸ₯·πŸΌ

ssh

Secure Shell (SSH) is a protocol used to securely connect to remote machines over a network. It allows secure command-line access to a remote server.

Connect to a remote server with the username user at the IP address 192.168.1.1:

ssh user@192.168.1.1

or connect to a remote server using a specific port (e.g., 2222):

ssh -p 2222 user@192.168.1.1

nano

A command-line text editor that is included with many Linux distributions. It's easier to use for beginners compared to Vim.

Example 1: Open or create a file named example.txt: bash Copy code nano example.txt Example 2: There's essentially one primary use case for nano, so a second example would also involve opening a different file.

vim

A highly configurable text editor built to enable efficient text editing. It is an improvement over the traditional vi editor.

On one side vim use a lot of commands ind if you do not know them you will feel stuck. But one the other hand it's so much quicker than the other graphical editor and if you know your basic you look like a real πŸ₯·πŸΌ

Open or create a file named example.txt:

vim example.txt
Then tape esc + :x to save your modifications inside your file.

More on vim in this incredible tuto here :

shred

Overwrites a file to hide its contents, and optionally deletes it.

Overwrite a file named sensitive.txt 3 times without removing the file:

shred -n 3 sensitive.txt

Overwrite and then delete sensitive.txt:

shred -u sensitive.txt

Hope you have learn a things or two in this article πŸ₯³

Of course this is not the end, there is a lot more commands but we have seen the basics let's say, keep in mind the important things is not the quantity of commands you know but the quality of your usage !