Log In

Basic Linux Commands

Basic Linux Commands
12.07.2024
Reading time: 9 min
Hostman Team
Technical writer

Linux is an open-source operating system commonly used in server solutions that require high reliability, fault tolerance, and customization flexibility. To interact with a Linux-based OS, you execute commands in a console terminal, especially when managing remote hosts such as cloud servers.

This guide will cover the most basic terminal commands necessary for working with most known Linux distributions:

  • Debian

  • Ubuntu

  • Alt Linux

  • Kali Linux

This guide uses Ubuntu 22.04, installed on a Hostman cloud server.

Basic Linux commands vary in their purposes but  are most commonly needed for the following tasks:

  • Managing files and directories: creating, deleting, moving, copying, and changing permissions of files and directories.

  • System administration: managing processes, installing, and removing programs.

  • Managing network: managing network connections, checking network status, and configuring network interfaces.

Command Input and Output Streams

Before diving into the commands, it is important to understand a few basic principles of how Linux handles data.

Every process (program) in Linux has three standard data streams:

  • stdin (number 0): input stream.

  • stdout (number 1): output stream.

  • stderr (number 2): error stream.

The most basic are stdin and stdout.

Using standard data streams, Linux allows you to build data processing pipelines. In other words, instead of displaying the application's (launched by a command) result in the console, you can pass the data as input to another application, and so on.

For example, a trivial pipeline might look like this:

ls .. | grep 32

Output:

lib32
libx32

First, we execute the ls command, which displays a list of all system files and folders, and then pass its output to the grep command, which searches for directories whose names contain 32.

Environment, Directories, and File Management

This section covers the basic commands in Linux for navigating directories and managing files.

Current Directory Address (pwd)

To see which directory you are currently in, use:

pwd

For example, if you are in the /root directory, the console will display:

/root

Changing to a Specific Directory (cd)

Instead of specifying absolute paths when executing commands, it may be convenient to manually navigate between directories:

cd /usr

In this example, we have navigated to the system directory /usr. You can verify this by explicitly requesting the current directory:

pwd

Output:

/usr

To return to the root directory, execute the cd command without specifying a path:

cd

Let's navigate to another system directory:

cd /sys/devices

To go up one level, use two dots (..):

cd ..

Now we are in the /sys directory.

Listing Files and Directories in the Current Directory (ls)

To explore the contents of the file system, you can request a list of files and directories in the current directory by using the ls command in Linux.

ls

Output:

bin  games  include  lib  lib32  lib64  libexec  libx32  local  sbin  share  src

To see hidden files and directories, add the -A flag:

ls -A

Output:

.ansible  ._history  .rc  .cache  .lesshst  .profile  resize.log  snap  .ssh

A similar command that provides a bit more information about the file system contents, adding pointers to the current and higher levels, is:

ls -a

Output:

.  ..  .ansible  ._history  .rc  .cache  .lesshst  .profile  resize.log  snap  .ssh

You can check the contents of a directory without navigating into it:

ls /var

The console output will contain a list of files and directories of the specified directory, not the user's current location:

backups  cache  crash  lib  local  lock  log  mail  opt  run  snap  spool  tmp

To request a list of all system files and directories, add a slash (/) or two dots (..):

ls /

Or:

ls ..

Creating a Directory (mkdir)

When creating a directory, specify its name:

mkdir mywork

Verify that the directory was created:

ls

Output will show the new directory among others:

mywork  resize.log  snap

Directories can be created recursively (one inside another) using the -p flag:

mkdir myjob/inner -p

Check for the nested directory:

ls ./myjob

Output:

inner

The -p flag also prevents an error when creating an existing directory. For example, try creating the directory again in the usual way:

mkdir mywork

The console will display an error:

mkdir: cannot create directory 'mywork': File exists

However, with the -p flag, there will be no error:

mkdir mywork -p

Creating a File (nano)

Creating a file and filling it with content is done through the console text editor nano:

sudo nano myfile

The console terminal will switch to text editing mode. After finishing work with the file, press Ctrl + S to save it and Ctrl + X to close it.

The content of the created file be as follows:

My text

Copying a File or Directory (cp)

The cp command in Linux is used for copying files and directories. Copying creates an exact copy of the file or directory to the specified address:

cp myfile myfile2

Check the file system:

ls

