Commands
Aniket Kulkarni  

Linux Command Basics

Linux is an open-source operating system. I personally feel the power of Linux is using the commands. The Linux command line is a text interface to your computer. Often referred to as the shell, terminal, console, prompt, etc. In this post, I am going to explain the most commonly used Linux commands.

ps – Process Status

To get the information about the process. For example, to check if the Java process is running or not, use the following command

ps -ef | grep java
Get the amazon ssm agent process running information

In the above image, I checked the amazon SSM agent process information. There are various options available using ps command

Help information on ps command

netstat – Network Statistics

Displays the socket information (such as TCP, UDP, etc), listening server sockets, PID/Program name for sockets, routing table. Helpful to identify the process running on which port.

netstat -tulpn
netstat command’s output, showing Protocol, Ports, and Process Ids

lsof – List of Open File

The command is used to retrieve the information about files that are opened by various processes. Using an option -i select IPv4/IPv6 files

lsof -i tcp:9090
lsof command with -i option to show tcp port 80 opened files

top

  • Shows the top processes currently running.
  • For example, if you are running any Java process or RAM available etc.
  • Information is updated dynamically.
  • Press the s button to change the delay in seconds default is 3 sec.
  • Once you set the delay then it updates the screen every specified number of seconds.
  • To quit press the q button.
top
Top command output

df – Disk Free

To check the Hard Disk space available. This is often useful to get insights into the space requirements. You can avoid a situation where you run out of disk space.

df -h
df command

The -h option shows the data (memory) in the human-readable format.

df -h command output

du (Disc Usage)

To get the disc space (size of the folder) used by all folder(s). It will list all the folders recursively.

du
du command

Human-readable format with -h option

du -h

To print the overall size of the current folder use du -sh. This will calculate and sums all the recursive folders’ sizes as well.

du -sh
du -sh

kill

Kill the running process by mentioning Process ID (PID).

kill -9 <pid>

To forcefully kill the process use the -9 option.

sed

It is mostly used to replace the text in a file.

sed command

In the above example, I have created a file using the two echo statements. There are two lines in the myfile.txt file. Now, using sed I replaced the word love with like. This is really helpful when there is a requirement to replace a particular word in the file.

awk

By default Awk prints every line of data from the specified file. 

awk command

It prints all the file content. Now, let’s try to find the word Java in the myfile.txt file.

A search of a specific column using awk command

It displays all the lines which contain the Java word. This is cool. We can easily search words. But, what if we want only specific columns from the file. Let’s say you are interested in columns number 2nd and 5th. Well, there is an option to get only specific column(s) from the file.

Display specific columns using awk command

Remember column numbering starts from 1.

find

Search a file in the Linux directories. When you are dealing with the Linux shell, it is hard to find the file. As there is no GUI. In this case, find commands comes to the rescue.

find command

Command description: find <where to look for files> <option> <file name>
Find the file yum.log in the root (/) directory. It uses an option -name to specify a name of a file to look for. It does the recursive search.

ls – List

To list the contents of a directory. This is the most commonly used command to search for a particular folder or file in the Linux OS.

ls command

ll

It is similar to using the ls -l command. Basically, it lists the contents of a directory in a long listing format. It provides more information about the directory and files. Information includes permissions, owner, size, last modified date, etc.

ll command output

pwd – Print Working Directory

It prints the current working directory.

pwd command

rm – Remove file or directory

This command deletes a file or a directory.

rm <file_name>           # deletes a file
rm -rf <directory_name>  # this deletes all the sub directories wihin it
rm file
rm directory

To delete multiple files with a similar pattern, there is an efficient way to do it.

rm *.txt    # deletes all text files

mkdir – Make/Create Directory

It is used to create a directory.

mkdir <dir_name>

To create multiple directories in one go, use -p option. If you doubt if the directory is present or not, always use this option to create directories in directories.

mkdir -p mylearning/shell/commands    # creates all the directories (dir in dir)

cat – concatenate

Reads the content of a file and shows it as an output. One or more file names can be specified as an input to the command

cat <file_name1> 
cat <file_name1> <file_name2>
cat command

vi – Visual

It is a text editor available in Linux. It is a powerful editor with edit, search, replace capabilities.

chmod – Change Mode

To change the access permission of a file chmod command is used. To illustrate the use of chmod, refer to the below example.

chmod command
  • Created a shell script file named myshell.sh with a command “pwd”.
  • Used ll command to check the permissions of file myshell.sh.
  • The owner of the file has read and write permissions but not the execution permission.
  • This is verified using executing the shell script with “./myshell.sh”.
  • Added the execution permission using chmod command.
  • After giving execution permission to a shell file is executed without any error.

chown – Change Owner

To change the ownership of a file or directory.

chown command example

Changed the owner of myshell.sh file from root to ec2-user.