-5

How can I find all the files in directory greater than a certain size, say, 15 KB, and which have been modified in the last 10 days?

  • 4
    Since this is homework it would be polite to show us what you've already attempted – roaima Jan 30 '16 at 14:34
  • I'll give you that the command you are looking for will probably start out as du -h, you can work yourself up from there (or use an otherwise specialized program to do it, a good file manager might even do the trick) – Cestarian Jan 30 '16 at 14:48
  • 2
    du would work, but find seems a better tool, @OP: This is not a script writing (or a one-liner writing) service. We are happy to help you if you get stuck after spending some effort. But do try to solve this on your own first and include what you already tried in the post. You can add that using the edit link. – Hennes Jan 30 '16 at 14:52
  • yeah, find would work as well. I mean I had no idea how to do this but was intrigued, I got as far as rounding files up larger than 15KB, but modified within last 10 days is a bit tougher. Edit: Nvm, I got it... it was actually quite easy with find. But what if the guy really wants to use nothing but bash scripting? – Cestarian Jan 30 '16 at 14:54
  • If you only want bash scripting it get much harder. Especially since you do nto want to parse the output of ls (which is not guaranteed to be stable and which date format might very much depend on your locale settings). – Hennes Jan 30 '16 at 15:17
4

I'd do it like this:

find /directory -mtime -10 -size +15k

The /directory is the base directory where the search is preformed (recursively by default).

-mtime 10 means it will look for files modified in the last 10 days:

  -mtime n
          File's data was last modified n*24 hours ago.  See the comments for -atime to understand how rounding affects the interpretation of file modi-
          fication times.

-size 15k means it will look for files larger than 15 kilobytes:

  -size n[cwbkMG]
          File uses n units of space, rounding up.  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.  The + and - prefixes  signify  greater
          than  and less than, as usual, but bear in mind that the size is
          rounded up to the next unit (so a 1-byte file is not matched  by
          -size -1M).

If this is a homework question of some kind, please read the find(1) manual for your operating system by typing man find on your system, and really learn how it's working.

1

Since I know it's difficult for a neophyte to find out which Linux command can be used to obtain a specific result, I'll point you to the correct direction.

The command you need to use is find. Its manual page, which you can read via

man find

will give you all the info you need. From this point you can work alone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.