What are some essential Unix/Linux commands?

Mohan Pawar
3 min readOct 22, 2021

Linux is an open-source operating system used for various purposes.

Linux includes many commands that can be performed on the command-line interface.

The most commonly used commands are provided below.

Commands

1. who

who is a command used to display the users who are currently logged in on the server.

who

2. grep

grep is used to search for a given string in a file.

For instance, we would use the following command to search for “user” in the user_file.txt file:

grep "user" users_file.txt

3.chmod

chmod is a command used to change the file permissions.

The permissions can be w for write, r for read, or x for execute.

They can also be represented by their numerical representation, using a combination of the following numbers:

  • 4: stands for read
  • 2: stands for write
  • 1: stands for execute
  • 0: means no permission
chmod +w file.txt
chmod +r file.txt
chmod +x file.txt
or
chmod 755 file.txt

4. ssh

ssh is known as secure shell. It is a protocol used to securely connect to a remote server.

ssh -l username remotehost.com/IP-address

We can also get the logs using the verbose mode:

ssh -vvv -l username remotehost.com/IP-address

5. du and df

du is used to estimate file space usage.

df is used to create the disk usage summary. It is especially used to determine how much space a particular directory or file takes.

For example, to find the summary of disk usage for a home directory with all its sub-directories, we would use:

du /home

To get the disk usage of file/directory with sizes in human-readable format, i.e., in KB, MB, etc:

du -h /home

For the total disk size of a home directory:

du -sh /home

To display information of device name, total blocks, total disk space, used disk space, available disk space, and mount points on a file system:

df

To display information of a particular partition:

df -hT /etc

To find top files which are consuming more space with its subdirectories:

du -s * | sort -nr | head -5

To find the largest file in the current directory:

du -a /home | sort -nr | head -n10

Some basic functionalities

  1. To swap every two lines in a file:
awk '{getline x; print x;}1' file
  1. To replace a string with another string in a file:
sed 's/word_to_replace/new_word/g' myfile.txt
  1. To find out the total words number of words in a file:
grep -io 'unix' file | wc -l
  1. To join every two lines in a file:
sed 'N; s/\n/,/' file

5. To reverse the content of a file.

nl file | sort -nr | cut -f 2-

nl is a linux command used to number lines.

  1. To find the number of directories in the current directory:
ls -l | grep "^d" | wc -l
  1. To check the free disk space on the server:
vmstate ==>> virtual memory state
free ==>> free and used physical memory and swap memory
  1. To check the CPU and memory usage of Linux servers:
top

top is a command utility to monitor the Linux system. It provides a summary of the processes currently running on the system.

Zombie processes

A zombie process is a process that is already in the terminated state and has completed its execution but that still has an entry in the process table.

When a process is completed in Linux, it is not removed from the system immediately. Instead, the process descriptor stays in memory, and its parent process is notified by the SIGCHILD signal that the child process has finished execution.

In order to find all zombie processes:

ps aux | grep 'Z'
or
ps -el | grep defunct

In order to kill a zombie process:

kill -s SIGCHILD ppid

Where ppid is the parent process ID.

Inodes

An inode is a data structure that contains metadata about a file when it is changed, accessed, or modified.

The file is assigned a numeric value identifier known as the inode number.

The inode number — also called the index number — stores pieces of information such as:

File type
Size
Permissions
Owner name
Group owners name
Last access/modification time
ACL settings
Link count
Pointer to the memory location where files data is actually stored.

1. Check Inodes on the filesystem

We can check the total number of inodes present on the file system using the disk space command as follows:

df -i /dev/sda1

2. Find the Inode number of any file

ls -il abc.txt
1150561 -rw-r - r - 1 mohan mohan 0 Oct 13 03:07 abc.txt

Originally published at What are some essential Unix/Linux Commands? on educative.io platform.

--

--