LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2009, 12:09 AM   #1
ojha_riddhish
LQ Newbie
 
Registered: Mar 2009
Location: Vapi, India
Distribution: RHEL 5, openSUSE 11.1, Ubuntu 8.10 / 9.04, Fedora 10
Posts: 19

Rep: Reputation: 0
Question 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.

Thanks in advance!
 
Old 04-11-2009, 12:13 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have GNU find, you can use printf with format specifiers. check the man page for more info.
 
Old 04-11-2009, 05:27 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
example needed.

don't understand.
 
Old 04-13-2009, 12:32 AM   #4
ojha_riddhish
LQ Newbie
 
Registered: Mar 2009
Location: Vapi, India
Distribution: RHEL 5, openSUSE 11.1, Ubuntu 8.10 / 9.04, Fedora 10
Posts: 19

Original Poster
Rep: Reputation: 0
Question

Dear friends,
I have the script which returns values like: (example)

/var/spool/mail/everything/
/usr/local/php/scripts/
/etc/httpd/conf/

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 in advance!
 
Old 04-13-2009, 01:08 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
mkdir -p /var/spool/mail/everything/
 
Old 04-13-2009, 01:32 AM   #6
ojha_riddhish
LQ Newbie
 
Registered: Mar 2009
Location: Vapi, India
Distribution: RHEL 5, openSUSE 11.1, Ubuntu 8.10 / 9.04, Fedora 10
Posts: 19

Original Poster
Rep: Reputation: 0
Question

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.

Thanks!
 
Old 04-13-2009, 01:50 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
i still don't understand what you are wanting to do.
 
Old 04-13-2009, 03:15 AM   #8
ojha_riddhish
LQ Newbie
 
Registered: Mar 2009
Location: Vapi, India
Distribution: RHEL 5, openSUSE 11.1, Ubuntu 8.10 / 9.04, Fedora 10
Posts: 19

Original Poster
Rep: Reputation: 0
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.

Thanks in advance!
 
Old 04-13-2009, 03:48 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if its PHP, you may want to look here
 
Old 04-13-2009, 04:18 AM   #10
ojha_riddhish
LQ Newbie
 
Registered: Mar 2009
Location: Vapi, India
Distribution: RHEL 5, openSUSE 11.1, Ubuntu 8.10 / 9.04, Fedora 10
Posts: 19

Original Poster
Rep: Reputation: 0
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.

Thanks in advance!
 
Old 04-13-2009, 05:08 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
# string="/var/spool/mail/everything/"
# echo ${string/\//}
var spool mail everything
# string=${string/\//}
# set -- $string
# echo $1
var
# echo $2
spool
# echo $3
mail
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
List file names in a directory WITHOUT the path dexus Linux - Newbie 16 11-09-2016 07:58 PM
Remove whitespace in file and directory names using bash TheFutonEng Programming 14 02-26-2012 12:35 PM
NFS mounting conventions and path names spankbot Linux - Server 1 03-31-2009 09:26 PM
Find a file path and directory path ak.lokesh Linux - Newbie 3 02-19-2009 12:37 PM
setting bash to look in current directory before searching the path muhkuhmasta Linux - Newbie 4 09-21-2004 02:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration