LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to extract path name starting with specific folder (https://www.linuxquestions.org/questions/linux-general-1/how-to-extract-path-name-starting-with-specific-folder-846158/)

john206 11-23-2010 09:02 PM

How to extract path name starting with specific folder
 
Hello guys,

Im writing a script that extracts part of the path name:



I drop a file in terminal and set the directory name of the file to x using dirname $0
note: ( directory is different every time since not all files are from the same location)



ex.
x="/home/Downloads/yesterday/photo/.../image.png" /image.png

/.../ means I dont know the name of folders between but I know the path name always starts with photo

and I want to capture directory after "photo" and store it like this:

y=/photo/image.png"
then something like
code:
cp file $y



All helps appreciated

catkin 11-23-2010 09:56 PM

Assuming for example you want to extract x/y/z/image.png from /home/Downloads/yesterday/photo/x/y/z/image.png (which is not what you illustrated but may have been what you meant):
Code:

x=/home/Downloads/yesterday/photo/x/y/z/image.png
echo ${x##*/photo/}


john206 11-23-2010 10:13 PM

Quote:

Originally Posted by catkin (Post 4168953)
Assuming for example you want to extract x/y/z/image.png from /home/Downloads/yesterday/photo/x/y/z/image.png (which is not what you illustrated but may have been what you meant):
Code:

x=/home/Downloads/yesterday/photo/x/y/z/image.png
echo ${x##*/photo/}



Thank you very much, it worked like a charm :D

catkin 11-23-2010 10:39 PM

Quote:

Originally Posted by john206 (Post 4168964)
Thank you very much, it worked like a charm :D

That's good. It is bash parameter expansion. Threads can be marked SOLVED via the Thread Tools menu.


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