Essential Linux Commands for Beginners With Examples

Understanding of Linux commands is essential for anyone using the Linux operating system. Hence, we’ve drilled down the most important commands which are extremely useful for beginners. These can be equally beneficial for the DevOps engineers and System admins. It’s a curated list of commands which either you can use in daily tasks or refer when needed.

If you’re working as a DevOps or SysAdmin or a C/C++ programmer using Linux for development, then these commands could turn out be quite useful for automating a lot of your tasks. You’ll find that we’ve targetted five core areas of Linux commands and also added a set of few quick tips at the end.

For those preparing for interviews should also go through the below Linux quizzes to assess the level of their preparations.

Now, let’s move on to checking the list of essential Linux commands that everyone using Linux should know.

Here is the table of content for quickly browsing through the commands.

Table of Content.

1. Commands To Check System Memory.
2. Commands To Check Disk/File Usage.
3. Commands To Check on Running Processes.
4. Commands To Monitor & Manage Network.
5. Commands To Configure Terminal & Screen.
6. Miscellaneous Commands for Quick Reference.

Essential Linux Commands for Beginners With Examples.

1. Linux Commands To Check System Memory.

1.1. The free command.

It’s the most frequently used command to track memory usage on Linux.

The “-m” option returns the usage data in MB format.

TOC

1.2. The </proc/meminfo> command.

Another way to quickly check the memory consumption is by printing the command. You need root or access to run this command.

TOC

1.3. The <vmstat> command.

It reverts with the memory usage in the same way as the command does. But you need not be a root user to run this command.

TOC

1.4. The top command.

If you are in a situation, where the memory usage is between 90-100%. Then, you should use top command to determine the process responsible. Most of the time, you can verify the process consuming resources by looking at the <%CPU> or the <%MEM> columns in the top output.

Linux Commands - Running Top

Running Top Command in Terminal

It also allows sorting on the columns. Press (Shift+O) to select a column via field letter. For example, press “a” letter to sort process with PID (Process ID).

Linux Commands - Sort Top Output

Sort Top Command Output

TOC

1.5. The <htop> command.

It’s an extension to the top command. And it provides several other options and details along with displaying the memory usage.

The top header in its output shows the CPU usage, RAM and swap statistics.

Linux Commands - Running HTOP in Terminal

Running HTOP Command in Terminal.

TOC

1.6. Additional Linux commands to isolate memory issues.

1. Print the top 10 processes consuming a lot of memory.

2. Kill a process while running the top command.

Run top and press “k”. It’ll prompt you for the process ID and ask for the signal to kill. You can enter the PID of your choice and provide 15 as the signal value.

Linux Commands - Kill a Process With Top Command

Using Top Command to Kill a Process.

3. Modify top command to print the absolute path in its output.

After running top, press “c” to display the processes with full path and arguments.

4. Sort “ps” output by memory usage, from high to low.

TOC

2. Linux Commands To Check Disk/File Usage.

2.1. The du command.

It’s one of the standard Linux commands to retrieve the disk usage of files and folders.

1. If you wish to check the disk usage of a folder and its subfolder, then run the following command.

It displays the output in the form of disk blocks. To print in bytes, kilo bytes, mega or Gigabytes, use the “-h” option with du command.

2. To check the total space occupied by a directory, use the “-s” option with the du command.

3. Using “-c” switch will get the total size of the directory including the subdirectories printed at the last line. There is also an “-a” flag to display the usage of all files and folders.

4. With the “–exclude” switch, you can specify a file pattern. Then, the du command will filter the files matching the given pattern.

5. Locate the biggest files in the current directory and sub-directories.

6. Look out for the largest directories.

TOC

2.2. The df command.

Another Linux command to monitor disk space is df (disk free). Now, let’s see what can we do with it.

1. Print the disk usage of all the file systems.

2. Use a fixed memory block size.

By default, it prints memory blocks of 1K. But with the “-B” option, we can alter the default memory size.

3. Style output to human readable format.

Use “-h” option, it makes the display memory in the form of gigabytes, megabytes, etc.

4. Check the type of available file systems.

You can run the df command with “-T” option to show the type of all file systems.

You can even exclude a file system with -x option. Or provide a type with -t option, it’ll result in displaying the same kind of file systems.

TOC

2.3. Additional Linux Commands for file management.

There are some other useful tips that you can use to optimizing your disk management tasks.

1. Delete files marked for deletion but not yet deleted.

2. Search for files more than 100 MB.

3. Check files created within the last one week.

4. Remove files older than two weeks.

5. Monitor a log file for errors or some text.

TOC

3. Linux Commands To Check on Running Processes.

3.1. The ps command.

It’s one of the most used Linux commands which returns a preview of the running processes along with their PID, CPU/RAM usage, and other details.

