Linux All-In-One For Dummies
Book image
Explore Book Buy On Amazon
Here are five useful Linux commands that will come in handy for your day-to-day Linux chores: tar, shutdown, free, df, and locate. After mastering these, check out our larger LIST OF BASIC LINUX COMMANDS to expand your skillset.

tar

The tar command was originally designed to be used to create backup copies of files on tape — in fact, tar actually stands for Tape ARchive. The tar command is the most common way to create a compressed archive of one or more files that you can easily move from one computer system to another.

You can use the tar command to create an archive of an entire directory like this:

tar –cvf archive.tar dirname/
In this example, the switch -cvf invokes three options: c, which creates a tar archive; v, which runs tar in verbose mode so that the files added to the archive are individually listed; and f, which provides the name of the archive file to be created. Next comes the name of the archive file (in this case, archive.tar). And finally comes the name of the folder that contains the files to be archived.

To extract files from an archive, use this command:

tar –xvf archive.tar
Here, the -x is specified instead of -c to extract rather than create the archive. The contents of the specified archive file (in this case, archive.tar) are extracted to the current working directory.

shutdown

An easy way to shut down a Linux system from a command prompt is to issue the shutdown command. To perform an immediate shutdown, enter this command:
shutdown now
To reboot a system immediately, enter this:
shutdown -r now
To schedule a reboot for a certain time, such as 2:00 a.m., enter this:
shutdown -r 2:00
To reboot an hour from now, enter this:
shutdown –r +60

free

The free command lets you know how much free memory is available on a Linux system. Simply type the command free and you'll see output similar to the following:
        total   used     free shared  buff/cache  available
Mem:  4030488 961888  1795920   2228     1272680    2803356
Swap: 2097148      0  2097148
You can also use the -h switch to convert the numbers to KB, MB, or GB so they're easier for a human to read:
      total   used    free   shared  buff/cache  available
Mem:   3.8G   938M    1.7G     2.2M        1.3G       2.7G
Swap:  2.0G     0B    2.0G
Here, you can see that the system has a total of 3.8GB of RAM, of which 938MB is used and 1.7GB is free.

df

The df command, short for disk free, lists the amount of free space available on all the disk volumes on a Linux system. For each mounted disk, df lists the total amount of disk space, the amount of space used, the amount of space that is free, and the percentage of used space.

For example, enter the command df with no switches and you'll see output similar to this:

Filesystem  1K-blocks    Used Available Use% Mounted on
devtmpfs      2005408       0   2005408   0% /dev
tmpfs         2015244     228   2015016   1% /dev/shm
tmpfs         2015244    1336   2013908   1% /run
tmpfs         2015244       0   2015244   0% /sys/fs/cgroup
/dev/sda5   154803352 5044044 149759308   4% /
tmpfs         2015244      96   2015148   1% /tmp
/dev/sda2      289293   92512    177325  35% /boot
tmpfs          403052       8    403044   1% /run/user/42
tmpfs          403052      24    403028   1% /run/user/1000
Here, the disk space for each volume is listed in units of 1KB blocks, which translates to 2GB of disk space. Thus, the first drive (devtmpfs) has a total of 2,005,408 1KB blocks.

You can use the -h switch to get more readable results:

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        2.0G     0  2.0G   0% /dev
tmpfs           2.0G  228K  2.0G   1% /dev/shm
tmpfs           2.0G  1.4M  2.0G   1% /run
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda5       148G  4.9G  143G   4% /
tmpfs           2.0G  112K  2.0G   1% /tmp
/dev/sda2       283M   91M  174M  35% /boot
tmpfs           394M  8.0K  394M   1% /run/user/42
tmpfs           394M   28K  394M   1% /run/user/1000

locate

The locate command can be very helpful if you can remember some or all of a filename but you're not sure what directory the file is in. For example, suppose you need to find the directory that contains the file httpd.conf. To do so, enter this command:
locate httpd.conf
You'll be rewarded with the location of any file in your system with named httpd.conf. On my Linux system, there are two:
/etc/httpd/conf/httpd.conf
/usr/lib/tmpfiles.d/httpd.conf
You can use wildcards if you aren't sure of the exact filename. For example, to find all files with the extension .conf, use this command:
locate *.conf
The result will be a listing of hundreds of files, which can be difficult to read. To limit the display to just one screen-full at a time, you can pipe the output to the more command, like this:
locate *.conf | more

About This Article

This article can be found in the category: