Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 24 May 2013, 01:49
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Need to get pathname out of full file name
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [7 Posts]  
Author Message
RSH


Joined: 05 Sep 2011
Posts: 1564
Location: Germany

PostPosted: 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
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 1759

PostPosted: 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
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 1564
Location: Germany

PostPosted: 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! Smile

_________________
Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 1759

PostPosted: 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
View user's profile Send private message 
Moose On The Loose


Joined: 24 Feb 2011
Posts: 279

PostPosted: 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
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 1759

PostPosted: 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
View user's profile Send private message 
Bruce B


Joined: 18 May 2005
Posts: 10818
Location: The Peoples Republic of California

PostPosted: 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
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [7 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0602s ][ Queries: 12 (0.0162s) ][ GZIP on ]