| Author |
Message |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: Tue 03 Apr 2012, 02:55 Post subject:
Need to get pathname out of full file name |
|
Hi,
i need to get the path of a filename.
Example:
/mnt/sdd1/test.sfs
- basename gives me the file name (test.sfs) without the path (/mnt/sdd1/)
How can i get only the path out of /mnt/sdd1/test.sfs?
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1759
|
Posted: Tue 03 Apr 2012, 03:53 Post subject:
|
|
'dirname'
You can also do both of those with bash. YMMMV with dash/ash/busbox-ash, etc.
FULL_PATH=/mnt/sdd1/test.sfs
BASE_NAME=${FULL_PATH##*/}
DIR_NAME=${FULL_PATH%/*}
|
|
Back to top
|
|
 |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: Tue 03 Apr 2012, 04:38 Post subject:
|
|
| amigo wrote: | 'dirname'
You can also do both of those with bash. YMMMV with dash/ash/busbox-ash, etc.
FULL_PATH=/mnt/sdd1/test.sfs
BASE_NAME=${FULL_PATH##*/}
DIR_NAME=${FULL_PATH%/*} |
Thanks!
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1759
|
Posted: Tue 03 Apr 2012, 12:48 Post subject:
|
|
If you find the bash syntax:
BASE_NAME=${FULL_PATH##*/}
DIR_NAME=${FULL_PATH%/*}
hard to read, you can create two small functions at the top of your script:
| Code: | _basename() {
echo ${1##*/}
}
_dirname () {
echo ${1%/*}
} |
Then, you can use them just like you would basename and dirname, except use _basename and _dirname instead.
Have fun!
|
|
Back to top
|
|
 |
Moose On The Loose

Joined: 24 Feb 2011 Posts: 279
|
Posted: Wed 04 Apr 2012, 10:48 Post subject:
|
|
| amigo wrote: | If you find the bash syntax:
BASE_NAME=${FULL_PATH##*/}
DIR_NAME=${FULL_PATH%/*}
hard to read, you can create two small functions at the top of your script:
| Code: | _basename() {
echo ${1##*/}
}
_dirname () {
echo ${1%/*}
} |
Then, you can use them just like you would basename and dirname, except use _basename and _dirname instead.
Have fun! |
echo "${1##*/}"
The quotes make the result be treated as just one word always.
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1759
|
Posted: Wed 04 Apr 2012, 13:23 Post subject:
|
|
Ridht you are -I simply quit having names with spaces a long time ago....
|
|
Back to top
|
|
 |
Bruce B

Joined: 18 May 2005 Posts: 10818 Location: The Peoples Republic of California
|
Posted: Tue 10 Apr 2012, 16:28 Post subject:
|
|
Different approaches
| Code: | #!/bin/bash
[ ! $1 ] && echo "Enter filename for argument" && exit
[ ! -f $1 ] && echo "$1 doesn't exist" && exit
# note: usage of realpath, dirname, and basename
full_path=`realpath $1`
file_path=`dirname $full_path`
file_name=`basename $full_path`
echo Full path: $full_path
echo File path: $file_path
echo File name: $file_name
|
_________________ New! Puppy Linux Links Page
|
|
Back to top
|
|
 |
|