1. Check the Shell you are using.

2. Check the processes not own by you.

grep -v option inverts the selection.

3. Remove grep command while filtering the process list.

4. Display all process including params and hierarchy.

5. List the files opened by a process.

6. Print the process running time, since it started.

7. Check all threads of a running process.

TOC

3.2. The kill command.

There come situations when you’ve to stop a process behaving intermittently and which refuse to close itself. So the kill command comes for rescue in such conditions. Its syntax is as follows.

1. Terminate a process by ID.

The “-9” flag refers to KILL signal. Some of the other signals are HUP (-1), SIGINT (-2) and TERM (15).

TOC

3.2. The killall command.

Another Linux command which kills a process by name is as follows.

You can even kill multiple processes using the single command.

TOC

3.3. The strace command.

It is one of the Linux commands which allows watching a process execution. It intercepts the system calls and signals that a program exercises while running.

1. Trace a program using its PID.

2. Monitor a process for any specific system call.

TOC

3.4. The watch command.

Sometimes, we need to monitor a process at regular intervals. For example, tracking the progress of copying a large release build or folder. That’s where the watch command is useful.

1. Run ls command after every 1 second.

2. See the differences between previous and present output.

You can anytime press CTRL+z to take control back from the watch command.

TOC

4. Linux Commands To Monitor & Manage Network.

4.1. The IFCONFIG and related commands.

The most common usage of command is to return the IP address of the system you are using. But you can also use it to initialize an interface, assign a new IP address and enable/disable the interface.

1. The below command will print the IP address of the current machine.

2. To print the information related to a particular interface, run the following.

3. Assign IP address and set gateway.

4. Enable/disable an interface.

TOC

4.2. Some nice ping command examples.

It’s basically to find out whether a machine on the network or the gateway is reachable. Here are some good examples.

1. Increase/decrease ping time interval.

2. Check if local I/F is active.

3. Update the size of the ping packet.

The above command changes the default packet size from 56 to 112.

4. Timeout a ping request.

5. Send X no. of packets and stop.

TOC

4.3. The TRACEROUTE command.

It’s for troubleshooting the network issues. It prints the no. of hops taken to reach the target.

TOC

4.4. The NETSTAT command.

This command allows a user to monitor both incoming and outgoing network connections. Most of the operating systems support this command. Let’s see some of its real-time applications.

1. Listing all TCP and UDP connections opened at a time.

2. List connections without resolving host, port and user name.

3. Listing only TCP connections.

4. Listing only UDP connections.

5. Display TCP connections in Listen state.

6. Display UDP connections in Listen state.

7. Display service names with PID.

8. Print routing table summary.

9. Retrieve IP address statistics.

10. Locate all programs in Listen state.

11. Find the port of a program is using.

12. Show all ports listening with process PID.

TOC

4.5. The dig command.

The full form of DIG command is domain information groper. It retrieves the DNS details like A record, CNAME, and MX records.

1. Display only MX Records.

2. Display only NS Records.

TOC

4.6. Miscellaneous network Linux commands.

1. Display all TCP sockets in use.

2. Resolve IP address from the hostname.

3. MTR – Top like traceroute command.

It combines the functionality of traceroute and ping tools. It examines the connectivity between the host and the given target system.

TOC

5. Linux Commands To Configure Terminal & Screen.

5.1. The screen command.

It might now be available on your system by default. So, to install it either use apt or yum as per the distribution you are using.

1. Start a screen session as the current user.

2. Reattach to a screen session.

3. Record a terminal session.

4. Play back a recorded terminal session.

TOC

5.2. Some useful terminal commands.

1. Open a new terminal.

2. Open a new tab in the existing terminal window.

3. Clear the screen.

4. Cache console output.

TOC

6. Some Important Linux Commands for Quick Reference.

1. Run Previous command as root.

2. Find differences between two directories.

3. Lock a directory.

The root user will still have access. To restore the permission, run the below command.

4. Smart cd commands.

5. Replacing same text in multiple files.

To replace the text Apple with Linux in all text files in current directory and down you can run this.

6. Listing files changed today.

Sometimes, we create a file during the day and forget what name we gave it. So here is the command to locate such files.

7. Shorten long commands.

Not all of us has the ability to memorize the long Linux commands. With the below command, we can assign user-friendly names in such cases.

8. Copy a file into multiple directories.

9. Disable incoming ping requests.

To block a flood of incoming pings, do the following.

To turn the above setting back, run the below command.

10. Some cool Nautilus tricks.

  • Press CTRL+l to open a location.
  • Use CTRL+up to open the parent directory.
  • Press arrow keys to navigate through folders.

TOC

Keep Learning,

TechBeamers.

Leave a Reply


*