11

I am trying to get the files older than a number of days and list them in descending order based on their size with all their information (size, full path etc - something similar that is provided by ls).

While I am able to locate files older with:

find . -mtime +10

I am not able to list of the desired information.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • please add your distro and shell informations – lese Dec 21 '15 at 15:11
  • @lese : AIX 6.1 ; while echo $SHELL --> /usr/bin/ksh – user148335 Dec 21 '15 at 15:23
  • This sounds like a XYProblem ... do you really, as you ask, need ALL the files by size order? or are you (more likely) looking for "old" (>10 days) files big enough and just need, say, the 200 biggest to then decide which one of those you can delete to save some space?. In the latter case, and since you are on AIX with maybe no gnu tools, a combination of find (outputting all files >10 days) and awk (keeping top 200 biggest files in an array, then outputing it in order) will be your best bet – Olivier Dulac Nov 10 '16 at 12:43

7 Answers7

10

Provided that your file paths do not contain newline characters:

find . -mtime +10 -printf "%s %n %m %u %g %t %p" \( \
  -type l -printf ' -> %l\n' -o -printf '\n' \) | sort -k1,1 -n

See find manual, section Actions.

  • %s File's size in bytes.
  • %n Number of hard links to file.
  • %m File's permission bits (in octal).
  • %u File's user name, or numeric user ID if the user has no name.
  • %g File's group name, or numeric group ID if the group has no name.
  • %t File's last modification time in the format returned by the C ctime function.
  • %p File's name.
  • %l Object of symbolic link (empty string if file is not a symbolic link).

BTW: Note that POSIX find manual doesn't specify most of the above actions.

patryk.beza
  • 1,179
4

This command uses only POSIX features of find and of ls:

find . -type f -mtime +10 -exec ls -lS {} +

However, it may call ls more than once, if there are a very large number of files in the current directory (or subdirectories recursively) matching the -mtime +10 primary.

If it calls ls more than once, of course, the sorting will only be done within each ls execution, not across multiple executions.

Still, for reasonable numbers of files, this is likely your best bet.

Wildcard
  • 36,499
  • The updated version doesn't break with spaces in the name – Miguel G Dec 21 '15 at 14:52
  • 1
    If ls is being passed only one argument, there's little sorting it can do on it. With + instead of ;, that would be better, but ls could still end up being called several times if there are many files. – Stéphane Chazelas Dec 21 '15 at 15:13
  • 2
    This is the exact command I would have given, so I added the explanatory text I would have given to go with it. – Wildcard Nov 10 '16 at 11:39
3

With zsh:

ls -ldrS -- **/*(m+10)

Or to avoid the double-sorting (by name by zsh and by size by ls):

ls -ldrS -- **/*(m+10oN) # disable zsh sorting

or (if your ls support -U for unsorted):

ls -ldU -- **/*(m+10oL) # disable ls sorting and have zsh sort by size

Add the D glob qualifier is you want to see hidden files as well.

1

I think you could do something similar to :

find . -type f -mtime +10 -print0 | sort -n -r -k1 | while IFS= read -r -d $'\0' line; do
    #echo "$line"
    ls -lS "$line"    
done

notice the -S parameter of ls command :

-S sort by file size

lese
  • 2,726
1

try this:

find ./ -type f -mtime +10 -exec ls -ln {} \; | sort -r -k 5 -g
dervishe
  • 465
  • With the -n option, it's not a trouble because ls don't list username and groupname but uid and gid instead – dervishe Dec 21 '15 at 14:51
0

For an easier one-liner you could use the find option -ls. It appears in most cases the file size is at the seventh column of the listed information (so change 7 at the end of the command as appropriate if not):

find . -type f -mtime +10 -ls | sort -n -r -k7
woodengod
  • 493
0

Another variation would be to use:

  • find (to find the matching files)
  • du (to show the filesize in KB)
  • sort -n -r (to short in descending order)

Here's the command:

find . -type f -mtime +10 -exec du -k {} \; | sort -n -r