Top 25 Linux Commands With Examples
In this quick lesson, we will see 25 most common Linux Commands which are used on daily basis when we work on Linux. For the purpose of this lesson, we will be using Ubuntu 18.04 LTS (GNU/Linux 4.15.0-20-generic x86_64) version but the commands should work the same in almost all version for Ubuntu:
Creating a new file
Use the below simple touch command to create a new file:
Let’s see the output for this command:
Open File to edit
Best way to edit a text file in Ubuntu is by using the VI editor which shipped with every version of Ubuntu. Start VI editor with the following command:
Let’s see the output for this command:
Edit file in VI Editor
The last command we showed above will only open the file so that you can see the file contents. If you wish to do any changes, first open the file with VI editor:
Next, press I so that you enter the edit mode. Now you can edit the file. Once, you’re done with changes, press Esc + :wq to save changes and exit the file:
See File content
It isn’t necessary to open the file in VI just to see its contents. Use the following command to print entire file on terminal:
Let’s see the output for this command:
Find file in with Prefix in name
To find a file with some prefix characters, just use the following command:
Let’s see the output for this command:
Just replace the characters before star to characters you want to find.
Make file read-only
We already know how permissions work in Linux based on these codes:
1 – execute only
2 – write only
3 – write and execute
4 – read-only
5 – read and execute
6 – read and write
7 – read, write and execute
Just run this command to make a file read-only:
Once we run this command and again open the file using VI, we will see the readonly indicator in the VI as well:
Give Read/Write Permissions in Unix
We can make a file as write again by using the following access-codes from a root user:
Now, we can again edit this file in VI or do anything else with it.
Find list of Links in a Directory
We can run a simple command to find list of Links in a Directory:
In above command:
- ^ – This signifies start of each line
- l – Each symbolic link has “1” in the beginning of line
See all files in current directory
We can see what all files are present in a directory using the following command:
Let’s see the output for this command:
The option -h provides us the size of the files present in human-readable form. The -l option also provides data in a format and show the permsissions and owner of each file present as well.
Find a Background Job
If we know a matching term which will be coming in the job name we wan to find, just run the following command:
Let’s see the output for this command:
This command just matches the process name and shows the results. Highlighted is the Process ID (PID).
Kill a Background Job
We can also kill a background job if we have its PID. Use the find command we showed above to fund the task and run the following command to kill it:
Let’s see the output for this command:
See current date
We can see the date and time for the machine. If you’re running a server, this can be different timezone from your actual timezone:
Let’s see the output for this command:
See Machine size
We can see how much space is left on the machine with a single command:
Let’s see the output for this command:
See History of commands
We can even see history of commands we have used in the session:
Let’s see the output for this command:
As I used 5 with the command, only last 5 commands were shown here.
Clear the console
When we want to clear the console, a simple command is enough:
Tracking process time
To see all current process and see how much sopace they are consuming, run the command:
Let’s see the output for this command:
Hit q to exit the window.
Count word in a File
If you want to find out how many times a single word has appeared in a file, use the following command:
Let’s see the output for this command:
Execute last command again
To execute the command you ran last, just run the following command:
Let’s see the output for this command:
Unzip a TAR Archive
If you download a file from web in the form of TAR Archive, just run the following command to unzip it:
File will be unarchived in the current directory itself.
Find files which were modifed in last 1 day
To find files which were modifed in last 1 day itself, run the following command:
Let’s see the output for this command:
Only the last file mentioned was the file we edited. Other files were edited by OS itself.
Find files which were modifed in more than 1 day
To find the files which were modified more than 1 day before, run the following command:
Create Soft link
To create a softlink to point to a file or a directory, run the following command:
Let’s see the output for this command:
Remove Soft link
To remove a softlink, run the following command:
Count Line, Word and Characters in a file
To count the Line, Word and the Characters in a file, run the following command:
Let’s see the output for this command:
See current user
To see the currently logged in user, run the following command:
Let’s see the output for this command:
Conclusion
In this lesson, we looked at 25 most popular commands which you can use with Linux everyday.