Chapter 5 study notes of Linux command line and shell script programming

Source: Internet
Author: User
Chapter 2: compile a script utility to monitor the function du required by the disk space, display the disk usage of files and directories, and view the size of a directory. we can: $ du -- max-depth = 0-handroid-sdk61Gandroid-sdk $ du-shandroid-sdk

Chapter 2: script utilities

 Disk space monitoring

Required functions

Du: displays the disk usage of files and directories.

To view the size of a directory, we can:

$ du --max-depth=0 -h android-sdk6.1Gandroid-sdk$ du -sh android-sdk6.1Gandroid-sdk$ du --max-depth=0 -sh android-sdkdu: warning: summarizing is the same as using --max-depth=06.1Gandroid-sdk

We can see that -- max-depth = 0 is the same as-s.

You can run the following command to view the size of the directory/file under the folder

$ du --max-depth=1 -h android-sdk1.5Gandroid-sdk/platforms1.5Gandroid-sdk/system-images4.0Kandroid-sdk/temp526Mandroid-sdk/docs228Mandroid-sdk/sources2.2Gandroid-sdk/add-ons27Mandroid-sdk/platform-tools96Mandroid-sdk/tools239Mandroid-sdk/samples12Mandroid-sdk/extras6.1Gandroid-sdk$ du -sh android-sdk/*4.0Kandroid-sdk/SDK Readme.txt2.2Gandroid-sdk/add-ons526Mandroid-sdk/docs12Mandroid-sdk/extras27Mandroid-sdk/platform-tools1.5Gandroid-sdk/platforms239Mandroid-sdk/samples228Mandroid-sdk/sources1.5Gandroid-sdk/system-images4.0Kandroid-sdk/temp96Mandroid-sdk/tools 

-- Max-depth = 1 not only counts the first-level directory under the folder, but also the size of the root directory, but does not count the file information under the folder. -S calculates the file information in the folder, but does not calculate the size of the folder itself. To calculate the sum, you only need to use-c (count ).Du-shc android-sdk /*

Most of the time, we only care about the guys who occupy a lot of space, so we only need to sort them by sort. for example, we want to find the top three:

$ du -s android-sdk/* | sort -rn | sed -n '1,3p'2217224android-sdk/add-ons1515352android-sdk/system-images1484724android-sdk/platforms$ du -s android-sdk/* | sort -rn | head -n32217224android-sdk/add-ons1515352android-sdk/system-images1484724android-sdk/platforms

In sort,-n can be sorted by numbers to identify negative numbers, and-r is sorted from large to small.

Create script

Summarize the above content and find the three most occupied space

#!/bin/bashF_DIR="$1"du -s "$F_DIR"* 2>/dev/null | sort -rn | head -n3 | sed '=' | sed 'N; s/\n/: /'

Execute scripts

$ du_test.sh ../../android-sdk/1: 2217224../../android-sdk/add-ons2: 1515352../../android-sdk/system-images3: 1484724../../android-sdk/platforms

Back up

Archive data files

Use tar. if you want to compress the file, use the-z option.

We can add a time for this.

$ date +%Y%m%d%H%M%S20131210132813 

Manage user accounts

Required functions

Delete an account. At least four steps are required to delete an account:

1. get the correct account name for the user account to be deleted

2. force terminate processes running on the system that belong to this account

3. confirm all files belonging to this account on the system

4. delete a user account

First, check whether the account entered by the user exists:

$ cut -d ':' -f 1 /etc/passwd | grep -w "$user_account"

To view the processes that belong to this user, you can use ps:

ps -u $user_account

Then they are killed:

kill -9 $user_pid

Search for the files belonging to this account:

find / -user $user_account

Note: The following are some options:

1. delete these questions (this is nothing to say)

2. associate these files with other users (this is nothing to say)

3. do not process

Assume that the UID of the account to be deleted is 1234. if we do not process the files belonging to this account, when we delete this account and create a new account, if the UID of the new account is 1234, the new account will automatically own these files!

Finally, delete the account:

userdel $user_account

(Note only contains the necessary scripts and there is no new knowledge. for all the scripts, see chapter 26th of the original book)

Repost the following link

My blog address

Http://su1216.iteye.com/

Http://blog.csdn.net/su1216/

Related Article

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.