
For someone new to Linux, using it can still feel challenging, even with user-friendly distributions like Ubuntu and Mint.
While these distributions simplify many tasks, some manual configuration is often required, but fully harnessing the power of Linux, understanding basic commands is essential.
In Linux, commands are the primary way to interact with the system and allow users to perform tasks, configure settings, and manage the system efficiently.
This guide introduces 60 essential Linux commands, providing a foundation for beginners and a pathway to becoming a skilled system administrator. These commands cover a wide range of functions, from navigating directories and managing files to performing advanced system operations.
Whether you’re just starting out or aiming to deepen your expertise, these commands will help you unlock the full potential of Linux.
Basic Linux Commands and Examples for Newbies
Whether you’re a beginner or an advanced user, mastering basic Linux commands is essential for navigating and controlling the operating system.
Below are some commonly used Linux commands along with their descriptions and examples:
1. View the Contents of a Directory
The ls command is used to view the files and directories within a specified directory, it can display both visible and hidden files (files starting with .
are hidden by default).
Additional options can provide detailed information like file permissions, ownership, size, and modification dates.
ls -la
2. Viewing Blocks, HDD Partition, External HDD
The lsblk command displays information about block devices such as hard drives, partitions, and external drives in a tree-like format.
lsblk
3. Checking the Integrity of Downloaded/Transferred Packages
The sha256sum
or md5sum
commands generate and verify cryptographic hash values for files, which ensures the file’s integrity after downloading or transferring.
sha256sum file.tar.gz
4. Converting and Copying a File
The dd command is used for low-level file copying and conversion, which is often employed to create bootable USB drives or clone disk images.
dd if=input.img of=output.img bs=4M status=progress
5. Know Your Machine Name, OS, and Kernel
The uname command provides system information, including the kernel version, system architecture, and hostname.
uname -a
6. Viewing History of Commands
The history command lists previously executed commands, allowing you to recall or reuse them.
history
7. Run Commands as Root User
The sudo command allows users to execute commands with administrative privileges. Use sudo -i
to switch to a root shell.
sudo -i
8. Make a New Directory
The mkdir command creates a new directory with the specified name in the current location.
mkdir my_folder
9. Create or Update File Timestamps
The touch command creates empty files or updates the timestamp of existing ones.
touch my_file.txt
10. Changing the File Permission
The chmod
command changes file permissions, controlling who can read, write, or execute a file.
chmod 644 my_file.txt
11. Change File Ownership
The chown command changes file ownership and group ownership.
sudo chown user:group my_file.txt
12. Install, Update, and Maintain Packages
The apt command manages software packages on Debian-based systems.
sudo apt update && sudo apt install package_name
13. Uncompressing a Tar File
The tar command extracts compressed files.
tar -xvzf file.tar.gz
14. See Current Date, Time, and Calendar
The date and cal
commands display the current date, time, and calendar.
date cal
15. Print Contents of a File
The cat command prints the contents of a file to the terminal.
cat my_file.txt
16. Copy and Move Files
The cp and mv commands copy and move files, respectively.
cp source.txt destination.txt mv old_name.txt new_name.txt
17. See the Working Directory for Easy Navigation
The pwd command prints the current directory path.
pwd
18. Change the Working Directory
The cd command navigates between directories.
cd /path/to/directory
Advanced Linux Commands for Intermediate Users
Once you’ve mastered the basic Linux commands, it’s time to dive deeper into more advanced commands that will help you manage and optimize your system more efficiently.
19. Finding a File in a Given Directory
The find command is used to search for files and directories within a specified directory or across the entire file system.
find /path/to/directory -name "filename"
20. Searching a File with the Given Keywords
The grep command is used to search for specific patterns (keywords) within files, which is highly useful when you want to find specific information inside a file or a set of files.
grep "keyword" filename
21. Finding Online Documentation
The man command displays the manual or help documentation for a specific command, which provides detailed information about how to use a command, its options, and its syntax.
man ls
22. List Current Running Processes
The ps command shows a snapshot of the current processes running on your system with details like process IDs (PIDs), memory usage, and CPU usage.
ps aux
To list all processes related to Firefox, using grep
to filter the output.
ps aux | grep firefox
23. Kill a Running Process
The kill command is used to terminate a running process by specifying the process ID (PID) of the process you want to terminate.
kill 1234
24. See the Location of Installed Binaries
The which
command helps locate the path of executables in your system’s PATH by searching through the directories listed in the PATH environment variable and returns the location of the executable.
which python3
25. Starting, Ending, Restarting a Service
The systemctl command is used to manage system services (also known as daemons) on systems using systemd.
systemctl start service_name systemctl stop service_name systemctl restart service_name
26. Creating and Removing Command Aliases
Aliases are shortcuts for commands, which can save time by reducing the amount of typing and the alias command creates a shortcut, while unalias remove it.
alias shortcut_name='command' unalias shortcut_name
You can create an alias for a command with a custom name.
alias ll='ls -l' unalias ll
This creates an alias ll
for the ls -l
command, which lists directory contents in a long format.
27. View Disk and Space Usage
The df command shows disk space usage for all mounted file systems by providing information about the total space, used space, and available space.
df -h
28. Removing a File and/or Directory
The rm command is used to remove files and directories. You can use the -r
option to remove directories and their contents recursively.
rm filename rm -r directory_name
29. Print/Echo a Custom Output on Standard Output
The echo command is used to print text or the value of a variable to the terminal.
echo "Custom Message"
30. Changing Password in Linux
The passwd
command is used to change passwords for the current user or other users (if you are the root user).
passwd username
31. View Printing Queue
The lpq
command shows the status of the printing queue, including any pending or completed print jobs.
lpq
32. Compare Two Files
The diff command compares two files line by line and displays the differences between them.
diff file1 file2
33. Download a File, the Linux Way (wget)
The wget command is used to download files from the internet, it supports HTTP, HTTPS, and FTP protocols.
wget https://example.com/file.zip
34. Mount a Block/Partition/External HDD
The mount
command is used to attach a block device (e.g., a hard drive or USB drive) to a directory in the filesystem.
mount /dev/sdX /mnt
35. Compile and Run C, C++, and Java Code
To compile and run code in C
, C++
, and Java
, you use the respective compilers and runtimes.
- C:
gcc
is used to compile C programs.c. - C++:
g++
is used to compile C++ programs. - Java:
javac
is used to compile Java programs, and java is used to run them.
To compile and run C
code:
gcc -o outputfile sourcefile.c ./outputfile
To compile and run C++
code:
g++ -o outputfile sourcefile.cpp ./outputfile
To compile and run Java
code:
javac filename.java java filename
Advance Linux Commands for Linux Sysadmins
In the last section of this series, we tried to cover the commands that needed to administer a Linux server.
36. Configuring Network Interface
The ifconfig command is used to allow you to set up, manage, and display network interface parameters, which is typically used to assign IP addresses, configure network interfaces, and troubleshoot network issues.
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
This command assigns the IP address 192.168.1.100
to the eth0
interface and brings it up.
37. Viewing Custom Network-Related Information
The netstat command provides detailed information about network connections, routing tables, interface statistics, and more.
netstat -tuln
38. Fetching Data with curl
The curl command is used to transfer data from or to a server. It supports various protocols, including HTTP, FTP, and more. You can use it to fetch data from a web server or test server responses.
curl -I https://www.example.com
39. Checking DNS Information
The dig command is a DNS lookup utility that provides detailed information about DNS records, which is commonly used for troubleshooting DNS issues.
dig example.com
40. Knowing Your System Uptime
The uptime command shows how long the system has been running, the number of users logged in, and the system load averages for the past 1, 5, and 15 minutes.
uptime
41. Broadcast Messages to Logged-in Users
The wall
command is used to send messages to all users currently logged into the system.
echo "System Maintenance in 10 minutes" | wall
42. Send Text Messages Directly to a User
The write
command allows you to send a message to another logged-in user.
write username
43. Piping Commands Together
You can combine multiple commands using pipes (|)
to pass the output of one command to another.
ps aux | grep firefox
44. Seeing the Processes of a CPU
The top command displays a dynamic, real-time view of system processes, including CPU and memory usage.
top
45. Creating a Newly Formatted ext4 Partition
The mkfs.ext4
command is used to create an ext4 file system on a partition.
sudo mkfs.ext4 /dev/sdb1
46. Editing Files with vi, emacs, and nano
In Linux, there are several text editors available for editing files directly from the command line. Among the most popular are vi, emacs, and nano.
To edit files with vi
:
vi filename.txt
To edit files with emacs
:
emacs myfile.txt
To edit files with nano
:
nano myfile.txt
47. Copying a Large File/Folder with Progress Bar
The rsync command is used to copy files and directories. With the --progress
option, it shows a progress bar.
rsync -avh --progress source_directory/ destination_directory/
48. Check Available Memory
The free command shows the amount of free and used memory in the system.
free -h
49. Backing Up a MySQL Database
The mysqldump command is used to create backups of MySQL databases.
mysqldump -u root -p database_name > backup.sql
50. Generate a Random Password
The openssl
command can generate a random password.
openssl rand -base64 12
51. Merge Two Text Files
The cat
command can be used to concatenate two text files into one.
cat file1.txt file2.txt > mergedfile.txt
52. List of All the Opened Files
The lsof command lists all open files and the processes that opened them.
lsof
These commands are fundamental tools for system administrators and users to interact with Linux systems efficiently.