check for directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check for directory
# 1  
Old 01-31-2011
check for directory

Hi trying to read directory name and compare if exist or not,haw can I do that
thanks
Code:
#!/bin/bash
echo -n "Enter: "
read var 
 find . -name "$var" -type f


Last edited by lio123; 01-31-2011 at 01:59 PM..
# 2  
Old 01-31-2011
I THINK you are are asking for this, let me know if this is not what you mean.
Code:
#!/bin/bash
echo -n "Enter: "
read var 
 find . -name "$var" -type d | xargs ls -l

# 3  
Old 01-31-2011
Thanks for the help,if I use your code I am getting all files in teh directory
I am trying to enter sertain directory and if exist to print message "the directory exist,and then to print the size of it
# 4  
Old 01-31-2011
Code:
dirname="path/to/dir"
[ -d "$dirname" ] && echo "$dirname exists" && du -hs "$dirname"


Last edited by Yogesh Sawant; 02-01-2011 at 08:03 AM.. Reason: added code tags
# 5  
Old 01-31-2011
Quote:
Originally Posted by Corona688
dirname="path/to/dir"
[ -d "$dirname" ] && echo "$dirname exists" && du -hs "$dirname"
Which reads " Test if '$dirname' is a directory " and "if that previous statement is true, say '$dirname exists'" and if that runs successfully "return $dirname size" I only do this if you're new to shell scripts and need an idea what this is saying. Research Unix test conditions on google. There are other tests besides ' -d ', you can check files ' -f ', check if something exists in general ' -e ', etc. " && " is the same as saying " Do this next statement if the previous statement is successful " you can use " || " to say " Do this if the previous statement is NOT successful ". Anyways hope any new people reading this get some info from it.
# 6  
Old 01-31-2011
Combining the above posts we get:

Code:
#!/bin/bash
echo -n "Enter: "   ; read var
if [ -d "${var}" ]
then
     echo "Directory exists: ${var}"
     du -hs "${var}"
else
     echo "Directory missing: ${var}"
fi


@Corona688 and DC_Slick. I would hazard a guess that Corona688 first typed a unix Conditional Expression before DC_Slick was born and before "test" became a Shell built-in. Mind you I could be wrong.

Last edited by methyl; 01-31-2011 at 08:04 PM..
# 7  
Old 02-01-2011
Thank you for the help ,is there any way for the code to check if the directory + the sybdirectory exist
Thank you for the help again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory check

How can i check in shell script if the file is coming from same directory as previous file. (3 Replies)
Discussion started by: Pratiksha Mehra
3 Replies

2. Homework & Coursework Questions

Check whether a Directory is empty or not

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1.pls tell me the command for checking whether a given directory is empty or not . 2. can i check what is the... (1 Reply)
Discussion started by: upvan111
1 Replies

3. Shell Programming and Scripting

How to check if something is a file, directory or other?

I want to know how you would go about checking if something is either a file or a directory. mostly for argument validation stuff. I know -d is to see if its a directory but im guessing -f is for files?? (1 Reply)
Discussion started by: Waffles
1 Replies

4. Shell Programming and Scripting

Empty Directory Check

Hi All, I have a requirement to check all the files in a directory and mail non empty files Files are in csv format , i need to skip header while checking pls help Thanks (12 Replies)
Discussion started by: gwrm
12 Replies

5. Shell Programming and Scripting

Check for a New Directory

Well, I know I could use a cron job to check if a new directory is made. However, I know that it won't happen like every 5 minutes and would be waste of resources. I was wondering is there a better way to check if a new directory is made in a certain folder without a cron job. I do not want it to... (3 Replies)
Discussion started by: almeidamik
3 Replies

6. Shell Programming and Scripting

check whether the directory is empty or not

I have the list of users in user.log, under each user folder there is sub1 folder is there. i want to check whether sub1 is empty or not, if it is empty i have to skip that user user folder and iterate next user folders. i have the sample code,its not giving not proper results. while read line... (8 Replies)
Discussion started by: KiranKumarKarre
8 Replies

7. UNIX for Dummies Questions & Answers

how to check for a directory

Hi, what is the command to in unix shell to find whether a particular direcory is existing or not ? example: i am now in ~/scripts directory and want to find if a direcory called 'log' exists or not from a shell scripts. can somebody please help. thanks, sateesh (3 Replies)
Discussion started by: kotasateesh
3 Replies

8. UNIX for Dummies Questions & Answers

How to check directory size

how say I have directory how can I get the directory size in mega and if it less then 1 mega so byts Thanks (4 Replies)
Discussion started by: umen
4 Replies

9. Shell Programming and Scripting

check if directory exists

Hi, I need to prompt for a response from a user to enter a path read dest_dir?"Please Enter Directory :" How do I do this until a valid directory is entered by the user. I can use to check the existence of the directory. However when I try the following I cannot get it to work. while ... (2 Replies)
Discussion started by: jerardfjay
2 Replies

10. Shell Programming and Scripting

Check directory space?

Is there some command I can use to check to see if there is 2 Gig of space available in a directory before I created a 2 Gig file? (3 Replies)
Discussion started by: lesstjm
3 Replies
Login or Register to Ask a Question