Order all files by size using find
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
As practice I need to complete a script that orders all filles by size with a given extension(.txt for example) including those in subdirectories too.
For example;
./ex1.sh einstein txt
einstein/copyright.txt
einstein/do-how.txt
einstein/etext9/bil11.txt
einstein/etext9/2ws271.txt
einstein/etext9/liber11.txt
einstein/etext0/bib0010h/Readme.txt
einstein/etext0/kknta10.txt
I can't use du or other advanced commands. At some point I need to use find, I tried something like this
find -depth - type f -name "*.$extension" | sort ....
but this hasn't work really well, as i don't really know how to sort them by size, only result i get is sorted by name.
I was looking for an output similar to ls -lhS but including subdirectories.
bash shell-script find sort size
add a comment |Â
up vote
0
down vote
favorite
As practice I need to complete a script that orders all filles by size with a given extension(.txt for example) including those in subdirectories too.
For example;
./ex1.sh einstein txt
einstein/copyright.txt
einstein/do-how.txt
einstein/etext9/bil11.txt
einstein/etext9/2ws271.txt
einstein/etext9/liber11.txt
einstein/etext0/bib0010h/Readme.txt
einstein/etext0/kknta10.txt
I can't use du or other advanced commands. At some point I need to use find, I tried something like this
find -depth - type f -name "*.$extension" | sort ....
but this hasn't work really well, as i don't really know how to sort them by size, only result i get is sorted by name.
I was looking for an output similar to ls -lhS but including subdirectories.
bash shell-script find sort size
1
If you can only usefind
, why are you usingsort
? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it yourex1.sh
script? If so, what are the parameters (einstein
andtxt
) that you are giving?
â terdonâ¦
Mar 15 at 12:10
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
As practice I need to complete a script that orders all filles by size with a given extension(.txt for example) including those in subdirectories too.
For example;
./ex1.sh einstein txt
einstein/copyright.txt
einstein/do-how.txt
einstein/etext9/bil11.txt
einstein/etext9/2ws271.txt
einstein/etext9/liber11.txt
einstein/etext0/bib0010h/Readme.txt
einstein/etext0/kknta10.txt
I can't use du or other advanced commands. At some point I need to use find, I tried something like this
find -depth - type f -name "*.$extension" | sort ....
but this hasn't work really well, as i don't really know how to sort them by size, only result i get is sorted by name.
I was looking for an output similar to ls -lhS but including subdirectories.
bash shell-script find sort size
As practice I need to complete a script that orders all filles by size with a given extension(.txt for example) including those in subdirectories too.
For example;
./ex1.sh einstein txt
einstein/copyright.txt
einstein/do-how.txt
einstein/etext9/bil11.txt
einstein/etext9/2ws271.txt
einstein/etext9/liber11.txt
einstein/etext0/bib0010h/Readme.txt
einstein/etext0/kknta10.txt
I can't use du or other advanced commands. At some point I need to use find, I tried something like this
find -depth - type f -name "*.$extension" | sort ....
but this hasn't work really well, as i don't really know how to sort them by size, only result i get is sorted by name.
I was looking for an output similar to ls -lhS but including subdirectories.
bash shell-script find sort size
edited Mar 15 at 12:14
asked Mar 15 at 12:02
Arnau MartÃnez
142
142
1
If you can only usefind
, why are you usingsort
? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it yourex1.sh
script? If so, what are the parameters (einstein
andtxt
) that you are giving?
â terdonâ¦
Mar 15 at 12:10
add a comment |Â
1
If you can only usefind
, why are you usingsort
? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it yourex1.sh
script? If so, what are the parameters (einstein
andtxt
) that you are giving?
â terdonâ¦
Mar 15 at 12:10
1
1
If you can only use
find
, why are you using sort
? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it your ex1.sh
script? If so, what are the parameters (einstein
and txt
) that you are giving?â terdonâ¦
Mar 15 at 12:10
If you can only use
find
, why are you using sort
? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it your ex1.sh
script? If so, what are the parameters (einstein
and txt
) that you are giving?â terdonâ¦
Mar 15 at 12:10
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
0
down vote
accepted
find
includes an option -printf
which allows you to format what to output about your results, and how to output them. One of possibilities listed in the documentation for that option (see the man
page) is %s
for a file's size in bytes. Thus, you can add to your find command something like `-printf "%s %pn".
@terdon - yes. His answer had not been posted when I began writing my answer.
â user1404316
Mar 15 at 12:35
Ah, the classic race condition! :)
â terdonâ¦
Mar 15 at 12:51
add a comment |Â
up vote
2
down vote
The only sorting that any of the find
s can do AFAIK is to have contents appear before the containing directory (the -depth
option). You will have use something else to sort on size.
If you have GNU find, try:
find . -type f -iname "*.$extension" -printf "%s %pn" | sort -n | sed 's/^[0-9]* //'
-printf "%s %pn"
prints the size and file path, then we sort numerically, and then strip the size using sed
.
1
(assuming file paths don't contain newlines and bytes not forming valid characters and that$extension
doesn't contain wildcards)
â Stéphane Chazelas
Mar 15 at 12:15
1
(in which casewith
-printf
and the-z
options as needed, I think)
â muru
Mar 15 at 12:16
yep, NUL terminated output is the solution - e.g.find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2'
(non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
â cas
Mar 15 at 12:48
add a comment |Â
up vote
1
down vote
With zsh
:
printf '%sn' **/*.$extension(D.oL)
To get a GNU ls -lh
-type output, with GNU ls
:
ls -Ulhd -- **/*.$extension(D.oL)
Or if the list is too large:
autoload zargs # best in ~/.zshrc
zargs --eof= -- **/*.$extension(D.oL) '' ls -Ulhd --
Or
printf '%s' **/*.$extension(D.oL) | xargs -r0 ls -Ulhd --
If for some reason, you do need to use find
, you can always do:
printf '%s' ./**/*.$extension(D.oL) | xargs -r0 sh -c '
exec find "$@" -prune ...' sh
The OP needs to usefind
for some reason.
â terdonâ¦
Mar 15 at 12:17
add a comment |Â
up vote
0
down vote
i use
find . -type f -iname "*.$extension" -print0 | xargs -0 ls -lS
this works fine for me
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
find
includes an option -printf
which allows you to format what to output about your results, and how to output them. One of possibilities listed in the documentation for that option (see the man
page) is %s
for a file's size in bytes. Thus, you can add to your find command something like `-printf "%s %pn".
@terdon - yes. His answer had not been posted when I began writing my answer.
â user1404316
Mar 15 at 12:35
Ah, the classic race condition! :)
â terdonâ¦
Mar 15 at 12:51
add a comment |Â
up vote
0
down vote
accepted
find
includes an option -printf
which allows you to format what to output about your results, and how to output them. One of possibilities listed in the documentation for that option (see the man
page) is %s
for a file's size in bytes. Thus, you can add to your find command something like `-printf "%s %pn".
@terdon - yes. His answer had not been posted when I began writing my answer.
â user1404316
Mar 15 at 12:35
Ah, the classic race condition! :)
â terdonâ¦
Mar 15 at 12:51
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
find
includes an option -printf
which allows you to format what to output about your results, and how to output them. One of possibilities listed in the documentation for that option (see the man
page) is %s
for a file's size in bytes. Thus, you can add to your find command something like `-printf "%s %pn".
find
includes an option -printf
which allows you to format what to output about your results, and how to output them. One of possibilities listed in the documentation for that option (see the man
page) is %s
for a file's size in bytes. Thus, you can add to your find command something like `-printf "%s %pn".
answered Mar 15 at 12:15
user1404316
2,314520
2,314520
@terdon - yes. His answer had not been posted when I began writing my answer.
â user1404316
Mar 15 at 12:35
Ah, the classic race condition! :)
â terdonâ¦
Mar 15 at 12:51
add a comment |Â
@terdon - yes. His answer had not been posted when I began writing my answer.
â user1404316
Mar 15 at 12:35
Ah, the classic race condition! :)
â terdonâ¦
Mar 15 at 12:51
@terdon - yes. His answer had not been posted when I began writing my answer.
â user1404316
Mar 15 at 12:35
@terdon - yes. His answer had not been posted when I began writing my answer.
â user1404316
Mar 15 at 12:35
Ah, the classic race condition! :)
â terdonâ¦
Mar 15 at 12:51
Ah, the classic race condition! :)
â terdonâ¦
Mar 15 at 12:51
add a comment |Â
up vote
2
down vote
The only sorting that any of the find
s can do AFAIK is to have contents appear before the containing directory (the -depth
option). You will have use something else to sort on size.
If you have GNU find, try:
find . -type f -iname "*.$extension" -printf "%s %pn" | sort -n | sed 's/^[0-9]* //'
-printf "%s %pn"
prints the size and file path, then we sort numerically, and then strip the size using sed
.
1
(assuming file paths don't contain newlines and bytes not forming valid characters and that$extension
doesn't contain wildcards)
â Stéphane Chazelas
Mar 15 at 12:15
1
(in which casewith
-printf
and the-z
options as needed, I think)
â muru
Mar 15 at 12:16
yep, NUL terminated output is the solution - e.g.find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2'
(non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
â cas
Mar 15 at 12:48
add a comment |Â
up vote
2
down vote
The only sorting that any of the find
s can do AFAIK is to have contents appear before the containing directory (the -depth
option). You will have use something else to sort on size.
If you have GNU find, try:
find . -type f -iname "*.$extension" -printf "%s %pn" | sort -n | sed 's/^[0-9]* //'
-printf "%s %pn"
prints the size and file path, then we sort numerically, and then strip the size using sed
.
1
(assuming file paths don't contain newlines and bytes not forming valid characters and that$extension
doesn't contain wildcards)
â Stéphane Chazelas
Mar 15 at 12:15
1
(in which casewith
-printf
and the-z
options as needed, I think)
â muru
Mar 15 at 12:16
yep, NUL terminated output is the solution - e.g.find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2'
(non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
â cas
Mar 15 at 12:48
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The only sorting that any of the find
s can do AFAIK is to have contents appear before the containing directory (the -depth
option). You will have use something else to sort on size.
If you have GNU find, try:
find . -type f -iname "*.$extension" -printf "%s %pn" | sort -n | sed 's/^[0-9]* //'
-printf "%s %pn"
prints the size and file path, then we sort numerically, and then strip the size using sed
.
The only sorting that any of the find
s can do AFAIK is to have contents appear before the containing directory (the -depth
option). You will have use something else to sort on size.
If you have GNU find, try:
find . -type f -iname "*.$extension" -printf "%s %pn" | sort -n | sed 's/^[0-9]* //'
-printf "%s %pn"
prints the size and file path, then we sort numerically, and then strip the size using sed
.
answered Mar 15 at 12:11
muru
33.4k577141
33.4k577141
1
(assuming file paths don't contain newlines and bytes not forming valid characters and that$extension
doesn't contain wildcards)
â Stéphane Chazelas
Mar 15 at 12:15
1
(in which casewith
-printf
and the-z
options as needed, I think)
â muru
Mar 15 at 12:16
yep, NUL terminated output is the solution - e.g.find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2'
(non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
â cas
Mar 15 at 12:48
add a comment |Â
1
(assuming file paths don't contain newlines and bytes not forming valid characters and that$extension
doesn't contain wildcards)
â Stéphane Chazelas
Mar 15 at 12:15
1
(in which casewith
-printf
and the-z
options as needed, I think)
â muru
Mar 15 at 12:16
yep, NUL terminated output is the solution - e.g.find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2'
(non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
â cas
Mar 15 at 12:48
1
1
(assuming file paths don't contain newlines and bytes not forming valid characters and that
$extension
doesn't contain wildcards)â Stéphane Chazelas
Mar 15 at 12:15
(assuming file paths don't contain newlines and bytes not forming valid characters and that
$extension
doesn't contain wildcards)â Stéphane Chazelas
Mar 15 at 12:15
1
1
(in which case
with -printf
and the -z
options as needed, I think)â muru
Mar 15 at 12:16
(in which case
with -printf
and the -z
options as needed, I think)â muru
Mar 15 at 12:16
yep, NUL terminated output is the solution - e.g.
find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2'
(non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.â cas
Mar 15 at 12:48
yep, NUL terminated output is the solution - e.g.
find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2'
(non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.â cas
Mar 15 at 12:48
add a comment |Â
up vote
1
down vote
With zsh
:
printf '%sn' **/*.$extension(D.oL)
To get a GNU ls -lh
-type output, with GNU ls
:
ls -Ulhd -- **/*.$extension(D.oL)
Or if the list is too large:
autoload zargs # best in ~/.zshrc
zargs --eof= -- **/*.$extension(D.oL) '' ls -Ulhd --
Or
printf '%s' **/*.$extension(D.oL) | xargs -r0 ls -Ulhd --
If for some reason, you do need to use find
, you can always do:
printf '%s' ./**/*.$extension(D.oL) | xargs -r0 sh -c '
exec find "$@" -prune ...' sh
The OP needs to usefind
for some reason.
â terdonâ¦
Mar 15 at 12:17
add a comment |Â
up vote
1
down vote
With zsh
:
printf '%sn' **/*.$extension(D.oL)
To get a GNU ls -lh
-type output, with GNU ls
:
ls -Ulhd -- **/*.$extension(D.oL)
Or if the list is too large:
autoload zargs # best in ~/.zshrc
zargs --eof= -- **/*.$extension(D.oL) '' ls -Ulhd --
Or
printf '%s' **/*.$extension(D.oL) | xargs -r0 ls -Ulhd --
If for some reason, you do need to use find
, you can always do:
printf '%s' ./**/*.$extension(D.oL) | xargs -r0 sh -c '
exec find "$@" -prune ...' sh
The OP needs to usefind
for some reason.
â terdonâ¦
Mar 15 at 12:17
add a comment |Â
up vote
1
down vote
up vote
1
down vote
With zsh
:
printf '%sn' **/*.$extension(D.oL)
To get a GNU ls -lh
-type output, with GNU ls
:
ls -Ulhd -- **/*.$extension(D.oL)
Or if the list is too large:
autoload zargs # best in ~/.zshrc
zargs --eof= -- **/*.$extension(D.oL) '' ls -Ulhd --
Or
printf '%s' **/*.$extension(D.oL) | xargs -r0 ls -Ulhd --
If for some reason, you do need to use find
, you can always do:
printf '%s' ./**/*.$extension(D.oL) | xargs -r0 sh -c '
exec find "$@" -prune ...' sh
With zsh
:
printf '%sn' **/*.$extension(D.oL)
To get a GNU ls -lh
-type output, with GNU ls
:
ls -Ulhd -- **/*.$extension(D.oL)
Or if the list is too large:
autoload zargs # best in ~/.zshrc
zargs --eof= -- **/*.$extension(D.oL) '' ls -Ulhd --
Or
printf '%s' **/*.$extension(D.oL) | xargs -r0 ls -Ulhd --
If for some reason, you do need to use find
, you can always do:
printf '%s' ./**/*.$extension(D.oL) | xargs -r0 sh -c '
exec find "$@" -prune ...' sh
edited Mar 15 at 12:20
answered Mar 15 at 12:13
Stéphane Chazelas
280k53515847
280k53515847
The OP needs to usefind
for some reason.
â terdonâ¦
Mar 15 at 12:17
add a comment |Â
The OP needs to usefind
for some reason.
â terdonâ¦
Mar 15 at 12:17
The OP needs to use
find
for some reason.â terdonâ¦
Mar 15 at 12:17
The OP needs to use
find
for some reason.â terdonâ¦
Mar 15 at 12:17
add a comment |Â
up vote
0
down vote
i use
find . -type f -iname "*.$extension" -print0 | xargs -0 ls -lS
this works fine for me
add a comment |Â
up vote
0
down vote
i use
find . -type f -iname "*.$extension" -print0 | xargs -0 ls -lS
this works fine for me
add a comment |Â
up vote
0
down vote
up vote
0
down vote
i use
find . -type f -iname "*.$extension" -print0 | xargs -0 ls -lS
this works fine for me
i use
find . -type f -iname "*.$extension" -print0 | xargs -0 ls -lS
this works fine for me
answered Mar 15 at 12:59
Ayyanar
715
715
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430381%2forder-all-files-by-size-using-find%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
If you can only use
find
, why are you usingsort
? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it yourex1.sh
script? If so, what are the parameters (einstein
andtxt
) that you are giving?â terdonâ¦
Mar 15 at 12:10