Basic Linux Commands
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 Copy link
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 32Output:
lib32
libx32First, 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 Copy link
This section covers the basic commands in Linux for navigating directories and managing files.
Current Directory Address (pwd) Copy link
To see which directory you are currently in, use:
pwdFor example, if you are in the /root directory, the console will display:
/rootChanging to a Specific Directory (cd) Copy link
Instead of specifying absolute paths when executing commands, it may be convenient to manually navigate between directories:
cd /usrIn this example, we have navigated to the system directory /usr. You can verify this by explicitly requesting the current directory:
pwdOutput:
/usrTo return to the root directory, execute the cd command without specifying a path:
cdLet's navigate to another system directory:
cd /sys/devicesTo go up one level, use two dots (..):
cd ..Now we are in the /sys directory.
Listing Files and Directories in the Current Directory (ls) Copy link
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.
lsOutput:
bin games include lib lib32 lib64 libexec libx32 local sbin share srcTo see hidden files and directories, add the -A flag:
ls -AOutput:
.ansible ._history .rc .cache .lesshst .profile resize.log snap .sshA similar command that provides a bit more information about the file system contents, adding pointers to the current and higher levels, is:
ls -aOutput:
. .. .ansible ._history .rc .cache .lesshst .profile resize.log snap .sshYou can check the contents of a directory without navigating into it:
ls /varThe 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 tmpTo request a list of all system files and directories, add a slash (/) or two dots (..):
ls /Or:
ls ..Creating a Directory (mkdir) Copy link
When creating a directory, specify its name:
mkdir myworkVerify that the directory was created:
lsOutput will show the new directory among others:
mywork resize.log snapDirectories can be created recursively (one inside another) using the -p flag:
mkdir myjob/inner -pCheck for the nested directory:
ls ./myjobOutput:
innerThe -p flag also prevents an error when creating an existing directory. For example, try creating the directory again in the usual way:
mkdir myworkThe console will display an error:
mkdir: cannot create directory 'mywork': File existsHowever, with the -p flag, there will be no error:
mkdir mywork -pCreating a File (nano) Copy link
Creating a file and filling it with content is done through the console text editor nano:
sudo nano myfileThe 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 textCopying a File or Directory (cp) Copy link
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 myfile2Check the file system:
lsOutput shows our new file:
myfile myfile2 myjob mywork resize.log snapCheck its content:
sudo nano myfile2It is identical to the content of the original file:
My textCopying directories requires specifying the recursive execution flag -r:
cp mywork mywork2 -rCheck the current directory:
lsThe directory was successfully copied:
myfile myfile2 myjob mywork mywork2 resize.log snapDeleting a File or Directory (rm) Copy link
Let's delete the previously created file:
rm myfile2To delete directories, specify the -r flag:
rm mywork2 -rYou can also use a special command to delete a directory:
rmdir mywork2Moving a File or Directory (mv) Copy link
Moving a file is similar to copying it, but the original file is deleted:
mv myfile myworkThe file will be located in the specified directory.
Moving directories is no different from moving files:
mv myjob myworkQuick File Content Viewing (cat) Copy link
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/myfileThe console will display the following:
My textSystem and Network Information Copy link
Basic commands to display system data.
System Clock (date) Copy link
You can request the system date and time through the console terminal:
dateOutput:
Sun Jul 07 09:27:16 PM BST 2024List of Active Users (w) Copy link
You can request a list of all users currently logged into the system:
wThe 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 wList of Active Processes (ps) Copy link
You can request a list of running processes:
psOutput:
PID TTY TIME CMD
11643 pts/1 00:00:00
11671 pts/1 00:00:00 psConnecting to a Remote Server via SSH (ssh) Copy link
ssh is a frequently used Linux command for managing remote hosts through an SSH connection:
ssh root@91.206.179.207The command above follows this structure:
ssh USER@IPReplace 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:
root@91.206.179.207's password:Downloading Files via URL (wget) Copy link
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.gzIn 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) Copy link
Next you will need to extract files from the downloaded archive:
tar -xvf release-1.25.4.tar.gzThe flags indicate how to perform the extraction:
-
-xmeans extracting compressed files from the archive; -
-vmeans displaying detailed information about the extraction process in the console; -
-fmeans 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.gzInstalling and Removing Packages Copy link
Commands for managing Linux packages.
Updating the Repository List Copy link
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 updateIt also makes sense to update already installed packages in the system:
sudo apt upgradeYou can also view the list of already installed packages in the system:
sudo apt list --installedThe console will display something like this:
Installing a Package Copy link
Packages are installed as follows:
sudo apt install nginxFor 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 -yRemoving a Package Copy link
To remove a Linux package:
sudo apt remove nginxConsole Management Copy link
Commands for managing the console terminal.
Command History (history) Copy link
You can view the history of commands entered in the console terminal:
historyClearing the Console Terminal (clear) Copy link
You can periodically clear the commands entered in the terminal:
clearThis will return the command line to its initial (clean) state.
Command Help (man) Copy link
You can always get help on any Linux command:
man lsThe 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:
helpConclusion Copy link
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).