Top 25 Linux Commands With Examples

Top 25 Linux Commands With Examples

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:

Ubuntu Version 18.04

Creating a new file

Use the below simple touch command to create a new file:

touch linuxhint.txt

Let’s see the output for this command:

Create new file

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:

vi linuxhint.txt

Let’s see the output for this command:

Open file in VI

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:

vi linuxhint.txt

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:

Edit File in VI

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:

cat linuxhint.txt

Let’s see the output for this command:

Print file contents

Find file in with Prefix in name

To find a file with some prefix characters, just use the following command:

ls -ltr | grep lin*

Let’s see the output for this command:

Find file

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:

0 – no permissions
1 – execute only
2write only
3write and execute
4 – read-only
5read and execute
6read and write
7read, write and execute

Just run this command to make a file read-only:

chmod 400 linuxhint.txt

Once we run this command and again open the file using VI, we will see the readonly indicator in the VI as well:

File read only

Give Read/Write Permissions in Unix

We can make a file as write again by using the following access-codes from a root user:

chmod 777 linuxhint.txt

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:

ls -lrt | grep “^l”

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:

ls -lh

Let’s see the output for this command:

List Files

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:

ps -ef | grep bash

Let’s see the output for this command:

Find Background Process

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:

kill -9 <PID>

Let’s see the output for this command:

Kill process

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:

date

Let’s see the output for this command:

Show Date

See Machine size

We can see how much space is left on the machine with a single command:

df -h

Let’s see the output for this command:

Machine Space

See History of commands

We can even see history of commands we have used in the session:

history 5

Let’s see the output for this command:

Command History

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:

clear

Tracking process time

To see all current process and see how much sopace they are consuming, run the command:

top

Let’s see the output for this command:

Process Performance

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:

grep -c <word-to-count> <filename>

Let’s see the output for this command:

Word Count

Execute last command again

To execute the command you ran last, just run the following command:

!!

Let’s see the output for this command:

Run last cmd

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:

tar -xvzf <tar-file>

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:

find . -mtime -1

Let’s see the output for this command:

Modified files in last 1 day

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:

find . -mtime +1

Create Soft link

To create a softlink to point to a file or a directory, run the following command:

ln -s <link-name> <directory-path>

Let’s see the output for this command:

Create Softlink

Remove Soft link

To remove a softlink, run the following command:

rm mydirectory

Count Line, Word and Characters in a file

To count the Line, Word and the Characters in a file, run the following command:

wc <filename>

Let’s see the output for this command:

Count

See current user

To see the currently logged in user, run the following command:

who

Let’s see the output for this command:

Who

Conclusion

In this lesson, we looked at 25 most popular commands which you can use with Linux everyday.

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *