7
1
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
7
1
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
3This is much more readable than the accepted answer. – itsafire – 2015-09-02T09:55:58.837
and in case there are spaces in the file, i would add quotation around the $file. https://stackoverflow.com/questions/7194192/basename-with-spaces-in-a-bash-script
– Hermann Ingjaldsson – 2018-07-19T10:12:56.8171In
zsh
of Windows Subsystem for Linux,path="$(dirname $file)"
will change$PATH
, thenonlyfile="$(basename $file)"
will print errorbasename: command not found
– Weekend – 2018-08-22T10:12:40.403