![]() |
BASH - script to loop through files in size order descending
I can write a simple for loop to loop through files in name order.
Code:
for ff in *.mp4; do HandBrakeCLI -i "$ff" -o "X$ff";done But I want the list in size order descending so I did this Code:
for ff in "$( ls *.mp4 | sort -r )"; do echo "$ff"; done Code:
$ for ff in "$( ls *.mp4 | sort -r )"; do echo "$ff"; done; Code:
for ff in "$( ls *.mp4 | sort -r )"; do HandBrakeCLI -i "$ff" -o "X$ff"; done The result Code:
$ for ff in "$( ls *.mp4 | sort -r )"; do HandBrakeCLI -i "$ff" -o "X$ff"; done; Note: Some files have spaces in their names |
I've tried something else, I've made a file of the movie names in descending size order, simple enough
Code:
$ ls *.mp4 | sort -r > x.x Code:
$ while read ff; do echo $ff; done < x.x Code:
$ while read ff; do HandBrakeCLI -i "$ff" -o "X$ff" --stop-at seconds:20; done < x.x I thought I'd cracked it! |
My guess is that handbrake is sending signals which is killing the shell.
Try this: Code:
|
Quote:
I know I will kick myself when I work it out or someone else does |
Code:
for ff in "$( ls *.mp4 | sort -r )"; do Without the quotes the string is subject to word splitting (partly desired) and filename generation (not desired). The best solution is a while-read loop Code:
ls *.mp4 | sort -r | while IFS= read -r ff; do off some magic in read. Commands in the loop body that can read from stdin would compete with the read, and should be redirected </dev/null If HandBrakeCLI is such a command: Code:
</dev/null HandBrakeCLI ... ls *.mp4 | sort -r This is not by size but reverse alphabetical, like ls -r *.mp4 |
Quote:
Anyway I trid it like this Code:
ls *.mp4 | sort -r | while IFS= read -r ff; do HandBrakeCLI -i "$ff" -o "X$ff" --stop-at seconds:20 ; done |
Try the </dev/null
Code:
ls *.mp4 | sort -r | while IFS= read -r ff; do </dev/null HandBrakeCLI -i "$ff" -o "X$ff" --stop-at seconds:20 ; done |
Quote:
Next step is to feed it a list by size, I think this might do it Code:
ls *.mp4 -S And yes this worked just fine Code:
ls *.mp4 -S | sort -r | while IFS= read -r ff; do </dev/null HandBrakeCLI -i "$ff" -o "Y$ff" --stop-at seconds:20 ; done |
Lots of ways of doing that with bash and friends.
Few Examples: Sort files by size. Code:
find *.mp4 -type f -exec du -h {} + | sort -rh Code:
files1=(*.mp4) |
If ls can sort it, do not re-sort it by piping to sort!
Perhaps you want to reverse the order, then you must tell the ls to do it: Code:
ls *.mp4 -rS | while ... The loop's stdin is fed by the pipe. Not only the read can read a line from the stdin. Obviously the HandBrakeCLI reads from the stdin, too. And "steals" it, so the read doesn't get it. |
Quote:
|
Thanks all, I've learn't a bit more now, enough to be dangerous!!
|
All times are GMT -5. The time now is 10:41 AM. |