LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help with bash script (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-bash-script-714738/)

d0minos 03-26-2009 10:25 PM

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
Code:

#!/bin/bash
if [ -z "$1" ] ; then echo "Nothing entered"
exit
fi
if [ -d "$1" ]; then echo "Directory exists"
else echo "No such directory"
exit
fi

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.

Simon Bridge 03-27-2009 01:12 AM

Hints:

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.

vjayraghavan 03-27-2009 01:57 AM

hi,

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

openSauce 03-27-2009 06:07 AM

Works for me:
Code:

~$ ./test.sh xpmnt
Directory exists
xpmnt/b
xpmnt/c
xpmnt/d
~$ cat test.sh
#!/bin/bash
if [ -z "$1" ] ; then echo "Nothing entered"
exit
fi
if [ -d "$1" ]; then echo "Directory exists"
else echo "No such directory"
exit
fi

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.

Quigi 03-27-2009 09:18 PM

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 :study: man find -- "To ignore a directory and the files under it, use -prune".

vjayraghavan 03-30-2009 12:20 AM

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


All times are GMT -5. The time now is 02:43 AM.