Computing.Net > Forums > Unix > Create a file with a path?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Create a file with a path?

Reply to Message Icon

Name: 427v8
Date: April 7, 2003 at 07:44:19 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 7, 2003 at 10:24:29 Pacific
Reply:

Hi:

I think you want to use the dirname command:

filepath=/home/eds/tdir/somefile.txt

dirpath=$(dirname $filepath)
echo $dirpath

Regards,


Nails


0

Response Number 2
Name: mibwalker
Date: April 7, 2003 at 10:44:10 Pacific
Reply:

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/


0

Response Number 3
Name: 427v8
Date: April 7, 2003 at 11:22:31 Pacific
Reply:

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


0

Response Number 4
Name: mibwalker
Date: April 7, 2003 at 13:31:59 Pacific
Reply:

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.


0

Response Number 5
Name: WilliamRobertson
Date: April 7, 2003 at 16:49:14 Pacific
Reply:

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%/*}


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Create a file with a path?

Count # of files with a string www.computing.net/answers/unix/count-of-files-with-a-string/7511.html

copy files with pattern from a file www.computing.net/answers/unix/copy-files-with-pattern-from-a-file/6254.html

A=`echo | tr '\012' '\001' ` www.computing.net/answers/unix/aecho-tr-012-001-/7647.html