Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I separate the path and file elements in a bash loop like this?

for file in `find /my/path -name "*.ext"`
do
    #(path,onlyfile) = separate_path_and_file $file
    #dosomethingwith $onlyfile
done
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You can't. But you can do them separately.

$ foo=/usr/local/bin/bar
$ echo "${foo##*/}"
bar
$ echo "${foo%/*}"
/usr/local/bin
share|improve this answer
    
Beautiful, thanks very much. –  130490868091234 Jul 2 '12 at 8:57
add comment

I would suggest dirname and basename:

for file in `find /my/path -name "*.ext"`
do
    path="$(dirname $file)"
    onlyfile="$(basename $file)"
    # ...
done
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.