Create a file with a path?
|
Original Message
|
Name: 427v8
Date: April 7, 2003 at 07:44:19 Pacific
Subject: Create a file with a path? OS: AIX CPU/Ram: 686 256
|
Comment: In a ksh script I get a file with a path from another program. I need to create the file and it's directories. I know mkdir -p can make the dirs but how to get the dirs without the filename? TIA Keith
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: nails
Date: April 7, 2003 at 10:24:29 Pacific
Subject: Create a file with a path? |
Reply: (edit)Hi: I think you want to use the dirname command: filepath=/home/eds/tdir/somefile.txt dirpath=$(dirname $filepath) echo $dirpath Regards, Nails
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: mibwalker
Date: April 7, 2003 at 10:44:10 Pacific
Subject: Create a file with a path?
|
Reply: (edit)Or you could use cut. get your file/path into some variable dir=/dir/dir1/dir2/file.txt echo $dir |cut -f1-4 -d"/" This will give you /dir/dir1/dir2/
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: 427v8
Date: April 7, 2003 at 11:22:31 Pacific
Subject: Create a file with a path?
|
Reply: (edit)Thanks nails! dirname, just what I was looking for. mibwalker, how do you handle variable length paths? Like /dir1/file.txt vs /dir1/dir2/.../dir99/file.txt
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: mibwalker
Date: April 7, 2003 at 13:31:59 Pacific
Subject: Create a file with a path?
|
Reply: (edit)The cut command does that with the -f (field). The -d is delimiter. I cut that text string into feilds using / as the delimiter. If you had a path like /dir1/dir2/dir3/file.txt cut -f2 -d"/" would give you dir1 cut -f1-2 -d"/" would give you /dir1/ This one is a little tricky because of the / at the very beginning the 1-2 means feild 1 through 2 the first slahs counts as a feild seperator. Just play around with it a little, I use cut for all kinds of things.
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: WilliamRobertson
Date: April 7, 2003 at 16:49:14 Pacific
Subject: Create a file with a path? |
Reply: (edit)An alternative to dirname is ${var%/*} e.g. if var=/some/filename/with/long/path/x.dat then print ${var%/*} gives /some/filename/with/long/path You could script this with filepath=${var%/*}
Report Offensive Follow Up For Removal
|
Use following form to reply to current message: