Computing.Net > Forums > Unix > parsing full path and filename

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.

parsing full path and filename

Reply to Message Icon

Name: nzslkr
Date: March 18, 2008 at 10:24:06 Pacific
OS: Unix
CPU/Ram: Unknown
Product: AIX
Comment:

I have a script that is receiving the full path and filename as 1 parm. Example: dir1/dir2/test.txt.
I need to split the path and filename into 2 variables.
dir = dir1/dir2
file = test.txt

Eric



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: March 18, 2008 at 11:17:08 Pacific
Reply:

One way uses unix commands and one way uses bash/ksh pattern matching operators:


#!/bin/ksh

var=dir1/dir2/test.txt

# use the unix basename command
file=$(basename $var)
echo $file

# use the unix dirname command
dir=$(dirname $var)
echo $dir

# use ksh/bash patterns


# Using ksh pattern-matching operators, this function
# replaces the unix basename command. If a 2nd argument
# exists, strip off the extension.
function my_basename {
typeset v x

v=${1##*/}
x=${2#.} # get rid of the '.'
v=${v%.$x}
echo $v
}


# Using ksh pattern-matching operators, this function
# replaces the unix dirname command.
function my_dirname {
typeset v="$@"

echo ${v%/*}
}


ffile=$(my_basename $var)
echo $ffile


ddir=$(my_dirname $var)
echo $ddir



0

Response Number 2
Name: nzslkr
Date: March 18, 2008 at 11:35:50 Pacific
Reply:

Thanks Nails...

I needed to parse the parm inorder to cd to the directory and then ftp the file.

That worked.

Eric

Eric


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


vi is wrapping at 80th co... simple srcipt



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: parsing full path and filename

how to parse a path in unix www.computing.net/answers/unix/how-to-parse-a-path-in-unix/3301.html

how can I get the full path of a running program ? www.computing.net/answers/unix/how-can-i-get-the-full-path-of-a-running-program-/1357.html

awk variable for filename www.computing.net/answers/unix/awk-variable-for-filename/7078.html