Linux – separate file and path in bash

bashlinuxpath

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

Best Answer

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

$ foo=/usr/local/bin/bar
$ echo "${foo##*/}"
bar
$ echo "${foo%/*}"
/usr/local/bin
Related Question