I retrieved all the pdfs in $HOME directory
$ find -E ~ -regex ".*/[^/].*.pdf"
It print more than 1000 files;
I intent to sort them by size and searched
$ stat -f '%z' draft.sh
184
I drafts the script:
#! /usr/local/bin/bash
OLD_IFS=IFS
IFS=$'\n'
touch sorted_pdf.md
for file in $(find -E ~ -regex ".*/[^/].*.pdf")
do
file_size=$(stat -c "%s" $file)
....
done > sorted_pdf.md
IFS=OLD_IFS
It's hard to work them together and get my result. Could you please provide any hint?
I refactored the code
#! /bin/zsh
OLD_IFS=IFS
IFS=$'\n'
touch sorted_pdf.md
for file in $(find -E ~ -regex ".*/[^/].*.pdf")
do
# file_size=$(stat -c "%s" $file)
printf '%s\n' $file(DoL)
done > sorted_pdf.md
IFS=OLD_IFS
but get error report
$ ./sort_files.sh
./sort_files.sh: line 12: syntax error near unexpected token `('
./sort_files.sh: line 12: ` printf '%s\n' $file(DoL)'
find
for-E
and BSDstat
for-f %z
, but then thestat -c %s
indicates GNUstat
, what system is it? Do you have to usebash
or can you use other shells likezsh
? – Stéphane Chazelas Oct 26 '18 at 14:48.pdf
. So it's different in that we need to sort files found byfind
(or other ways to find files by name). – Stéphane Chazelas Oct 26 '18 at 14:53find ... -printf '%s %P\n' | sort -n
work? – pLumo Oct 26 '18 at 15:07syntax error near unexpected token '('
is abash
message, but in any case,$file(DoL)
wouldn't make sense inzsh
either. That's meant to be a glob qualifier to sort the glob expansion, so doesn't make sense when applied to a single file. – Stéphane Chazelas Oct 26 '18 at 15:08find
’s output by size (using-printf
on GNUfind
). – Stephen Kitt Oct 26 '18 at 15:09printf
is GNU specific. The OP is on macOS. – Stéphane Chazelas Oct 26 '18 at 15:09-printf
preficate offind
is GNU-specific. Theprintf
utility itself is standard. – Stéphane Chazelas Oct 26 '18 at 18:04