How to list Matching Directories OR NULL w/o error message?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to list Matching Directories OR NULL w/o error message?
# 1  
Old 09-26-2011
How to list Matching Directories OR NULL w/o error message?

I want to be able to get all the directories in a path into a variable array, BUT if there ARE NO directories I want the Variable to be NULL and not echo any error message!

If there ARE directories, this will get the list of the directories whose name begins with the string "20":
Code:
DATE_DIRS=$(ls 20* -d)



And "ls 20* -d" entered at a command prompt will list the directories on the console.

But if there ARE NO directories then script or the command spits out the annoying error
"ls: cannot access 20*: No such file or directory"

How do I suppress the warning message?
# 2  
Old 09-26-2011
Could try this - but be carefull that no files start with 20, you may want to check with [ -d $dir ] to trap any non-directories.

Code:
DATE_DIRS=$(ls -d 20* 2> /dev/null)

or

Code:
DATE_DIRS=$(echo 20*)
[ "$DATE_DIRS" = '20*' ] && DATE_DIRS=''


Last edited by Chubler_XL; 09-27-2011 at 03:55 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 09-27-2011
Thankyou,

The snipit
Code:
DATE_DIRS=$(ls -d 20* 2> /dev/null)

seams to do what I needed it to do.Smilie

So what exactly is going on here?

The ls is list, the '-d' filters for directories, and the 20* is the pattern the directories must match.... but what does the "2 > /dev/null" do ??Smilie
# 4  
Old 09-27-2011
Quote:
but what does the "2 > /dev/null" do ??Smilie
this is sending stderr to the /dev/null file. /dev/null is basically a black hole so in essence its just not sending any error messages to your screen.
This User Gave Thanks to Tommyk For This Post:
# 5  
Old 09-27-2011
Ahh, OK. Thankyou.

so in
Code:
"2 > /dev/null"

the "> /dev/null" uses the standard redirect into a "black hole" called "/dev/null" LOL.

How about the digit "2" ? Does the number "2" just kinda stands for "to", or does it hold some special meaning in this context?
# 6  
Old 09-27-2011
Quote:
the "> /dev/null" uses the standard redirect into a "black hole" called "/dev/null" LOL.

How about the digit "2" ? Does the number "2" just kinda stands for "to", or does it hold some special meaning in this context?
the actual addition to the command is:

Code:
2> /dev/null

notice there is no space between the 2 and the >.

> means redirect standard output (stdout)
2> means redirect standard error (stderr)

and as your directing it into /dev/null it is written to a special file which discards anything sent to it.

you could do something like:

Code:
DATE_DIRS=$(ls -d 20* 2> /home/yourhome/error.out)

and then you can later see what error messages were sent from running the command but as you dont care /dev/null just discards the message for you.
This User Gave Thanks to Tommyk For This Post:
# 7  
Old 09-27-2011
Supper.

I understand it now.

Thanks for the details !

I can really understand why people both LOVE and HATE this langauge.

SO much fuctionality is packed into so few charecters - makes the code terse and fuctional, but hard to understand unless yon know the 'secret code" Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find Null values in Columns and fail execution by displaying error message

Hi All, I am new to shell scripting. I have a requirement as part of my job to find out null/empty values in column 2 and column 3 from a CSV file and exit the further execution of script by displaying a simple error message. I have developed a script to do this by reading various articles... (7 Replies)
Discussion started by: tpk
7 Replies

2. UNIX for Dummies Questions & Answers

List the directories, having given pattern in the directories name, sorted by creation date

It is for HP-Unix B.11.31. Requirement: 1. List the directories, having given pattern in the directories name, sorted by creation date. Example: Directories with name "pkg32*" or "pkg33*" 2. On the output of 1. list the directories by creation date as sort order, with creation date... (2 Replies)
Discussion started by: Siva SQL
2 Replies

3. UNIX for Dummies Questions & Answers

Eliminate error message (/dev/null)?

I am trying to eliminate an error message from a script. This is the error message: find: stat() error /usr/openv/netbackup/db/images/KUMAX: No such file or directory if ]; then runthiscommand=`su nxadm -c "ssh -t $new3 exec /bin/sh -s">/tmp/filew3 2>/tmp/error.txt<<EOF ... (1 Reply)
Discussion started by: newbie2010
1 Replies

4. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

5. Shell Programming and Scripting

Bash function accepting list of strings and error message

I have a variable strLst containing a list of strings. I want to create a function that takes the list and an error message. If the number of strings is 0 or greater than 1, I print the error message. (1 Reply)
Discussion started by: kristinu
1 Replies

6. Emergency UNIX and Linux Support

List matching directories in current folder only on AIX

Hi, I need to find the list of matching direcories in current folder only and no subfolders on AIX.I tried -maxdepth option but its not working. Also, tried ls -d option to list the matching directories but getting argument list too long... So, any help would be appreciated. (6 Replies)
Discussion started by: sachinkl
6 Replies

7. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

8. Shell Programming and Scripting

How to list all the directories, sub directories in a mount along with size in ascending order?

Hi , I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In a particular mount, have to list all the directories and sub directories along with size of the directory and sub directory in ascending order. Please help me in this regard and many... (4 Replies)
Discussion started by: nmakkena
4 Replies

9. Shell Programming and Scripting

REGEX: Matching Null?

I'm using the URL Regex feature of Squid for allowing sites via a list of regex strings to match allowed domains. The regex was actually copied from our previous proxy solution and it seemed to "just work". But, we've recently discovered that some domains (likely due to virtual hosts or host... (2 Replies)
Discussion started by: deckard
2 Replies

10. Shell Programming and Scripting

null string matching in sed?

Hello, I would assume the expression ^$ should match a null string. Yet when I run: echo -n | sed 's/^$/nullstring/' I get no output. Can anyone tell me why? (6 Replies)
Discussion started by: Allasso
6 Replies
Login or Register to Ask a Question