SyncMaster
SyncMaster

Reputation: 9936

Bash script to find specific type of files with absolute path and sort them by size

In bash, how can I search for files of a specific type (say "*.txt") in a directory and its sub-directories. Then display the files in descending order of size along with its size and full path.

I tried the following but it doesn't work.

find . -type f -name "*.txt" -print0 | ls -sS

How can I do this?

Upvotes: 1

Views: 1573

Answers (3)

John3136
John3136

Reputation: 29266

find . -type f -name "*.txt" -print0 | xargs -0 ls -sS

Should work unless there are loads and loads of matching files (man xargs(1) to see what the defaults are)

Swiss' comment below if 100% correct, xargs -0 is the way to go since you are using find -print0

Upvotes: 2

jordanm
jordanm

Reputation: 34954

You can use GNU find's printf option to accomplish this:

find "$PWD" -type f -name '*.txt' -printf "%s %h/%f\n" | sort -rg

To show the size in KBs instead of bytes:

find "$PWD" -type f -name '*.txt' -printf "%k %h/%f\n" | sort -rg

Upvotes: 4

walrii
walrii

Reputation: 3522

find . -type f -name "*.txt" | xargs -i{} stat {} --format "%012s %n" | sort -r

Gives the size in bytes.

Upvotes: 1

Related Questions