Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Linux Command Line Fundamentals

Jul 23, 2018 • 8 Minute Read

System Information Commands

The commands in this section will help sysadmins get to know the systems they are responsible for like the back of their hands:

  • When run without options, hostname will show the system's host name as defined in /etc/hosts or /etc/hostname. By adding -I (or its equivalent --all-ip-addresses), all the configured addresses on all network interfaces will be returned.

  • Disk usage and available storage space can be easily found out using du and df, respectively.

Strictly speaking, the former will estimate the file space usage by file or directory, whereas the latter will report the same information on a per-filesystem basis. Both support the -h flag, which formats the output of human-readable form (10K, 15M, 3G, for example) instead of using bytes.

When followed by a directory and the star wildcard *, the combined switch -sch allows du to summarize totals for the directory and each subdirectory within. Also, a grand total is provided. In other words,

      du -sch /var/log/*
    

will print the file space usage of all files and subdirectories inside, along with the grand total, for /var/log. To view only the grand total, remove the c from -sch.

  • Memory and CPU represent another essential set of information when it comes to planning the kind of software that can run on the system.

For example, you would not run a mail service on a machine with only 1 GB of RAM. To find out the architecture, number of cores, vendor id, model name, and speed (and more) of the CPU(s), you can use lscpu. On the other hand, the free command (which, by the way, also supports the -h for returning output in human-readable form) shows the amount of free and used memory in the system.

  • Uptime is a critical metric that indicates the time during which the system has been operational. If you're only interested in knowing how long the machine has been running, use the -p option. If you remove that flag, uptime will also return the current system time, the number of logged users, and the load average for the past 1, 5, and 15 minutes.

Text Processing Commands

It is often said that in Unix and its derivatives everything is a file, and thus the need to master command-line text processing tools. The following commands are a must for every system administrator. When in the Linux community we speak of everything being a file, we mean that every system object can be opened, read from, and written to as a file in the regular sense of the word (given you have the necessary permissions to do so).

  • To display the first or last ten lines of a file, use head or tail, respectively.

If you are interested in inspecting a different number of lines from the beginning or the bottom of the file, you can use head -n or tail -n followed by an integer and the name of the file. For example,

      head -n 2 /etc/passwd
    

will return the first two lines of /etc/passwd. Often, we will be interested in viewing the last lines of a file and monitoring it as more lines are added. This is the case when we want to monitor a system log as events are recorded. To do so, we use tail -f followed by the path to the log file, such as

      tail -f /var/log/httpd/access_log
    

Incidentally, the above command will allow you to monitor the access log of the Apache web server in a CentOS 7 server.

  • To search for patterns and regular expressions in files, grep is the tool for the job.

This command must be followed by a pattern, character class, or regular expression and the file where the search must be performed.

To illustrate, let's return the line(s) in /etc/passwd where the word student (a simple pattern) is found:

      grep student /etc/passwd
    

To ignore case so that both Student and StUdEnT are also matches, use

      grep -i student /etc/passwd
    

instead.

You can also use a regular expression instead of a simple pattern. To do so, add the -E switch as in

      grep -E 'svm|vmx' /proc/cpuinfo
    

where 'svm|vmx' is a regular expression that matches either the string svm or vmx. By the way, this command checks if virtualization is enabled on your CPU(s).

  • To search for system objects (most likely, files or directories we want to locate or work with), we'll use find.

First, we need to indicate the directory where we must start the search, the object type, and the name. Although there are other filter criteria that we can add to narrow down our search, this is a typical example of the use of find. For example,

      find /etc -type f -name sshd_config
    

will search for a regular file (type f) named sshd_config starting at /etc.

  • To extract sections from each line in a file, cut will be our best ally.

This tool is often used with -d to specify a delimiter surrounded in single quotes, and -f followed by a number, range, or a comma-separated list of files that indicate which section(s) we're interested in.

If you want to list the users (1st field) found in /etc/passwd with their default shell (7th field), you can use

      cut -d':' -f1,7 /etc/passwd
    

To add the home directories (6th field), you would use

      cut -d':' -f1,6-7 /etc/passwd
    

instead.

Empty Files, Redirection, and Pipes

Linux allows you to create empty files using the touch command followed by the name of the new file. This is useful to initialize files for a sandbox environment. If touch is followed by the name of an existing file, its modification time will be updated to the current system time.

  • To initialize a file and add content to it (or overwrite its contents) in a single step, we will use the > redirection operator followed by the name of the file. Keep in mind that the redirection operator must be preceded by the command whose output will be inserted in the file.

For example,

      cut -d':' -f1,7 /etc/passwd > usersandshells.txt
    

will create a file named usersandshells.txt in the current working directory (if it does not exist previously) or overwrite its contents (if such file exists) with the output of the command.

  • To avoid overwriting an existing file when adding content, use >> instead. This approach will also initialize the file if it does not exist.

It is important to not that using > or >> is not a one-size-fits-all solution, but depends on the specific scenario where it is to be utilized.

  • Most often, you will need to chain two or more commands together so that the output of one is sent as input to the next.

To do this, we will use the vertical bar |, also known as pipeline. Thus,

      cut -d':' -f1,7 /etc/passwd | grep student
    

will filter out the line containing the word student from the output of cut -d':' -f1,7 /etc/passwd.

  • To sort unique lines in a file or from the output of a command, we'll use sort and uniq together.

To illustrate, you can do

      cut -d':' -f7 /etc/passwd | sort | uniq
    

which will return the default shell(s) for the accounts listed in /etc/passwd. There is a good reason why sort follows the first pipeline and then sends its output to uniq and not the other way around. The latter requires its input to be sorted beforehand to return unique lines.

Please continue on to the next Guide in this Series User and Group Management in Linux to continue learning about Linux Administration.