Important Linux Commands That You Should Know


Linux is quite a desirable technology skill in the software field since it helps to gain greater control over system functions, quicker management of operating systems, enable scripting to automate repetitive tasks, etc. On top of that,  Linux has huge online community support that makes it easy and available for learning. 

This article is a compilation of frequently asked Linux commands during interviews by companies in Southeast Asia.

File Handling

Renaming files in Linux

Method 1: mv command

The mv command (short form of move) is used to rename or move the files and directories from one location to another.

The move command moves the source file to the destination location, hence we can rename it to a new name. Only the destination file is retained.

$ mv  <sourcefile>  <destinationfile>

To rename more than one file, we can use the below format of the mv command. Here we are moving files: file1 and file2 to a directory named dir1.

$ mv file1 file2 dir1

And, we can rename a whole directory by using the below command:

$ mv directory1 directory2

mv command can be used to rename multiple files by using for loop as below example. It will change all the files in the directory with extension .txt to .pdf.

for f in *txt; do
   mv -- "$f" "${f%.txt}.pdf"

Method 2: rename command

Rename command renames the file based on the options and expression provided.

rename 's/old/new/' files
rename [options] 's/old/new/' files

Where options could be:
-v, –verbose => Verbose: print names of files successfully renamed
-n, –no-act => No Action: show what files would be renamed
-f, –force => Force: overwrite existing files

For example, rename all perl files (*.perl) to *.pl, enter:

rename 's/perl/pl/' *.perl


rename -v 's/perl/pl/' *.perl
'file1.perl' renamed to ''
'file2.perl' renamed to ''

Creating a new file in Linux

Method 1: touch command

We can create a new file using the touch command. If the file exists, this command will update the timestamp on the existing file. 

$ touch  <newfilename>

Multiple files can be created using a single touch command by specifying their name separated by space as shown below.

$ touch file1.txt file2.txt file3.txt

Note: To create a new file, users should have ‘write’ permission on the directory otherwise ‘permission denied’ error will be received. 

Method 2: cat command

cat command is commonly used to view the content of an existing file or to concatenate files.  

  • Step 1: $ cat > newfile.txt 
  • Step 2: Press enter and type in the content you need. 
  • Step 3: Press ctrl+d to save changes to the file

Method 3: echo command

echo command is used to print strings which are passed as arguments to the standard output. However, we can redirect the output to a file, thus creating a new file. 

To create a new file with text inputted directly:

$ echo “your text here” > newfile.txt

Or we can just create a new empty file, without providing the text.

$ echo > newfile.txt

Differences between BASH and DOS commands

Directory Operations  

To list all the files and subdirectories present in the directory

$ ls 


Find out current directory you are in right now

$ pwd 


Create a new directory

$ mkdir <newdirectory or the complete path>

Note: To view the newly created directory, we can use the ‘ls’ command to see if the directory has been created and present in the listing. 

Remove/delete a directory

$ rmdir <directory or the complete path>

To verify that a directory has been successfully removed, use ‘ls‘ command to view the directory listing. 

Process Management

To terminate a process in Linux

We can terminate a process in Linux by using the ‘kill’ command. It takes a pid(process id) as an argument. When a process becomes unresponsive and does not shut down on its own, then we need to use the kill command to terminate it. We use the ‘ps command to list the currently running processes and display information about those processes including pid. 

$ ps 

PID     TTY       TIME      CMD 
13330   pts/0    00:00:00   bash 
24621   pts/0    00:00:00   ps

PID – unique process ID
TTY – terminal type
TIME – time in minutes and seconds since the process has been running
CMD – the name of the command that launched the process

$ kill <process id>

Find out more on terminating processes is here. 

View history of the commands executed

The history of the commands on the command line is stored in a history file. When we run the ‘history’ command, we can see the list of all the commands that were run before.  

$ history
Output of history command in Linux

When used with an option like ‘5’, it gives the 5 most recent commands from history. 

$ history 5

Search Operations 

grep stands for  ‘global regular expression print’, i.e. it is commonly use for finding the input pattern that is present within a file. It searches a particular pattern of characters in a file and displays the lines which contain that pattern. 

Search for a String in a single file 

grep “example” sample.txt 

This is the line which contains a string called example.

Search for Multiple Strings

We can search multiple strings in a single command using OR operator “|” as below:

grep -E 'rose|jasmine|lotus' flowers.txt 

This is a rose flower. 
Here is jasmine. 
Also a lotus.

Invert Match (Exclude)

To perform a search by excluding specific text or pattern, we use -v option as shown below. This command will display the lines which do not contain the ‘fruit’ string in file ‘example.txt’ .  

example.txt has below contents: 
This is a vegetable. 
Apple fruit. 

grep -v fruit example.txt 

This is a vegetable.

Filter the Output of a Command

We can use pipe “|” to filter the output of one command according to the search condition provided in grep. 

$ cat example.txt | grep Apple 

Apple fruit.

By default, the grep command is case-sensitive, to deliberately make the search insensitive we use the below option in the grep command: 

$ grep -i apple example.txt 

Apple fruit.

File Permissions 

First, use ls command to find out a file permissions.

ls -l file1.txt

-rw-r--r-- 12 myfile users 28.0K May 10 10:23 file1.txt
|[-][-][-]-  [------] [---]
| | | | |        |      |
| | | | |        |      +-----------> 7. Group
| | | | |        +------------------> 6. Owner
| | | | +---------------------------> 5. Alternate Access Method
| | | +-----------------------------> 4. Others Permissions
| | +-------------------------------> 3. Group Permissions
| +----------------------------------> 2. Owner Permissions
+------------------------------------> 1. File Type

First character refers to the file type. (-) refers to a file while (d) refers to a directory.

(rw-r–r–) in the example above means that the file owner has read and write permissions (rw-) while the group and others have only read permissions (r–).

chmod(change mode) command is used to assign and change file/directory permissions either through symbolic or numeric mode.

$ chmod [OPTIONS] MODE filename

Symbolic Mode

chmod [OPTIONS] [ugoa…][-+=]perms…[,…] FILE...

A file has three permissions: read(r), write(w), execute(x).
A file has three types of users: owner(u), group(g), others(o) and all(a).

Operation flags (-+=) defines if the permission are to be removed, added or set:

  • (-) Removes the specified permissions
  • (+) Adds the specific permissions
  • (=) Change the current permissions to the specific permissions.
Example to grant members of the group with permission to read-only for file1.txt:
chmod g=r file1.txt

Example to remove read, write and execute permission for both others and group:
chmod og-rwx filename

Numeric Mode


where NUMBER can be either a 3 or 4-digits

The below table depicts the permissions in octal numbers: 

Example of chmod in symbolic mode:   
chmod u=r,g=wx,o=rx <filename> 

Example of chmod in numeric mode:   
chmod 435 <filename>

Both of these commands are the same, where user(u) is given read(4) permission. Group(g) is given write and execute (3) permission. Other(o) are given read and execute(5) permission. 

Differences between chmod and chown

chmod  chown 
Changes the mode/permission(read,write,execute) on a file for user/group/others/all.  Changes the file ownership of a file/directory. 





chmod 700 permissions/file2 


chown user1 test/test1.txt 


Related Articles