Learn about various commands used for disk manipulations with a variety of options.
- [Instructor] Hi, and welcome to the eighth section of this course, Put on the Monitors Cap. An operating system consists of a collection of system software that's designed for different purposes. It's a good idea to monitor each of these programs, in order to know whether they are working properly or not. We will also use a technique called "logging", by which we can get important information in a file, while the program is running. The content of this file can be used to understand the timeline of operations that are taking place in a running program or daemon.
For instance, if an application or a service crashes, this information helps to debug the issue, and enables us to fix any issues. This section deals with the different commands that can be used to monitor different activities. It also goes through logging techniques and their usages. Now we move on to the first video of section eight, Monitoring Disk Usage. In this video, we'll illustrate various commands used for disk manipulations, with a variety of options. Disk space is a limited resource.
We frequently perform disk usage calculation on storage media, such as hard disks, to find out the free space available on them. When free space becomes scarce, we find out large files to be deleted or moved, in order to create free space. In addition to this disk usage manipulations, they're also stored in shell scripting contexts. Df and du are two significant commands I use for calculation disk usage in Linux. The command, df, stands for disk free, and du stands for disk usage.
Let's see how we can use them to perform various tasks that involve disk usage calculation. To find the disk space used by a file or files, use ... (silence) The result is, by default, shown as size in bytes. To obtain the disk usage for all files inside a directory, along with the individual disk usage for each file, shown in each line, use minus a output results for all files in the specified directory or directories, recursively.
Running du directory will output a similar result, but it will show only the size consumed by subdirectories. However, this does not show the disk usage for each of the files. For printing the disk usage by files, minus a is mandatory. An example of using du directory is as follows.
Let's go through additional usage practices for the du command. Displaying disk usage in KB, MB, or blocks, be default, the disk usage command displays the total bytes used by a file. A more human readable format is expressed in units, such as KB, MB, or GB. In order to print the disk usage in a display-friendly format, use minus h, as follows. (silence) Displaying the grand total sum of disk usage.
If we need to calculate the total size, taken by all files or directories, displaying individual file sizes won't help. Du has an option, minus c, such that it will output the total disk usage of all files and directories given, as an argument. It appends a line, size, total, with the result. Syntax is as follows.
(silence) Minus c can be used along with other options, like minus a and minus h, in which case, they'll produce their usual output with an extra line, containing the total size. There is another option, minus s, summarize, which will print only the grand total as the output.
It will print the total sum and the flag, minus h, can be used along with it, to print in human-readable format. This combination has frequent use in practice. Printing files in specific units, we can force du to print the disk usage in specified units. For example, print the size in bytes, by default, by using ...
Print the size in kilobytes, by using ... Print the size in megabytes, by using ... Print the size in the given block size, specified by using ... Here, block underscore size is specified in bytes. An example consisting of all the commands is as follows.
Excluding files from the disk usage calculation. There are circumstances where we need to exclude certain files from the disk usage calculation. Such excluded files can be specified in two ways, wildcards. We can specify a wildcard as follows. Exclude list. We can specify a list of files to be excluded from a file, as follows.
(silence) There are also some other handy options available with du to restrict the disk usage calculation. With the minus minus max minus depth parameter, we can specify the maximum depth of a hierarchy du should traverse, while calculating disk usage. Specifying a depth of one calculates the size of files in the current directory. A depth of two specifies to calculate the files in the current directory, and the next subdirectory, and so on.
For example, du can be restricted to traverse only one file system, by using the minus x argument. Suppose du directory is run. It will traverse through every possible subdirectory of directory, recursively. A subdirectory in the directory hierarchy may be a mount point. For example, slash mnt slash sda1 is a subdirectory of mnt, and is also a mount point for the device slash dev slash sda1. Du will traverse that mount point and calculate the sum of disk usage for that device system, also.
Minus x is used to prevent du from doing this. For example, du minus x slash would exclude all mount points from slash mnt, for disk usage calculation. While using du, make sure that the directory is all files that traverses have proper read permissions. Finding the 10 largest sized files from a given directory. Finding large files is a task we come across regularly, so that we can delete or move them. We can easily find out such files using du in a sort commands, like this.
Here, minus a makes du traverse the SOURCE DIR, and calculates the size of all files and directories. The first column of the output contains the size in kilobytes, since minus k is specified, and the second column contains the file or folder name. Sort is used to perform a numerical sort with column one, and reverse it. Head is used to pass the first 10 lines from the output. For example, one of the drawbacks of the preceding one line is that is includes directories, in result.
However, when we need to find only the largest files, and not directories, we can improve the one line into output the large files only, as follows. We used find to filter only files to du, rather than allow du to traverse by itself. Disk free information. The du command provides information about the usage, whereas df provides information about free disk space. Use minus h with df to print the disk space in human, readable format.
For example, great. We've successfully learned how to monitor disk usage. In the next video, we'll see how to calculate the execution time for a command.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Printing in the terminal
- Performing math in the Linux shell
- Getting and setting dates
- Working with functions and arguments
- Reading output
- Making comparisons
- Concatenating text
- Finding, editing, generating, and deleting files
- Running parallel processes
- Using regular expressions
- Downloading webpages
- Parsing data from a website
- Finding broken links
- Backing up and archiving
- Transferring files and data through the network
- Monitoring your Linux system
- Gathering data for system administration
Skill Level Intermediate
Linux: Bash Shell and Scriptswith Kevin Dankwardt2h 46m Intermediate
1. Shell Something Out
2. Have a Good Command
3. File In, File Out
4. Texting and Driving
Using regular expressions9m 25s
5. Tangled Web? Not at All
6. The Backup Plan
7. The Old-Boy Network
8. Put on the Monitor's Cap
9. Administration Calls
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.