Use the Find command to Find the files to be cleared.

Source: Internet
Author: User

Use the Find command to Find the files to be cleared.

Credit: Sandra H-S

One problem is that almost all file systems-including Unix and others-are constantly accumulating files. Almost no one is willing to spend time cleaning out the files they no longer use and organizing the file system. As a result, the files become messy and it is difficult to find useful things, it will be a persistent challenge to make them run well, maintain backups, and be easy to manage.

One solution I have seen is to recommend that you create a summary report or "Overview" for all the data fragments to report the number of files, such as the number of all files. The oldest, the latest and largest file, and statistics on who owns the file. If someone saw a folder containing 0.5 million files five years ago, they could delete which files -- or, at least, archive and compress them. The main problem is that too many folders may cause people to worry about accidental deletion of important things. If there is a folder description method that can help display the nature of the file, you can clear it.

When I prepare a summary report for a Unix file system, several useful Unix Commands provide very useful statistics. To calculate the number of files in the directory, you can use the find command.

  1. $ find .-type f | wc -l
  2. 187534

Although searching for the oldest and newest files is complicated, it is quite convenient. In the following command, we use the find command to find the files again, which are displayed in chronological order and in the format of year-month-day. The files on the top of the list are obviously the oldest.

In the second command, we do the same, but the last line is printed, which is the latest.

  1. $ find -type f -printf '%T+ %p\n'| sort | head -n 1
  2. 2006-02-03+02:40:33./skel/.xemacs/init.el
  3. $ find -type f -printf '%T+ %p\n'| sort | tail -n 1
  4. 2015-07-19+14:20:16./.bash_history

The printf command outputs the % T (File date and time) and % P (file name with Path) parameters.

If we find that the history file (for example,. bash_history) is up-to-date, it is useless. You can ignore these files through "un-grepping" or ignore files starting with., as shown in.

  1. $ find -type f -printf '%T+ %p\n'| grep -v "\./\."| sort | tail -n 1
  2. 2015-07-19+13:02:12./isPrime

Find the maximum file using the % s (size) parameter, including the file name (% f), because this is what we want to display in the report.

  1. $ find -type f -printf '%s %f \n'| sort -n | uniq | tail -1
  2. 20183040 project.org.tar

Statistics file owner, using % u (owner)

  1. $ find -type f -printf '%u \n'| grep -v "\./\."| sort | uniq -c
  2. 180034 shs
  3. 7500 jdoe

If the file system can record the last access date, it will also be very useful. It can be used to check whether the file has been accessed. For example, it has not been accessed within two years. This will allow you to clearly distinguish the value of these files. The last access (% a) parameter is used as follows:

  1. $ find -type f -printf '%a+ %p\n'| sort | head -n 1
  2. FriDec1503:00:302006+./statreport

Of course, if most recently accessed files were stored long ago, it seems that you need to process more files.

  1. $ find -type f -printf '%a+ %p\n'| sort | tail -n 1
  2. WedNov2603:00:272007+./my-notes

To have a clear hierarchy, you can create a summary report for a file system or a large directory to display the date range of these files, the largest files, file owners, the oldest files, and the latest access time, it helps the file owner determine which folders are important and which are cleaned up.

Linux find command usage Summary

Find a file in Linux

Detailed description of the find command in Linux

Use of find, a text search tool

Powerful find command

Detailed description of the find command in Linux

Via: Handy commands for profiling your Unix file systems

Author: Sandra Henry-Stocker Translator: strugglingyouth Proofreader: wxy

This article was originally translated by LCTT and launched with the Linux honor in China

This article permanently updates the link address:

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.