Output shows our new file:

myfile  myfile2  myjob  mywork  resize.log  snap

Check its content:

sudo nano myfile2

It is identical to the content of the original file:

My text

Copying directories requires specifying the recursive execution flag -r:

cp mywork mywork2 -r

Check the current directory:

ls

The directory was successfully copied:

myfile  myfile2  myjob  mywork  mywork2  resize.log  snap

Deleting a File or Directory (rm)

Let's delete the previously created file:

rm myfile2

To delete directories, specify the -r flag:

rm mywork2 -r

You can also use a special command to delete a directory:

rmdir mywork2

Moving a File or Directory (mv)

Moving a file is similar to copying it, but the original file is deleted:

mv myfile mywork

The file will be located in the specified directory.

Moving directories is no different from moving files:

mv myjob mywork

Quick File Content Viewing (cat)

Instead of using the nano editor, you can immediately display the content of a file in the console using the Linux cat command:

cat mywork/myfile

The console will display the following:

My text

System and Network Information

Basic commands to display system data.

System Clock (date)

You can request the system date and time through the console terminal:

date

Output:

Sun Jul 07 09:27:16 PM BST 2024

List of Active Users (w)

You can request a list of all users currently logged into the system:

w

The console output will be similar to this:

05:00:30 up 40 min,  1 user,  load average: 0.02, 0.01, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    91.206.179.207   04:51    1.00s  0.09s  0.00s w

List of Active Processes (ps)

You can request a list of running processes:

ps

Output:

PID TTY          TIME CMD
11643 pts/1    00:00:00
11671 pts/1    00:00:00 ps

Connecting to a Remote Server via SSH (ssh)

ssh is a frequently used Linux command for managing remote hosts through an SSH connection:

ssh [email protected]

The command above follows this structure:

ssh USER@IP

Replace USER with the remote user's name and IP with the remote host's IP address. The console terminal will then ask for the root password:

[email protected]'s password:

Downloading Files via URL (wget)

Often, some programs are manually downloaded from remote repositories as regular files:

wget https://github.com/nginx/nginx/archive/refs/tags/release-1.25.4.tar.gz

In this example, we download an archive with the Nginx web server from the official GitHub repository. After that, you can check the current directory's status with the ls command to see that the downloaded archive has appeared.

Extracting Archives (tar)

Next you will need to extract files from the downloaded archive:

tar -xvf release-1.25.4.tar.gz

The flags indicate how to perform the extraction:

  • -x means extracting compressed files from the archive;

  • -v means displaying detailed information about the extraction process in the console;

  • -f means that the passed parameters are archive file names.

After extraction, a new folder with the extracted archive's name will appear in the current directory.

After extraction, you can remove the archive.

rm release-1.25.4.tar.gz

Installing and Removing Packages

Commands for managing Linux packages.

Updating the Repository List

Linux distributions have a standard package manager, apt. It is used by default in Debian and Ubuntu distributions. Typically, before using it, update the list of available repositories:

sudo apt update

It also makes sense to update already installed packages in the system:

sudo apt upgrade

You can also view the list of already installed packages in the system:

sudo apt list --installed

The console will display something like this:

Powershell Hr Xj Iijh Lv

Installing a Package

Packages are installed as follows:

sudo apt install nginx

For example, this way we installed the Nginx web server. Very often, during installation, the package manager asks additional questions, which can be answered with yes or no using the console inputs y and n. To have APT automatically answer yes to all questions during installation, add the -y flag:

sudo apt install nginx -y

Removing a Package

To remove a Linux package:

sudo apt remove nginx

Console Management

Commands for managing the console terminal.

Command History (history)

You can view the history of commands entered in the console terminal:

history

Clearing the Console Terminal (clear)

You can periodically clear the commands entered in the terminal:

clear

This will return the command line to its initial (clean) state.

Command Help (man)

You can always get help on any Linux command:

man ls

The console will display information about the command's structure, its possible flags, and parameters.

You can also obtain a shorter and less extensive version of the manual for as follows:

help

Conclusion

In this guide, we discussed the most frequently used commands in Linux. You can use it as a Linux commands cheat sheet, as these are the most basic commands that any user needs to know when working with Linux.

The official Linux kernel project website provides a complete list of commands and official documentation (including command guides, installation, and configuration guides).


Share