More used linux commands

Most Commonly Used Linux Commands You Should Know

Linux is a very popular Operating System (OS) amongst programmers and regular users. One of the main reasons for its popularity is its exceptional command line support. We can manage the entire Linux operating system via command line interface (CLI) only. This allows us to accomplish complex tasks with a just few commands.

In this guide, we will discuss some commonly used commands that are useful for experienced sysadmin or a beginner. After following this guide, users will be able to operate the Linux system confidently.

For better organization, these commands are grouped under three sections – file system, networking, and system information.

Linux File System Commands

In this section, we will discuss some of the useful commands related to files and directories in Linux.

1. cat Command

The cat command is mainly used to display the file contents. It reads the content of the file and displays them on the standard output (stdout).

The common syntax of the cat command is:

Let’s display the contents of the /etc/os-release file using the cat command:

View File Content in Linux

Additionally, we can also use the -n option of the command to display the contents with the line number:

View File Content with Line Numbers

2. cp Command

The cp command is useful for copying files, groups of files, and directories.

The common syntax of the cp command is:

Here, the square brackets ([]) represent the optional arguments whereas angular brackets (<>) represent the essential arguments.

Let’s copy the /etc/os-release file to the /tmp directory:

$ cp /etc/os-release /tmp/new-file.txt

Now, let’s display the contents of the file to verify the file has been copied:

Copy File in Linux

Similarly, we can copy the directory using the cp command. Let’s copy the /etc/cron.d directory inside the /tmp directory:

We have used the -r option with the cp command, which represents the recursive operation. It copies the directory recursively which includes its files and sub-directories.

In the next example, we will see how to verify that the directory has been copied successfully.

$ ls /tmp/cron.d $ ls -l /tmp/cron.d

Recursively Copy Directory in Linux

3. ls Command

The ls command is used to list the directory contents and sort files by size and last modified time in descending order.

Читайте также:  Windows гостевая linux общие папки

The common syntax of the ls command is:

If we don’t provide any argument to the ls command then it lists the contents of the current directory.

List Current Directory Files

In the previous example, we copied the /etc/cron.d directory to the /tmp directory. Let’s verify that is present there and contains the required files:

We can use the -l option with the ls command to display more detailed information like – file permissions, owner, timestamp, size, etc.

Let’s find out more details about the files present in the /tmp/cron.d directory:

List Directory Files

4. mkdir Command

We often create a directory structure to organize the contents. In Linux, we can use the mkdir command to create a directory or multiple directories and set the correct permissions for the directories.

The common syntax of the mkdir command is:

Let’s create a directory with the name dir-1 in the /tmp directory:

Now, let’s verify that the directory has been created:

Here, we can see that the ls command doesn’t report any error which means the directory is present there.

Sometimes, we need to create a nested directory structure for better data organization. In such cases, we can use the -p option of the command to create a few nested directories under the /tmp/dir-1 directory:

$ mkdir -p /tmp/dir-1/dir-2/dir-3/dir-4/dir-5

In the above example, we have created 4 levels of the nested directories. Let’s confirm it using the ls command:

Here, we have used the -R option with the command to display the directory contents in a recursive way.

Create Directory in Linux

5. history Command

To audit the last executed commands, you can use the history command, which displays the list of last executed commands in a terminal session.

List Linux Commands History

To view the command history with a time stamp, you need to set the timestamp in bash history, run:

$ HISTTIMEFORMAT="%d/%m/%y %T " #Temporarily set the history timestamp $ export HISTTIMEFORMAT="%d/%m/%y %T " #Permanently set the history timestamp $ history

List Last Executed Commands with Timestamp

6. du Command

How will you check the top 10 files that are eating out your disk space? A simple one-liner script made from the du command, which is primarily used for file space usage.

$ du -hsx * | sort -rh | head -10

Find Files Using Most Disk Space in Linux

