Skip to content
Advertisement

cd command : how to go back an unknown number of levels from current subdirectory to a particular parent directory (unix and dos)

Ok, so I am trying to resolve a uri in an xmlcatalog and I want to go back from a particular sub-directory back to a parent-directory that is an-unknown-number-of-levels behind. eg:

file:///D:/Sahil/WorkSpaces1/Cartridges1/Project1/ParticularFolder/Level1/Level2/<so-many-levels>/CurrentFolder

I want to go back from “CurrentFolder” to “ParticularFolder” without typing in the full FilePath.

I want to achieve this because, I work in multiple Projects which all have “ParticularFolder” in it, so the codes inside the sub-directories of this folder should dynamically have access to all other files in other sub-directories inside this parent folder. I do not want to specify separate full filepaths for my various projects and make the code too rigid.

Is it possible? Please mention how to achieve this in windows, unix as well as linux os.

Advertisement

Answer

In UNIX/Linux/OS X/etc.:

while [ "$(basename $PWD)" != "ParticularFolder" ]; do cd ..; done
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement