LinuxQuestions.org
Visit Jeremy's Blog.
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 08-07-2007, 11:57 PM   #1
firefoxlinux
LQ Newbie
 
Registered: Aug 2007
Posts: 6
Blog Entries: 1

Rep: Reputation: 0
Shell Script to Get Folder Names


Hi,

I want to write a shell script to get all the folder names in to a file for a given path. asume therer is folder called test and it has sub folders called "A", "B", "C" and "D" when i run the script a file shud contain this folder name as given below
FOLDER A
FOLDER B
FOLDER C
FOLDER D
 
Old 08-08-2007, 12:29 AM   #2
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
You can do it with one command.

Code:
find -type d -printf 'FOLDER %f\n'
or

Code:
find NAME_OF_DIRECTORY_TO_START_IN -type d -printf 'FOLDER %f\n'
 
Old 08-08-2007, 12:33 AM   #3
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,310

Rep: Reputation: 139Reputation: 139
You can also do it with this:

ls <path to directory>/ | grep /
 
Old 08-09-2007, 01:52 AM   #4
firefoxlinux
LQ Newbie
 
Registered: Aug 2007
Posts: 6
Blog Entries: 1

Original Poster
Rep: Reputation: 0
hi kilgoretrout

thnx for the 2 commands it works fine but can i get the full path of the folder for an example the command u gave gives the out put as
FOLDER A
FOLDER B
FOLDER C

assume if the folder C is inside the B still it give the answer as above, if i want to get the output as given below what shud i do.

FOLDER A
FOLDER B
FOLDER B/C
 
Old 08-09-2007, 02:23 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
The find cmd gives you that layout eg
find . -iname '*' -print 2>/dev/null
 
Old 09-27-2007, 11:27 AM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Or even better:
Code:
DIR=<name_of_target_directory>
find $DIR -iname '*' -printf "FOLDER %P\n"
I didn't find a need for "2>/dev/null", but add it back if you find you need it.

(RTM find)
 
Old 09-27-2007, 02:56 PM   #7
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Quote:
Originally Posted by firefoxlinux View Post
hi kilgoretrout
[...]
assume if the folder C is inside the B still it give the answer as above, if i want to get the output as given below what shud i do.

FOLDER A
FOLDER B
FOLDER B/C
With zsh:

Code:
zsh 4.3.4% mkdir -p A B B/C            
zsh 4.3.4% printf "FOLDER %s\n" **/*(/)
FOLDER A
FOLDER B
FOLDER B/C
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
BASH Shell script : copying a file to multiple folder zamri Programming 14 04-29-2008 10:27 AM
Shell script deletes folder before due OR13 Linux - General 3 08-04-2006 08:10 PM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 03:40 AM
long folder names opsraja Fedora 2 09-23-2004 01:56 PM
Putting Script Names In Shell Scripts sayble96 Linux - Newbie 4 12-17-2003 10:16 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration