graceful degradation for globbing on terminal
-
22-09-2019 - |
Question
Whenever glob pattern match fails, it stops the whole job. For instance,
$ mv *.jpg *.png folder1 && blahblah
mv: cannot stat `*.jpg': No such file or directory
*.png isn't moved to folder1 and blahblah is not run.
And the script below works only for the case when both .[A-z]* and * succeed.
#!/bin/bash
cd $1
du -sk .[A-z]* *| sort -rn | head
How do I make globbing fail gracefully, at most only displaying warnings, but never stopping the job?
Solution
In Bash, shopt -s nullglob
will allow a failed glob to expand to nothing with no errors.
OTHER TIPS
then use a loop. KISS
for files in jpg png
do
mv *.${files} /destination 2>/dev/null && do_something
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow