Hi,
I am using the bash shell on suse.I need to split the absolute file path
into filename and directory path.
eg: /cas/43fg/temp/sample.txt
i want to split the above into
sample.txt\(file\) and /cas/43fg/temp\(directory path\)
I want to do this by using shell commands.Would anyone help ?
aigles
January 9, 2007, 9:52am
2
A solution :
full_path=/cas/43fg/temp/sample.txt
file=$(basename $full_path)
dir=$(dirname $full_path)
Jean-Pierre.
anbu23
January 10, 2007, 1:35am
3
full_path=/cas/43fg/temp/sample.txt
pth=$( echo ${full_path%/*} )
file=$( echo ${full_path##/*/} )
or
full_path=/cas/43fg/temp/sample.txt
pth=$( echo $full_path | sed "s,/[^/]*$,," )
file=$( echo $full_path | sed "s,/.*/,," )
All solutions are working fine,Thank you all for your valuable comment and time.
anbu,
would you pls explain the first solution of yours.It's working fine,but i am curious to know the chemistry.So pls.
anbu23
January 10, 2007, 4:13am
5
pth=$( echo ${full_path%/*} )
Remove the shortest match from / to end of the input string
file=$( echo ${full_path##/*/} )
Remove the longest match /*/ from the start of the string
vino
January 10, 2007, 4:45am
6
Why the $(..) and the echo ?
This will work as well.
pth=${full_path%/*}
file=${full_path##/*/}