Top 30 Linux Commands Everyone Should Know – Part I

Why Linux?
Linux may not have the nicest UI out there in the market or never your favourite destination when it comes to gaming. It may not be the number one operating system for desktop users around the globe(only has 4% of global share). So, why is it still the top choice for cloud solutions ?
Have you ever wondered why over 90% of the Internet’s infrastructure relies on or is built upon Linux or UNIX-based systems? Why do nearly all leading companies prefer Linux or Unix systems for everything from simple code to enterprise-level applications?
What sets these systems apart from others?
The answer often lies in the power of Linux command lines.
What is Linux Command Line?
“Text editors will help you write applications. But command lines help you become a great developer”
– Jithin Modon
A Linux command line or a Terminal is an interface that will let you interact with your operating system to perform various operations using the commands. These commands are predefined programs written or supported by the operating system to perform various operations on its file system or, in some cases, on remote file systems located on the internet.
Use Cases
The following are some use cases of a Linux command line.
File Management
Apart from the listing and navigating through the file system, Linux commands will help you in create, delete, move, and rename files and directories.
File Manipulation
Viewing file content, updating the file content using regular expression and changing the file permission are some of the operations we can do with Linux commands.
System Administration
To manage the users of the system, process and the services.
Software Development
CLI are great tools for saving time while developing. Integrating a third party library to your application becomes a lot easier with the use of the Command Line Interface.
Network Configuration
CLI is the best tool for managing network traffic, users, and resources.
Automation
Want to automate your boring repetitive task, you are in good hands. Linux shell script can be used to schedule complex and repetitive operations most efficiently. Once a shell script is defined and configured, it requires minimal or no further involvement to perform the tasks.
Cloud Computing
With the appropriate permissions, you can perform all the actions available in your cloud console. The console may look nice, but the command line has a certain coolness to it.
Prerequisites
Before diving into the Linux commands, let’s see what you need to get started.
- A Linux or Unix based system.
- An user login with an administrative privilege.
Did you know?
- Did you know your Android device is built on top of Linux Kernel?
- Most Linux commands will also work on macOS because it is based on UNIX.
Linux Commands
Here are 30 most useful Linux commands that every developer should know. This blog post is split into two parts to accommodate all the 30 commands. If you have already covered the first part, here’s the link for part 2.
1. ls
The ls
command, or list command, is used to display files and directories in the working directory. It is one of the most widely used commands by Linux users.
admin@dotsperpixels.com ~ % ls
google-cloud-sdk projects
The commonly used arguments for the ls
command are
- -l : This will list the files and directory with detailed information.
admin@dotsperpixels.com ~ % ls -l
total 2
drwxr-xr-x@ 21 admin dotsperpixels 672 Jul 9 19:36 google-cloud-sdk
drwxr-xr-x 6 admin dotsperpixels 192 Jul 15 19:24 projects
- -a: To list hidden files and directories.
- -h: To display file and directory sizes in a human-readable format.
admin@dotsperpixels.com ~ % ls -lha
total 280
drwxr-x---+ 28 admin dotsperpixels 896B Aug 3 21:33 .
drwxr-xr-x 6 root admin 192B Jul 17 14:43 ..
-rw-r--r--@ 1 admin dotsperpixels 487B Jul 10 11:48 .zshrc
drwxr-xr-x@ 21 admin dotsperpixels 672B Jul 9 19:36 google-cloud-sdk
drwxr-xr-x 6 admin dotsperpixels 192B Jul 15 19:24 projects
2. cd
This allows a user to change or navigate between directories.
- cd : Without specifying a path, it will change the working directory to the home directory.
- cd ~ : Take the user to the home directory.
- cd .. : cd followed by two periods(..) will take the user to the parent directory or move one directory up.
- cd – : Takes the user to the previous directory selection.
# change the working directory to Downloads
admin@dotsperpixels.com ~ % cd Downloads
admin@dotsperpixels.com Downloads %
# Move one directory up
admin@dotsperpixels.com Downloads % cd ..
admin@dotsperpixels.com ~ %
# Navigate to the previous directory selection
admin@dotsperpixels.com ~ % cd -
admin@dotsperpixels.com Downloads %
3. pwd
To print the current working directory in the terminal.
admin@dotsperpixels.com Downloads % pwd
/Users/admin/Downloads
4. mkdir
To create a new directory or folder using the terminal. Typically, root or sudo access is required for this operation.
# To create a new directory called dotsperpixels under the current working directory
admin@dotsperpixels.com ~ % mkdir dotsperpixels
# To create a new directory called python under dotsperpixels
admin@dotsperpixels.com ~ % mkdir dotsperpixels/python
5. rmdir
To remove an empty directory or folder using the terminal. Typically, root or sudo access is required for this operation.
# Trying to remove a non empty folder
admin@dotsperpixels.com ~ % rmdir dotsperpixels
rmdir: dotsperpixels: Directory not empty
# To remove directory called python under dotsperpixels
admin@dotsperpixels.com ~ % mkdir dotsperpixels/python
# To remove directory called dotsperpixels under the current working directory
admin@dotsperpixels.com ~ % mkdir dotsperpixels
6. cp
Used for copying files and directories. Here the copy operation is equivalent to copy and paste operation in our file managers. While copying a directory, all its child directories and files will be copied.
- Here’s the syntax:
cp source destination
# To copy a file to the same directory
admin@dotsperpixels.com ~ % cp sample.txt sample_copy.txt
# To copy a file to another directory
admin@dotsperpixels.com ~ % cp sample.txt tmp/sample_copy.txt
# To copy a directory in to tmp directory
admin@dotsperpixels.com ~ % cp -r sample_folder tmp/sample_folder
- -r : Indicates that the content and folders will be copied recursively to the destination.
- The copy operation does not alter the original file in any way.
A copy operation can be performed between two remote systems using scp(Secure Copy Protocol) command. This is commonly used to transfer files from your local machine to a remote server ( vice versa), without needing a file-sharing application like FileZilla.
7. mv
Used for moving files and directories. Here move operation is equivalent to cut and paste operation in our file managers. While moving a directory, all its child directories and files will also be copied.
- Here’s the syntax:
mv source destination
# To move a file to the same directory
admin@dotsperpixels.com ~ % mv sample.txt sample_copy.txt
# To copy a file to another directory
admin@dotsperpixels.com ~ % mv sample.txt tmp/sample_copy.txt
# To copy a directory in to tmp directory
admin@dotsperpixels.com ~ % mv -r sample_folder tmp/sample_folder
- -r : Indicates that the content and folders will be moved recursively to the destination.
- The move operation changes the original file name or location.
- We can rename a file or folder by moving it within the same directory with a new name.
8. rm
This command will allow users to remove files or directories. Typically, root or sudo access is required for this operation.
# To remove a file
admin@dotsperpixels.com ~ % rm sample.txt
# To copy a file to another directory
admin@dotsperpixels.com ~ % sudo rm -fr sample_folder
- -f : Allows file or directory removal without a confirmation.
- -r : Recursively deletes directories and their contents.
Note: Files or directories deleted using the rm
command will be permanently removed from your system, so ensure you are deleting the correct file or directory.
9. clear
To clear a terminal window screen.
admin@dotsperpixels.com tmp % clear
10. man
Without a doubt this is the most powerful command in Linux or Unix-based systems. This command provides detailed manuals or documentation on various Linux commands and tools.
# For getting the manual or documentation for chmod command
admin@dotsperpixels.com tmp % man chmod
11. cal
To print the calendar for a month.
# Cal without any parameter will give the current month.
admin@dotsperpixels.com tmp % cal
August 2024
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
# To display a specific month
admin@dotsperpixels.com tmp % cal March 1995
March 1995
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
12. apt
The apt
(Advanced Package Tool) command is a package management tool for Linux based systems. This is used to install a new application, update and remove the application from the system.
# For Updating the package list with latest version meta data.
admin@dotsperpixels.com tmp % sudo apt update
# For installing a package
admin@dotsperpixels.com tmp % sudo apt install htop
# For upgrading all the installed packages
admin@dotsperpixels.com tmp % sudo apt upgrade
# For upgrading the specified package
admin@dotsperpixels.com tmp % sudo apt upgrade htop
# For removing a package
admin@dotsperpixels.com tmp % sudo apt remove htop
- update – This is used to update the package list with the latest package version and other meta information. Here, no existing packages gets updated.
- install – To install a package with the latest version. Optionally we can specify a version to it.
- upgrade – This command is used to upgrade a package with the provided version. If version is not provided, it will upgrade to the latest version of the package. And if no package name is provided, it will upgrade all the installed package to the latest version.
- remove – To remove a package from the system. Here, the configuration file won’t be removed.
- purge – To perform a clean uninstall of an application, you can remove both the package and its configuration files from the system.
- autoremove – To remove all unused dependency files from your system. These packages are typically left behind when you remove an application.
# For a clean uninstall
admin@dotsperpixels.com tmp % sudo apt remove --purge package_name
admin@dotsperpixels.com tmp % sudo apt autoremove
13. sudo
Not all command can run as a regular user, for some you need to have the admin or super user privilege. The sudo command is used to run command as a super user. It stands for “Superuser do”.
# For a clean uninstall
admin@dotsperpixels.com tmp % sudo apt update
14. alias
Remembering commands is useful, but why memorize lengthy ones when you can achieve the same with less typing? This is where aliases come in handy. This will let you create your own command or shortcut for your system. Make sure you are not using the same alias names, as your system commands.
It’s common to have trouble remembering the IP addresses of servers when working with multiple ones. Simply add an alias entry in your .bashrc
or .zshrc
file to make this easier.
# For adding an alias for the ssh connection
admin@dotsperpixels.com tmp % alias myserver='ssh -i /path/to/my-key.pem ubuntu@ec12-125-0-103-25.compute-1.amazonaws.com'
# SSH to the server
admin@dotsperpixels.com tmp % myserver
#remove the alias from the system
admin@dotsperpixels.com tmp % unalias myserver
15. ifconfig
The ifconfig
command will give you the list of all the network interfaces along with the IP addresses, MAC addresses and other information about the interface.
admin@dotsperpixels.com tmp % ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
anpi0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether 5a:52:c7:e6:d5:d4
media: none
status: inactive
Conclusion
In part 1 of our tutorial we have seen some of the use cases of Linux commands and some basic Linux commands, you can apply to your day-to-day life. In part 2, we will expand on this knowledge by covering a mix of basic to intermediate commands, helping you enhance your command-line proficiency and tackle more complex tasks with confidence.
Happy coding!
Credits :
Photo by Gabriel Heinzer on Unsplash
2 thoughts on “Top 30 Linux Commands Everyone Should Know – Part I”