LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-12-2011, 01:11 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Rep: Reputation: 2
get full filepath from filename


Hi,
I want to get a full filepath from filename:
Code:
filename="/folder1/f2/f3/file.xxx"
farray=(echo $filename | cut -d\/ -f-1)
echo ${farray[0]} #desired outcome here is "/folder1/f2/f3/"
echo ${farray[1]} #desired outcome here is "file.xxx"
Should cut work arlight? what did i do wrong?
Thanks,
Ted
 
Old 11-12-2011, 01:16 AM   #2
bplis*
Member
 
Registered: Nov 2011
Distribution: Fedora
Posts: 65

Rep: Reputation: Disabled
Why would you do this if you already know the file path? I don't know your purpose in this. It won't find the file path from just the name.extension if thats what you want....
 
0 members found this post helpful.
Old 11-12-2011, 01:20 AM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by bplis* View Post
Why would you do this if you already know the file path? I don't know your purpose in this. It won't find the file path from just the name.extension if thats what you want....
Because I am building a page from bash that tries to browse the directory of which I saved the filename temporarily.
 
Old 11-12-2011, 02:06 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Hi,

how about 'dirname' and 'basename'?
Code:
$ filename="/folder1/f2/f3/file.xxx"
$ dirname "$filename"
/folder1/f2/f3
$ basename "$filename"
file.xxx
[EDIT]
Alternatively, if the path always ends with a filename:
Code:
echo ${filename##*/} # filename
echo ${filename%${filename##*/}} # path
Or if it is always a full path, i.e. there will never be just a filename, then you could also:
Code:
echo ${filename##*/} # filename
echo ${filename%/*}} # path

Last edited by crts; 11-12-2011 at 02:14 AM.
 
1 members found this post helpful.
Old 11-13-2011, 06:29 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
This is wrong.
Code:
farray=(echo $filename | cut -d\/ -f-1)
The array setting pattern is arrayname=(). The brackets should contain a list of words to set the elements to. As it is, your list of words is: "echo", "$filename", "|", etc., with the "|" causing a syntax error.

What you need is to use a command substitution to generate a list of words from the command first. The syntax for that is $(..).

Also, the command you'd usually want to use to break up a string is tr. The following converts the filename four separate "words", which will then be placed into individual array indexes:

Code:
filename="/folder1/f2/f3/file.xxx"
farray=( $( echo "$filename" | tr '/' ' ' ) )
There are other, and better, ways to break up a string into an array too. See here:

http://mywiki.wooledge.org/BashGuide/Arrays

http://www.linuxquestions.org/questi...3/#post4521744

The second link is to another recent post by me on the same topic.

Note that since the array stores a list of the separate elements without the delimiters, if you wanted to print out the "pathname" you'd have to use something like this:

Code:
echo "/${farray[0]}/${farray[1]}/${farray[2]}/"
But all this is an overly-complex way to get what you want. What you really need is to use either the dirname/basename commands, or parameter substitution, as crts has already demonstrated.

By the way, cut can do what you want, within limits. But you have to be clear on how it works. If you have a look at the man page you'll see that "-M" is the same as "1-M", so in the above command you were asking it to print only the first field.

There's yet another issue however that's less obvious. When you set a delimiter to something other than the default, and the delimiting character happens to fall at the start of the string, then the actual first field is null space in front of it. So:

Code:
filename="/folder1/f2/f3/file.xxx"
echo "$filename" | cut -d'/' -f 1
...means it prints what comes before the first "/", which is "nothing".

The same thing happens at the other end of the string too, by the way. If the final character is the delimiter, then the last field will also be an empty field.

Finally, cut is limited in that you can only tell it to print a fixed number of strings, and you can only specify one set of fields in a single command. So you can use, for example...
Code:
echo "$filename" | cut -d'/' -f 1-4
echo "$filename" | cut -d '/' -f 5
...to get what you want. But what happens if the input filepath has 5 levels? Or 3?

Last edited by David the H.; 11-13-2011 at 06:34 AM. Reason: short addendum
 
1 members found this post helpful.
Old 11-13-2011, 10:33 AM   #6
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Original Poster
Rep: Reputation: 2
thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ls - Do not list full path only filename baddah Linux - General 12 06-17-2013 01:21 AM
[SOLVED] bash script - return full path and filename Andy Alt Programming 44 01-10-2013 10:11 AM
curl certificates being refused, possible filepath issue JDska55 Linux - Newbie 3 07-13-2009 08:18 PM
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 07:07 AM
how to get absolute filepath from filedescriptor appas Programming 1 07-27-2004 11:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration