Ahmad
Ahmad

Reputation: 9658

how do I access or sort the files of a directory in another order that the alphabetical order?

I run the following commands in linux on a pdf file to convert its pages to image files. However, it runs twice over the pdf file

pdftoppm  -H 700  -f 30 -l 40 -png rl.pdf top
pdftoppm -y 700  -f 30 -l 40 -png rl.pdf bottom

output would be (the list of output files):

bottom-001.png
bottom-002.png
top-001.png
top-002.png

However, I want to access and process them in the following order (for ffmpeg):

top-001.png
bottom-001.png
top-002.png
bottom-002.png

To reach this goal you may suggest another way for naming the output files or run another script on the output files to sort them out.

Upvotes: 1

Views: 295

Answers (3)

Ivan
Ivan

Reputation: 7277

Variants of sorting with ls command

$ ls --help
...
  -r, --reverse              reverse order while sorting
  -S                         sort by file size, largest first
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                               time (-t), version (-v), extension (-X)
  -t                         sort by modification time, newest first
  -u                         with -lt: sort by, and show, access time;
                               with -l: show access time and sort by name;
                               otherwise: sort by access time, newest first
  -U                         do not sort; list entries in directory order
  -v                         natural sort of (version) numbers within text
  -X                         sort alphabetically by entry extension

Upvotes: 0

Ahmad
Ahmad

Reputation: 9658

Another solution in this case is adding a suffix (in alphabetical order) to the output files of each command and move them to a new directory:

pdftoppm  -H 450  -f 30 -l 40 -png rl.pdf page
for file in *.png; do
     mv "$file" "out/${file%.png}_a.png"
done

pdftoppm  -y 700 -f 30 -l 40 -png rl.pdf page
for file in *.png; do
     mv "$file" "out/${file%.png}_b.png"
done

Upvotes: 0

KamilCuk
KamilCuk

Reputation: 141115

sort -n -t- -s -k2

Sort numerically using - as separator on the second field. Stable sort so that top is on top.

Alternatively sort the first field in reverse:

sort -t- -k2n -k1r

For example the following command:

echo 'bottom-001.png
bottom-002.png
top-001.png
top-002.png' | sort -t- -k2n -k1r

outputs:

top-001.png
bottom-001.png
top-002.png
bottom-002.png

Upvotes: 2

Related Questions