I'm in a directory that contains many folders like these:
At_5.2000_displacement
At_-6.4000_displacement
At_2nd_-4.3000_displacement
At_2nd_2.2000_displacement
I am writing a bash script that is cd
-ing to each of the folders of the type:
At_X.XXXX_displacement
i.e., I would like to
cd At_5.2000_displacement
and
cd At_-6.4000_displacement
folders. This leaves out the folders of the type:
At_2nd_X.XXXX_displacement
My attempts (Edited):
I found the reg-ex that yields the name of the target folders, which is the following:
At_-\?[0-9][.].*displacement
which yields:
At_5.2000_displacement
At_-6.4000_displacement
Now, in the bash
script, every time I do:
cd /path_to_the_folders/At_-\?[0-9][.].*displacement
There is no way of accessing each folder, since the error received is:
trial_cding.sh: line 11: cd: /path_to_the_folders/At_-?[0-9][.].*displacement: No such file or directory
How can I make the line cd /path_to_the_folders/At_-\?[0-9][.].*displacement
to work ?
The implementation of @dawg's answer via the if
statement (not using the shortcut), as far as I understood, would be something like:
for pn in *; do
if [[ ! -d "$pn" && $pn =~ At_[0-9.-]*_displacement ]]
then
echo No desired directory found
else
continue
cd $pn
pwd
cd -
fi
done
However, this returns no results. What am I getting wrong here?
Also, the I am not quite sure how to use parenthesis in cd $pn
in order to avoid going back
Regarding the [0-9.-]
match...
In the end we are looking for positive or negative decimal numbers.
In other words, we are looking for any number from (-)0 to (-)9, and followed by a .
This makes me think that [-0-9.]
is the most intuitive instruction.
Surprisingly, it happens that the following three also do match:
[0-9.-]
[0-9-.]
[-.0-9]
Please check https://regex101.com/r/G3fXo5/1, where I show the matches. This makes me think that the matching criteria inside [ ]
is quite broad.
So, I f I try [.-0-9]
I get no matching results. Why is this happening? What is the rule behind the matching inside [ ]
?