I have a list of files inside a directory and I want to make a bash script which uses a regular expression to check if every file name inside it has this syntax:
xxxx_xxxx_xx_xx
Where x are numbers.
Edit: I only need with the regex
How about
#/usr/bin/env bash
for f in *
do
[[ $f =~ [0-9]{4}_[0-9]{4}_[0-9]{2}_[0-9]{2} ]] ||
echo "File $f does not match"
done
The regular expression checks for any digit ([0-9]
). The numbers in curly braces are the number of repetitions so [0-9]{4}
will match any 4 digits.
I would recommend you don't use bash for this but find instead. It will probably be faster, and it is certainly more portable (not all shells can deal with regular expressions):
find -regextype posix-egrep -not -regex '\./[0-9]{4}_[0-9]{4}_[0-9]{2}_[0-9]{2}'
find
command to break, but I still believe (on general principles) that the regex should probably be quoted, for safety.
Jul 11, 2014 at 14:50
5{4}_3{4}_0{2}_9{2}
(and you don’t have shell globbing turned off), then \./[0-9]{4}_[0-9]{4}_[0-9]{2}_[0-9]{2}
(your regex) will expand to ./5{4}_3{4}_0{2}_9{2}
, and your find
will match only ./5555_3333_00_99
(if it exists). … … (BTW, 5309 is the last four digits of Jenny’s phone number.)
Jul 11, 2014 at 15:50
It’s clunky, but this should be very portable:
#!/bin/sh for x in * do case "$x" in [0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]) echo "$x matches" ;; *) echo "$x doesn’t match" esac done # And, to be annoyingly complete, for x in .* do echo "$x doesn’t match" done