when I change any configuration I made a copy of the original file with suffix .original. Now I am composing a simple script that will find all *.original files and work with both versions, ie with and without .original suffix.
I do use command locate
with regular expression to avoid mismatched files with the original
string in the middle of the path.
My command is locate --regex ^.*\\.original$
that works perfectly in terminal
. It finds all files that ends with the .original suffix such as file.original
However when I use the same command in bash script
in the for loop
it returns me variants such as file.original
file-original
file_original
etc.
How should I modify the regex or for loop to get only .original files?
My shell is:
$ echo $BASH
/usr/bin/bash
My bash script is:
#!/usr/bin/bash
echo "Plain locate command:"
locate --regex ^.*\\.original$ | grep test
echo "For loop:"
for file in locate --regex ^.*\\.original$ | grep test
; do
echo $file
done
You can use this for testing:
mkdir /tmp/test
touch /tmp/test/file.original
touch /tmp/test/file-original
touch /tmp/test/file_original
updatedb
locate --regex ^.*\\.original$
In my terminal it will find:
/tmp/test/file.original
But my script will find:
Plain locate command:
/tmp/test/file.original
For loop:
/tmp/test/file-original
/tmp/test/file.original
/tmp/test/file_original