Question
How would you go about telling whether files of a specific extension are present in a directory, with bash?
How-To
Consider below shell script:
- test.sh
- #!/bin/sh
- EXT=$1
- count=`ls -1 test/*.$EXT 2>/dev/null | wc -l`
- if [ $count -ge 1 ]; then
- echo "Folder test contains file with extension '$EXT'"
- else
- echo "Folder test contains no file with extension '$EXT'"
- fi
Below is the extended usage to show file with desired extension:
- test2.sh
- #!/bin/sh
- EXT=$1
- ARRAY=($(ls -1 test/*.$EXT))
- echo "List file with extension($EXT):"
- for f in "${ARRAY[@]}"
- do
- echo -e "\t$f"
- done
* How do I assign ls to an array in Linux Bash?
* nixCraft - Bash Iterate Array Examples
沒有留言:
張貼留言