get full filepath from filename
Hi,
I want to get a full filepath from filename: Code:
filename="/folder1/f2/f3/file.xxx" Thanks, Ted |
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....
|
Quote:
|
Hi,
how about 'dirname' and 'basename'? Code:
$ filename="/folder1/f2/f3/file.xxx" Alternatively, if the path always ends with a filename: Code:
echo ${filename##*/} # filename Code:
echo ${filename##*/} # filename |
This is wrong.
Code:
farray=(echo $filename | cut -d\/ -f-1) 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" 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]}/" 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" 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 |
thanks
|
All times are GMT -5. The time now is 09:44 AM. |