Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

Script or method to get "du . -s " with outputs for sizes above certain size & later than certain date


When I'm doing housekeeping, issuing
 " du . -s "   or " du . " often give volumes
of outputs which I have to manually sieve
through.

Any Shell script/method for me to sieve out only
 those outputs of a certain size (which I can
 specify, say 200 kblocks) & later than certain
 date (say last 1 week) ?  Don't want any installed
 software/rpm.

I'm running on HP-Ux  B11.11 & RHES 4.x/5.x

0
sunhux
Asked:
sunhux
  • 7
  • 3
  • 2
4 Solutions
 
PapertripCommented:
Find all files over 200k blocks:
find . -size +200000b

Open in new window

Find all files modified more than 7 days ago
find . -mtime +7

Open in new window

If you want to delete files that match either of those finds, do this:
find . -size +200000b -print0 |xargs -0 rm -f

Open in new window

or
find . -size +200000b -exec rm -f {} +

Open in new window


The difference between the 2 boils down to, for the most part, how to handle "weird" characters in filenames (spaces fall into that category too.)  This also depends on the versions of find and xargs, I suggest testing both safely.

Try each of those commands but with 'ls -l' instead of 'rm -f' to do your testing to see which, if not both, commands work for the files you are dealing with.
0
 
PapertripCommented:
Actually after looking at my syntax again, I see something that might hold you up --

add '-type f' into the find syntax to find files only, so that you don't accidentally delete directories that match your criteria which might have files underneath it that do not match it.  Aside from that, you would get an error from 'rm -f' for those matches anyways so the rm shouldn't rm those directories, but better to be safe than sorry.

0
 
PapertripCommented:
Ah yet again, some info that could prove useful to you.

You probably aren't going to be searching for files by blocks used (are you?).  Here are the other suffixes that can be used with '-size'... keep in mind the note at the end regarding if you are really going to search by block size.

 -size n[cwbkMG]
              File uses n units of space.  The following suffixes can be used:

              `b'    for 512-byte blocks (this is the default if no suffix is used)

              `c'    for bytes

              `w'    for two-byte words

              `k'    for Kilobytes (units of 1024 bytes)

              `M'    for Megabytes (units of 1048576 bytes)

              `G'    for Gigabytes (units of 1073741824 bytes)

              The  size  does  not count indirect blocks, but it does count blocks in sparse files that are not actually allocated.
              Bear in mind that the `%k' and `%b' format specifiers of -printf handle sparse files  differently.   The  `b'  suffix
              always denotes 512-byte blocks and never 1 Kilobyte blocks, which is different to the behaviour of -ls.

Open in new window

Now in regards to the time-based need you asked about, there are other options for that as well... atime, ctime, mtime.  Check out the 'find' man page for other modifiers, but mtime which stands for 'modified time' is probably the most common out of the bunch.

Long story short, now that you have an idea of how to do what you want, I recommend using the 'find' man page to fine tune it to your needs :)



0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sunhuxAuthor Commented:

Can you combine both the criteria (& one more criteria) together in one single command :

list out files ( newer NOT older  than 7 days) of more than 100kBytes & text files only
(don't want binaries as they should be quite static)
0
 
sunhuxAuthor Commented:


Would something like the following work?  Correct my syntax if it's wrong :

find . -mtime -7  -size +100000b -name *.log -exec rm -f {} +

0
 
PapertripCommented:
Absolutely!

Test all the combos you want, just substitute something like 'ls -l' for 'rm -rf'.
0
 
PapertripCommented:
Oh wait I misread your syntax... maybe that was a typo on your side?

find . -mtime -7

Open in new window

should be
find . -mtime +7

Open in new window

0
 
TintinCommented:
You can't easily detect binaries, but -type f will match files only.  So your syntax should be:

find . -type f -mtime -7  -size +100000b -name "*.log" | xargs rm

Open in new window


Note it's more efficient to use xargs
0
 
PapertripCommented:
You can't easily detect binaries, but -type f will match files only
Apologies, I overlooked the comment that was posted about the binaries.
Note it's more efficient to use xargs
That is true if you are using an old or non-GNU version of find.  If you have a current GNU find, then the following operate the same way:
xargs rm

Open in new window

-exec rm {} +

Open in new window




0
 
PapertripCommented:
@Tintin
find . -type f -mtime -7  -size +100000b -name "*.log" | xargs rm

Open in new window

That syntax is fine (except for the mtime typo) if your files do not contain any characters that should be escaped.

The syntax I posted however will:
find . -size +200000b -print0 |xargs -0 rm -f

Open in new window


That syntax can of course be used in conjunction with the other arguments posted in this thread such as -type  and -name.
0
 
TintinCommented:
>mtime typo

What typo?


Note that HP/UX does not have GNU find.
0
 
sunhuxAuthor Commented:
ok
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now