Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash; change directory to folders start with 2* or 21*

I have a series of folders which are named based on the date of creation and some other information; i.e.

220310_AS
220307_DF
220228_1A
..
211228_QR
..
201224_HH

How can I in a bash script loop over folders which start with 22 or 21?
Here is part of my code which does not work

..
for dd in */2*; do
    cd "$dd"
    #do something
    eval "ls"
    cd ..
..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your wildcard */2* matches directories 2 levels below the original directory. So to get back to the original directory, you need cd ../.. instead of cd ...

You could also do the cd in a subshell, then simply exiting the shell will return you to the original directory.

for dd in */2*; do (
    cd "$dd"
    # do stuff
); done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading