Visit The New, Modern Unix Linux Community


How to check whether directory has files in it or not in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check whether directory has files in it or not in shell script?
# 1  
Linux How to check whether directory has files in it or not in shell script?

hi,

I am having script in which i want to check if directory has any file in it or not. If directory contains a single or more files then and only then it should proceed to further operations...

P.S.: Directory might have thousand number of files. so there might be chance of getting error message of argument list too long...
if anyone can help me with this? Code snippet is as follows -
Code:
files=directory1/* 
 if [ ${#files[@]} -gt 0 ]     
 then
          tar -czvf $file1.tar.gz directory1/ 
          mv $file1.tar.gz directory2

Issue is that though directory1 is empty it is going inside if and creating tar file of the empty directory structure. If there are no files under directory1 then i just want to avoid tarring of the same.

Last edited by VSom007; 05-13-2013 at 09:59 AM..
# 2  
Please search. There are various threads stating similar topic.

e.g. the one I found is here
This User Gave Thanks to clx For This Post:
# 3  
Code:
if [ `ls | wc -l` -gt 0 ]

This User Gave Thanks to balajesuri For This Post:
# 4  
Code:
CNT=`ls $dir/* | wc -l`
  if [ $CNT -eq 0 ]; then
    echo "There are no files in the dir"
  else
    echo "There are files in the dir"

This User Gave Thanks to apenkov For This Post:
# 5  
GNU Linux:
Code:
if find "$dir" -prune -empty | grep -q .
then
  echo "$dir is empty"
fi

This User Gave Thanks to MadeInGermany For This Post:

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #291
Difficulty: Easy
In August 2016, the United States government announced a new federal source-code policy. This policy mandates that at least 50% of custom source code developed by or for any agency of the federal government must be released as open-source software (OSS).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check the files existence inside a directory.

Hello Folks, On Below Script, I want to apply condition. Condition that it check the directory for files if not found keep checking. Once directory have files and founded do the calculation and finish the code. Script to check the files existence inside a directory, If not then keep... (16 Replies)
Discussion started by: sadique.manzar
16 Replies

2. Shell Programming and Scripting

Shell script to check a file and delete old files

Hello, I needed help with a shell script where in it checks if a file exists under a directory and also checks the age of the file and delete it if it is older than 3 weeks. thanks (10 Replies)
Discussion started by: hasn318
10 Replies

3. UNIX for Dummies Questions & Answers

Shell script to check the file sitting in the directory more than 10 hours

Hi, I require shell script to check for any pending files which are sitting in the particular directory for more than 10 hours. Please help me on this...Thank you. (5 Replies)
Discussion started by: kiruthiish
5 Replies

4. Shell Programming and Scripting

Moving Files one directory to another directory shell script

Hi, Could you please assist how to move the gz files which are older than the 90 days from one folder to another folder ,before that it need to check the file system named "nfs" if size is less than 90 or not. If size is above 90 then it shouldn't perform file move and exit the script throwing... (4 Replies)
Discussion started by: venkat918
4 Replies

5. Shell Programming and Scripting

Shell/perl script to check for files

Hi, I am trying to write a script for following scenario: I have a list of countries from where I receive files...eg. (Indonesia, Thailand, Australia...etc) For each country, I have a list of files that they send. IND -- a,b,c TH -- p,q,r AU -- x,y,z The path for these files could... (2 Replies)
Discussion started by: neil.k
2 Replies

6. Shell Programming and Scripting

Shell script to check files if exist else touch the file

Hi All, Thanks in Advance I wrote the following code if then echo "version is 1.1" for i in "subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account" do FILE="SDP_DUMP_$i.csv" echo "$FILE" ... (5 Replies)
Discussion started by: aealexanderraj
5 Replies

7. Shell Programming and Scripting

need a shell script to extract the files from source file and check whether those files existonserve

Hi, I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is # echo $0 -sh My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
Discussion started by: muraliinfy04
13 Replies

8. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

9. Shell Programming and Scripting

Shell script to check the files hourly and purge

I'm new to shell scripting... i have been given a task.. can any one help in this regard.... 1) Check hourly for files in <destination-path><destination-file-template><destination-file-suffix> for files older than <destination-file-retention> days and purge. It should then check... (1 Reply)
Discussion started by: satishpabba
1 Replies

10. Shell Programming and Scripting

HOW TO CHECK ONLY .C FILES EXISTS OR NOT IN A FOLDER using IF in C shell script?

Hi friends.. I hav a problem.... I dont know how to check .c files exists r not in a folder using IF in C shell script actually i tried like this if(=~ *.c) even though some .c files or there in the current folder..it is not entering int o the if control statement...... (17 Replies)
Discussion started by: p.hemadrireddy
17 Replies

Featured Tech Videos