Linux is a popular operating system that is widely used for various purposes, such as servers, desktops, and smartphones. Linux is free and open-source software, which means that anyone can modify and distribute it. Linux is based on Unix, a family of operating systems that originated in the 1970s.
One of the features of Linux is the command line, which is a text interface to interact with the system. The command line allows users to perform various tasks by typing commands and receiving output. The command line can be faster and more powerful than graphical user interfaces (GUIs), but it can also be more complex and confusing for beginners.
In this article, we will introduce some of the basic Linux commands that every beginner should know. These commands will help you navigate through the file system, manipulate files and directories, access information about the system, and perform other common operations. We will also provide some examples and tips on how to use these commands effectively.
Navigating the File System
The file system is the structure that organizes all the files and directories on your computer. To work with files and directories, you need to know how to move around the file system and locate them. The following commands will help you do that:
- pwd – This command stands for “print working directory”. It shows the current directory you are in. For example:
$ pwd /home/user
- cd – This command stands for “change directory”. It allows you to move to another directory by specifying its path. For example:
$ cd /etc $ pwd /etc
You can also use some shortcuts with this command, such as:
cd .. – This moves you up one level in the directory hierarchy.
cd ~ – This moves you to your home directory.
cd - – This moves you to the previous directory you were in.
ls – This command stands for “list”. It displays the contents of the current or specified directory. For example:
$ ls Desktop Documents Downloads Music Pictures Videos
You can also use some options with this command, such as:
- ls -a – This shows all files and directories, including hidden ones that start with a dot (.).
- ls -l – This shows more details about each file and directory, such as size, permissions, owner, date, etc.
- ls -h – This shows the file sizes in a human-readable format, such as KB, MB, GB, etc.
Manipulating Files and Directories
Once you know how to locate files and directories, you may want to create, delete, copy, move, or rename them. The following commands will help you do that:
- mkdir – This command stands for “make directory”. It creates a new directory with the specified name. For example:
$ mkdir test $ ls Desktop Documents Downloads Music Pictures test Videos
- rmdir – This command stands for “remove directory”. It deletes an empty directory with the specified name. For example:
$ rmdir test $ ls Desktop Documents Downloads Music Pictures Videos
- rm – This command stands for “remove”. It deletes a file or a directory with the specified name. For example:
$ rm test.txt $ ls Desktop Documents Downloads Music Pictures Videos
You can also use some options with this command, such as:
rm -r – This deletes a directory and all its contents recursively.
rm -f – This forces the deletion of a file or a directory without asking for confirmation.
rm -i – This asks for confirmation before deleting each file or directory.
cp – This command stands for “copy”. It copies a file or a directory from one location to another. For example:
$ cp test.txt /tmp $ ls /tmp test.txt
You can also use some options with this command, such as:
cp -r – This copies a directory and all its contents recursively.
cp -i – This asks for confirmation before overwriting an existing file or directory.
cp -v – This shows the progress of the copying process.
mv – This command stands for “move”. It moves or renames a file or a directory from one location to another. For example:
$ mv test.txt /tmp/new.txt $ ls /tmp new.txt
You can also use some options with this command, such as:
- mv -i – This asks for confirmation before overwriting an existing file or directory.
- mv -v – This shows the progress of the moving process.
Accessing Information about the System
Linux provides various commands to access information about the system, such as the hardware, the software, the processes, the users, etc. The following commands will help you do that:
- uname – This command shows basic information about the operating system, such as the kernel name, the kernel version, the machine architecture, etc. For example:
$ uname Linux $ uname -a Linux user 5.4.0-81-generic #91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
- whoami – This command shows the username of the current user. For example:
$ whoami user
- ps – This command shows information about the processes running on the system. For example:
$ ps PID TTY TIME CMD 956 pts/0 00:00:00 bash 1000 pts/0 00:00:00 ps
You can also use some options with this command, such as:
ps -e – This shows all processes on the system.
ps -u – This shows processes owned by a specific user.
ps -f – This shows more details about each process, such as parent process ID, start time, command line, etc.
top – This command shows the dynamic information about the processes running on the system, such as CPU usage, memory usage, process ID, etc. It also allows you to interact with the processes, such as killing them, changing their priority, etc. For example:
$ top top - 06:49:18 up 1:14, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 193 total, 1 running, 192 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.3 us, 0.2 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 3906.2 total, 1439.7 free, 973.4 used, 1493.1 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 2567.9 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND ...
- man – This command shows the manual page for a specific command or topic. It provides a detailed description of the command or topic, its syntax, its options, its examples, etc. For example:
$ man ls LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is speci‐ fied. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. ...
You can also use some options with this command, such as:
- man -k – This searches for a keyword in all manual pages and shows a brief summary of each match.
- man -f – This searches for an exact match of a command or topic in all manual pages and shows a brief summary of each match.
- man -w – This shows the location of the manual page file for a specific command or topic.
Other Useful Commands
There are many other useful commands in Linux that can help you perform various tasks and operations on the system. Here are some of them:
- echo – This command prints a message or a value to the standard output (usually the terminal). For example:
$ echo Hello World! Hello World! $ echo $USER user
- grep – This command searches for a pattern in a file or an input and prints the matching lines. For example:
$ grep user /etc/passwd user:x:1000:1000:user,,,:/home/user:/bin/bash