Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-26-2009, 11:25 PM   #1
LQ Newbie
Registered: Mar 2009
Posts: 1

Rep: Reputation: 0
help with bash script

I need to write a script that would take in a path to the folder and list all the subdirectories of that folder.
However, it should not list folders named private.
So far I have come up with this code
if [ -z "$1" ] ; then echo "Nothing entered" 
if [ -d "$1" ]; then echo "Directory exists"
else echo "No such directory"

find $1/* -type d ! -name private
The problem is that it does not find the subdirectories for the folder I entered, instead it finds all the subfolders in the system. Is there any way to fix this?

The another issue is that at the moment it works only if i enter the full path to the folder. I would prefer it it worked with relative paths also.

I would appreciate your help.
Old 03-27-2009, 02:12 AM   #2
Simon Bridge
LQ Guru
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198

pwd gets you the path to the current directory - relative paths do not have the leading /

A subdirectory and a sub-folder are the same thing. folder == directory.
Old 03-27-2009, 02:57 AM   #3
LQ Newbie
Registered: Feb 2009
Posts: 15
Blog Entries: 1

Rep: Reputation: 0

If u just want to list the directories under one folder just try the below command... It ahould help u..

find ./* -type d ! -name private | awk -F'/' '{ print $2 }' | uniq -c | grep -v private
Old 03-27-2009, 07:07 AM   #4
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Works for me:
~$ ./ xpmnt
Directory exists
~$ cat
if [ -z "$1" ] ; then echo "Nothing entered" 
if [ -d "$1" ]; then echo "Directory exists"
else echo "No such directory"

find $1/* -type d ! -name private~$
What was the exact command you used that didn't work?

Edit: Maybe I misunderstood what you were asking. Do you only want the immediate subdirectories of the one you're looking at? If so use -maxdepth 1 as an option to find.

Last edited by openSauce; 03-27-2009 at 07:09 AM.
Old 03-27-2009, 10:18 PM   #5
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
The "find" commands shown above don't print a directory called "private", but descend into it. I think that's why vjayraghavan post-processes the output through "grep -v private".

But man find -- "To ignore a directory and the files under it, use -prune".
Old 03-30-2009, 01:20 AM   #6
LQ Newbie
Registered: Feb 2009
Posts: 15
Blog Entries: 1

Rep: Reputation: 0
exactly quigi...
The find command will just not find private directory but it prints all the files and directories under it...


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 11:53 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 08:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 07:51 AM > Forums > Linux Forums > Linux - Newbie

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

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