Explanation of above du command options and switches.

  • du – Estimate file space usage.
  • -hsx – (-h) Human Readable Format, (-s) Summaries Output, (-x) One File Format, skip directories on other file formats.
  • sort – Sort text file lines.
  • -rh – (-r) Reverse the result of the comparison, (-h) to compare the human-readable format.
  • head – output first n lines of file.
Читайте также:  Delete programs on linux

7. stat Command

The stat command is used to get the information about the file size, access permission, access time, and the user ID and group ID of the file.

Check File Access Information in Linux

Linux Networking Commands

In this section, we will discuss some of the networking commands that beginners can use to troubleshoot network-related issues.

8. ping Command

One of the very common operations performed in any network is to check if a particular host is reachable or not. We can use the ping command to check the connectivity with the other host.

The general syntax of the ping command is:

Here, the destination can be an IP address or a Fully Qualified Domain Name (FQDN) such as google.com. Let’s verify that the current system can communicate with google:

Ping Host in Linux

In the above example, the command shows the statistics about network communication, which shows that the response is received for all four network requests (packets). It is important to note that, we have used the -c option with the command to limit the number of requests to be sent to the particular host.

Let’s see the example when the communication between the two hosts is broken.

To simulate this scenario, we will try to reach a non-reachable IP address. In this case, it is 192.168.10.100:

Ping IP in Linux

Here, we can see that we didn’t receive a response for any network request. Hence the command reports the error – Destination Host Unreachable.

9. host Command

Sometimes, we need to find the IP address of the particular domain. To achieve this, we can use the host command, which performs a DNS lookup and translates FQDN to IP address and vice-versa.

The general syntax of the host command is:

Here, the destination can be an IP address or FQDN.

Let’s find out the IP address of google.com using the host command:

Find IP of Domain

10. whois Command

All the details about the registered domains are stored in the centralized database and can be queried using the whois command, which shows details about the particular domain.

The general syntax of the whois command is:

Читайте также:  Linux как посмотреть логин

Let’s find out details of the google.com:

Find Domain Whois Information

Here, we can see much detailed information like – domain registration/renew/expiration date, domain provider, and so on.

It is important to note that, the whois command is not available by default on all systems. However, we can install it using the package manager. For example, on Debian-based distributions we can install it using the apt package manager:

On RHEL-based and other distributions, you can install it as shown.

$ sudo yum install whois [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a net-misc/whois [On Gentoo Linux] $ sudo apk add whois [On Alpine Linux] $ sudo pacman -S whois [On Arch Linux] $ sudo zypper install whois [On OpenSUSE]

Linux System Information Commands

In this section, we will discuss some of the commands that can provide details about the current system.

11. uptime Command

It’s a very common requirement to find when the system was rebooted last time using the uptime command, which tells how long the system has been running.

Let’s find out the uptime of the current system:

$ uptime -p 12:10:57 up 2:00, 1 user, load average: 0.48, 0.60, 0.45 

In this example, we have used the -p option to show the output in the pretty form.

12. free Command

Users often need to find the details about the installed, available, and used memory. This information plays an important role while troubleshooting performance issues. We can use the free command to find the details about the memory:

Here, we have used the -m option with the command which shows the output in the mebibytes.

Check Linux Memory Usage

In a similar way, we can the -g , -t , and -p options to show the output in the gibibytes, tebibytes, and pebibytes respectively.

13. lsblk Command

Computer systems store data on block devices. Examples of block devices are Hard Disk Drives (HDD), Solid State Drives (SSD), and so on. We can use the lsblk command to display detailed information about the block devices:

In this example, we can see that there is only one block device and its name is /dev/sda. There are three partitions created on that block device.

Check Linux Device Info

In this article, we discussed some of the commands that are useful for Linux beginners. First, we discussed the file system commands. Then we discussed networking commands. Finally, we discussed some commands that provided details about the current system.

Источник

Оцените статью
Adblock
detector