LinuxShell script strategy ---- Chapter 8 as a good manager

Source: Internet
Author: User

8.1 count disk usage

1. Disk Space is a limited resource.

2 df and du are important commands used in Linux to measure disk usage. df is short for disk free and du is short for disk usage.

3. Find the disk space occupied by a file (or multiple files): du file1 file2... // the statistical results are calculated in bytes by default.

4. You can use

Du-a DIRECTORY //-a Recursively outputs the statistical results of all files in a specified DIRECTORY or multiple directories

The 5 command du displays the total number of bytes occupied by the file by default, but the disk usage is displayed in the standard KB, MB, and GB. You can use the-h option.

Du-h file // command to display the KB occupied by the file

6 du option-c can output the total disk usage of all files and directories used as command parameters. It will add a line at the end of the output result.

Du-c file1 file2 // The command will summarize the disk usage of all files or directories in the last line

7. We can force du to use a specific unit to print disk usage.

Du-B file // print the file size in bytes

Du-k file // print the file size in KB

Du-m file // print the file size in MB

Du-B file // print the file size in BLOCK_SIZE

8 sometimes we need to exclude some files from the disk usage statistics. You can use either of the following methods:

(1) wildcard

Du -- exclude "word" DIRECTORY

Du -- exclude "*. txt"/path // command to exclude all. txt files

(2) Exclusion List

Du -- exclude-from EXCLUDE.txt DIRECTORY // EXCLUDE.txt contains the list of files to be excluded

9. We can use -- max-depth to specify the maximum depth of the directory hierarchy that du should traverse, and set the depth to 1 to count the memory usage of all files in the current directory, specify the depth to 2 to count the memory usage of the files in the current directory and the sub-directories below.

Du -- max-depth 2 DIRECTORY

10. Find the maximum 10 files in the specified directory: du-ak source_dir | sort-nrk 1 | head

-A specifies all directories and files, so du will traverse source_dir and calculate the size of all files. Because option-k is specified, the first column of the output will contain the file size in KB, and the second column will contain the name of the file or folder.

11 du provides disk usage information, while df provides disk available space information. This command can be used or used without the option-h. If-h is used, disk space information is printed in readable format.

8.2 calculate the command execution time

1. All UNIX-like operating systems contain the time command. You can put the time before the command to calculate the execution time.

Time command // command will execute and generate the output

The output displays the real time, user time, and system time used to execute the command.

Real time refers to the time when the alarm is triggered, that is, the time from the start to the end of the command.

User time refers to the cpu time spent by the process in user mode, which is the only time actually used to execute the process.

Sys time refers to the cpu time spent by the process in kernel mode.

8.3 information related to current login users, startup logs, and startup failures

1. to obtain information about the current logon user, you can use: who or: w.

This command provides the currently logged-on user, the user's Pseudo Terminal TTY, the Command currently executed by the Pseudo Terminal, and the user's logon IP address.

2 TTY is the device file associated with the text Terminal

3. to list the users of the currently logged on host, you can use users.

If you open multiple pseudo terminals

4. You can use uptime to view how long the system has been running.

5. To obtain the previous startup and user logon session information, you can use: last

To obtain the logon session information of a single user, you can use: last user

8.4 watch command output

1 watch command can be used to monitor command output at fixed intervals on the terminal. watch command

The command updates the output every 2 seconds by default.

2. We can use-n second to specify the time interval for updating the output.

Watch-n 5 command // The command will update the output once in 5 seconds

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.