Linux commands cheatsheet for every developer
The tasks of the developers were previously confined to coding, testing and pushing of code to the repository. A developer was never bothered about where the application was run and how the environment or the server was set up. Now with the DevOps culture taking a front seat, it is imperative for a developer to know the basic commands and working of the servers where their programs are running. In this post, we are going to see the most common Linux commands that a developer should know for supporting basic DevOps.
About 90% of the production servers operating in the cloud are run on Linux operating system. Google and Amazon have very good support for the Linux servers and there are different flavours of Linux available. Though there are different releases, the commands are mostly similar across them. As an icing on the cake, Mac OS which itself is based on Unix also recognizes most of the commands we are about to discuss.
Following would be a list of common Linux based operating systems.
- Centos – This is the most common flavour of OS for the enterprise environment. This managed by RedHat enterprises.
- Ubuntu – Another very common choice of Linux. This most widely used on Desktops and they have a dedicated trimmed down version of servers which is used in many mission-critical applications.
Each of the above listed operating systems has got their own package manager ( for installation of programs and dependencies), but the commands we discuss are going to be common for all.
Let’s see what are common commands we would need as a developer in a Linux system. We will be having it organized based on the operations. Please note that you need to either run a local Linux system or have access to a Linux terminal for trying out the commands.
In case you are the TL;DR person, you can refer to the following gist for the commands:
Linux command cheatsheet gist
I would suggest to go through the details to understand each command and keep the gist handy for quick reference.
Connecting to the remote server using SSH
The first command that you would need to connect to a server is SSH. This command allows you to connect to a remote server using its IP or the hostname. Following would be the basic usage.
ssh email@example.com ssh firstname.lastname@example.org
This will ask for the password and you can enter the password for the provided user. On successful authentication, you will be provided with a shell where you can enter the commands and access the files on the server.
Connecting using a private key pem file.
There are cases where you would need to access the remote server using a key file ( pem file ). In that case, you need to specify the key file location using the following format
ssh -i /path/to/file.pem email@example.com
In case you want to get the verbose output, you can use -v flag as below.
ssh -v -i /path/to/file.pem firstname.lastname@example.org
Directory navigation commands
Once you have access to the terminal, you would need to move around the locations or get the current location. Let’s see what are the commands.
Getting the current working directory
Type the following command to get the current directory
This will print the current directory ( the directory where you are right now )
Changing the directory
To change to a particular directory, you can type the following command in the terminal
Listing of files
We can use the following command to list the files in a directory
You can see that the above commands list the files. This will also show the size of the file, permissions and the owner and group of the file.
You can list the hidden files using the following version of ls.
Listing files based on created time
We can list the files in the order of creation using the following command.
Let’s see the commands to manage the directories.
Creating a directory
This will create the directory in the current location with name-of-dir
Removing a directory
This will delete the directory provided. This command will only work when the directory is empty.
Removing all the files in a directory
For deleting all the files, we need to use the following command
rm -rf /path/to/dir
This will delete all the files in the directory recursively and the -f will make sure that no confirmation is asked for the deletion.
Please be careful about the rm -rf command as this could delete the files on the specified directory. Make sure that you specified the correct path without any space .
Basic vi commands
The most common editor available in the Linux environment would be vi. But this editor uses some special key combinations and is terminal based. Let’s see how to use vi editor.
Opening a file
You can use the following command to open the file.
This will open the file as below
For editing a file, you need to press i and will show “INSERT” mode at the bottom.
Now you can move around the file using arrow keys and insert content. Now to save the file, you need to press <Esc> key and this will put the editor in the command mode.
Saving the file
To save the file, you need to be in the command mode ( by clicking on the <Esc> mode ) and then press the following keys
Quitting the file
Quitting the file by ignoring changes
Tailing the file
This is one common requirement when we are monitoring the logs of an application. We would need to see the logs as it is coming in the file. We can use the following command for that
tail -f /path/to/the/file.log
This would display the content as its coming.
Listing the last 10 lines
tail -n 10 /path/to/file.log
This will list the last 10 files from the log file.
Resource monitoring ( CPU, Memory, free space )
Most of the times we would need to check the resource usage of a server or a particular process. Let’s see what are the commands available for the same.
TOP command for resource usage
You can run the top command it will list the CPU usage, RAM and the commands.
You can see the relevant details in the highlighted section.
Getting the free memory
You can also get the free memory in the system using the following command:
This will show the total memory and the available memory in the server
Disk space usage
Run the following command for getting the disk usage details
This will display the disk mounts and the available as well as free space.
Size of the files and folders in a location
Run the following command to see the files and folder sizes
du -sh *
This will display the folders, files and the corresponding sizes.
Downloading from a link
Most often we would need to download a file from the internet or access a URL file. We can use wget for the same using the following format.
This would download the file.zip to the current directory using HTTP. You can also the same to download an HTML file
wget may not be installed by default in the server. You may need to get it installed using the respective package manager for the operating system.
Calling an API from command line
We can use cURL command to call an API using GET, POST, PUT etc.
Making a GET call
curl -X GET "http://server:port/api/endpoint"
Placing a GET call with headers
curl -H "Authorization:Basic xxxx" -X GET "http://server:port/api/endpoint"
We can use multiple -H flags and pass multiple headers
Making a POST call with params
curl -H "Authorization: Basic xxx" -H "Content-Type: application/x-www-form-urlencoded" --data "grant_type=password&username=test&password=secret&client_id=trustedService" -X POST http://localhost:50002/oauth/token
Here we are passing the “Authorization”, “Content-Type” header and the post params using –data
Running a command continuously
There are situations wherein we would need to monitor the status of a file transfer or run a command continuously. We could use the watch command in these cases. Following is the format.
In the below example we are using watch on the listing and we can see the file changes every 2 seconds
watch ls -l
Getting the network details ( IP address )
Another very common command we need to use is for getting the network details ( IP address, hardware Mac address and the interfaces). We can use the ifconfig command for that. This will generate a response like below which lists the interfaces, IP address and the hardware address ( Mac address )
Finding the process id of an application using its port
We have our applications ( Java spring boot mostly ) running in remote servers and it’s not easy to just find process id when multiple services are running in the same server. In those cases, we can use the following command
In the below sample, we are trying to find out the process id for elasticsearch using the port on which it is running.
Killing a process
We can kill a process by using the following command
kill -9 processid
Here -9 is a kill status which means a graceful kill command. The application will be given an instruction to shut down and the process any cleanup.
You can also kill by the name of the process using the following command.
The commands we have discussed above is only the basic commands in Linux. There are different combinations and switches that extend the above commands and lot of other commands that we can use. I will try to keep this list updated with more suggestions and common commands.
Let us know your queries and comments.