Important Linux Commands That You Should Know

Linux Commands

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

 

1. 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.

 

Syntax:
$ 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.

 

Syntax:
$ mv file1 file2 dir1

 

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

 

Syntax:
$ 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.

 

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

 

Method 2: rename command

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

 

Example:
rename 's/old/new/' files
or
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

 

Alternatively;

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


Explore the Job Openings in APAC

 

2. 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.

Syntax:
$ touch  <newfilename>

 

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

Example:
$ 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 trings 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

 

3. Differences between BASH and DOS commands

DOS BASH
Commands are not case-sensitive. Commands are case-sensitive.
Character forward slash ‘/’ is command line delimiter. Character forward slash ‘/’ is a directory separator.
Character backward slash ‘\’ is a directory separator. Character backward slash ‘\’ is an escape character.
Follows file naming convention of: 8 character file name + dot(.) + 3 character file extension. There is no file naming convention.


Test your coding skills here

 

Directory Operations

 

1. To List All the Files and Subdirectories Present in the Directory

Syntax:
$ ls 

Output: 
Desktop 
Documents 
Music

 

2. Find Out Current Directory You are in Right Now

Syntax:
$ pwd 

Output: 
/home/hackertrail

 

3. Create a New Directory

Syntax:
$ 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.

 

4. Remove/delete a directory

Syntax:
$ 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

 

1. 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.

Syntax: 
$ ps 

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

 

Where:

  • 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

 

Syntax: 
$ kill <process id>

 

Find out more on terminating processes is here

 

2. 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.

 

Syntax:
$ history

 

Output of history command in Linux
Credit: geek-university.com
 

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

 

Example:
$ history 5


Explore the Job Openings in APAC

 

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.

 

1. Search for a String in a single file

Example: 
grep “example” sample.txt 

Output: 
This is the line which contains a string called example.

 

2. Search for Multiple Strings

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

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

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

 

3. 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: 
fruit 
This is a vegetable. 
Apple fruit. 

Example: 
grep -v fruit example.txt 

Output: 
This is a vegetable.

 

4. 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.

Example: 
$ cat example.txt | grep Apple 

Output: 
Apple fruit.

 

5. Case Insensitive Search

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

Example: 
$ grep -i apple example.txt 

Output: 
Apple fruit.


Test your coding skills here

 

File Permissions

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

Example:
ls -l file1.txt

Output:
-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.

Syntax: 
$ chmod [OPTIONS] MODE filename

 

1. Symbolic Mode

Syntax:
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

 

2. Numeric Mode

Syntax:
chmod [OPTIONS] NUMBER FILE...

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.

 

3. 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.

Syntax:

chmod [OPTIONS] [PERMISSIONS] FILE(s)

Syntax:

chown [OPTIONS] [USER][:GROUP] FILE(s)

Example:

chmod 700 permissions/file2

Example:

chown user1 test/test1.txt


Explore the Job Openings in APAC

 

Backend Technology Interview Questions

C Programming Language Interview Questions | PHP Interview Questions | .NET Core Interview Questions | NumPy Interview Questions | API Interview Questions | FastAPI Python Web Framework | Java Exception Handling Interview Questions | OOPs Interview Questions and Answers | Java Collections Interview Questions | System Design Interview Questions | Data Structure Concepts | Node.js Interview Questions | Django Interview Questions | React Interview Questions | Microservices Interview Questions | Key Backend Development Skills | Data Science Interview Questions | Python Interview Questions | Java Spring Framework Interview Questions | Spring Boot Interview Questions.

 

Frontend Technology Interview Questions

HTML Interview Questions | Angular Interview Questions | JavaScript Interview Questions | CSS Interview Questions.

 

Database Interview Questions

SQL Interview Questions | PostgreSQL Interview Questions | MongoDB Interview Questions | MySQL Interview Questions | DBMS Interview Questions.

 

Cloud Interview Questions

AWS Lambda Interview Questions | Azure Interview Questions | Cloud Computing Interview Questions | AWS Interview Questions.

 

Quality Assurance Interview Questions

Moving from Manual Testing to Automated Testing | Selenium Interview Questions | Automation Testing Interview Questions.

 

DevOps and Cyber Security Interview Questions

DevOps Interview Questions | How to Prevent Cyber Security Attacks | Guide to Ethical Hacking | Network Security Interview Questions.

 

Design Product Interview Questions

Product Manager Interview Questions | UX Designer Interview Questions.

 

Interview Preparation Tips

Strength and Weakness Interview Questions | I Accepted a Job Offer But Got Another Interview | Preparation Tips For the Virtual Technical Interview | 7 Tips to Improve Your GitHub Profile to Land a Job | Software Engineer Career Opportunities in Singapore | What can you expect during a whiteboard interview | How To Write A Resignation Letter | Recommendation Letter Templates and Tips.

 

Quick Links

Practice Skills | Best Tech Recruitment Agency in Singapore, India | Graduate Hiring | HackerTrail Litmus | Scout - Sourcing Top Tech Talent in ONE Minute | About HackerTrail | Careers | Job Openings.

Related Articles

HACKERBUCK AWARDED