0

I have a number of directories I want to enter and all start with the same string "example". Beyond, in the same location, there are also other directories that I do NOT want to enter. I was used to loop in this way:

for dir1 in */

before other directories with other names were present in the same location, but now I cannot anymore. I tried to vary the command using:

for dir1 in "example"*/

or for dir1 in example*/ cd "dir1"

but it does not enter the directory. It says: line 8: cd: example*//: file or directory does not exist. How can I do that?

5
  • Did you try for dir1 in example*/ without the quotes? Commented Feb 28, 2014 at 3:40
  • yep, it does not work as well. Commented Feb 28, 2014 at 3:42
  • Specifically what are you trying to match? If I have a folder with directories that start with "c", I can do for x in c*/; do echo $x; done and it gives me the directory names that begin with "c". Could you please clarify what your statement, "does not work" means? Commented Feb 28, 2014 at 3:49
  • I edited my original text to clarify the question. Thanks. Commented Feb 28, 2014 at 3:55
  • "line 8"? Could you show the whole part of the script starting at the for through to and including the cd command that failed? Commented Feb 28, 2014 at 4:02

3 Answers 3

1

The shell expands the wildcard

for dir1 in */

into a list of wildcard matches;

for dir1 in ack/ bar/ foo/ nst/ pth/ quux/

If you want to exclude foo and bar from this list, the simplest to explain is to enable extended globbing, assuming you have Bash;

shopt -s extglob
for dir1 in !(foo|bar)*/

but any trick to exclude the files you don't want from matching the wildcard is okay. In this particular case, you could do

for dir1 in [!bf]*/

but in the pessimal case, you just have to break down and list the directories you do want to match. Or maybe just bypass the undesired ones separately:

for dir1 in */; do
    case $dir1 in bar|foo) continue ;; esac
    : ... your code here
done

If indeed you want to loop over all the directories whose names start with example and no others, then the way to do that is certainly

for dir1 in example*/

The error message you got seemed to indicate that there are no directories with this name.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this bash one liner,

for dir in example*; do if [ -d $dir ];then cd $dir; pwd; echo $dir; fi; done

Bash script,

#!/bin/bash

for dir in example*
do 
   if [ -d $dir ];then 
        cd $dir; 
        pwd; 
        echo $dir; 
    fi
done

Comments

0

Bash file name globbing is a slick thing. It may be more convenient to use find instead. Like this:

    for dir in `find /cygdrive/c/workspaces/ -name '*log*' -a -type d`; do echo $dir; done

'-type d' is for directories only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.