0

I have a requirement where I have to read the names of the sub directories and copy the names to a file. Following is the code that I have written.

 for file in $HOME/AutoQA/screenshots/*
 do
 if [ -d "$file" ];
     then
        echo "$file" >> $HOME/AutoQA/FailedTestCases.txt
  fi
done 

Now the above code works almost perfectly but the output that I get is something like

/home/AutoQA/screenshots/1
/home/AutoQA/screenshots/2
......

But I expect it to be as follows

1
2
....

So I need only the names of the sub directories. Here before running the for loop, I dont want to do

cd /home/AutoQA/screenshots/

How can I manage that? Can anyone help?

1
  • If you add a trailing / to your glob, it will expand to directory names only, removing the need for the if statement. Commented Oct 7, 2013 at 13:45

1 Answer 1

2

Replace the line

echo "$file" >> $HOME/AutoQA/FailedTestCases.txt

with

basename "$file" >> $HOME/AutoQA/FailedTestCases.txt
Sign up to request clarification or add additional context in comments.

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.