LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   get full filepath from filename (https://www.linuxquestions.org/questions/linux-newbie-8/get-full-filepath-from-filename-913113/)

ted_chou12 11-12-2011 12:11 AM

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

bplis* 11-12-2011 12:16 AM

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....

ted_chou12 11-12-2011 12:20 AM

Quote:

Originally Posted by bplis* (Post 4521986)
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.

crts 11-12-2011 01:06 AM

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


David the H. 11-13-2011 05:29 AM

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?

ted_chou12 11-13-2011 09:33 AM

thanks


All times are GMT -5. The time now is 09:44 AM.