ls first file of given type from given directory
Question
This searches only in the pwd
, despite having specified the directory. Probably caused by the third argument.
supported="*mov *mp4"
ls /home/kv/m $supported | head -1
..Removing the filter brings up the first file found by ls
, but what can I use to tell ls
to consider only the file types listed in $supported? --It's worth mentioning that the extensions mustn't be case-sensitive.
ls /home/kv/m | head -1
Solution
ls /home/kv/m | grep -i -E '\.(mov|mp4)$' | head -1
OTHER TIPS
Run it in a subshell, and cd
to the directory first
first=$( cd /home/kv/m && ls $supported | head -1 )
you might want to shopt -s nullglob
first, in case there are not .mov or .mp4 files.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow