Getting directory names from a given path in Bash?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Getting directory names from a given path in Bash?
Dear friends,
I am programming in bash and really stuck finding directory names.
I have a script to find all the .php files on my / partition which will return the whole path. Is there a way to print directory hierarchy with all those values leaving out the forward slashes.
These are addresses of *.php files on my root(/) partition. I just wanted to know whether I can separate all the directory names removing the '/' slashes and create a dirctory hierarchy such as:
var
|_spool
|_mail
|_everything
I can do echo $(folder%/*) and it's combinations to separate the folders but the problem is that I have to provide the number of slashes that occur in the path which I don't know at runtime. Please help if anyone knows about this tiny problem.
Thanks, once again ghostdog74! for your fast response. But you might see that I don't want to create directories in the path. The directories are created and I want to display those directories containing php files after formatting them accordingly.
Dear Friends,
I am sorry that I'm not making myself clear. I'll try once more.
I am making a php editor that crawls your hard disk for .php files to be used with the editor. After searching, it will display the files in tree-view as in Dolphin-Nautilus-Explorer that you might have experienced. The list of files are stored in a temp file that I want to read line-by-line and separate out the directory names. Then another module will generate the tree-view based on the folders and files returned. Hope, now it is clear for all of you to understand.
Dear friends,
Thanks for the advise. I am using php with some bash scripts. I can get my work done in php using explode() function but I want to use in bash script. That's the question. And by the way I don't have problem in generating the tree, it's the value containing my path.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.