Script for checking the files in a folder


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script for checking the files in a folder
# 1  
Old 09-28-2011
Script for checking the files in a folder

Hi ,

I am using the below script for checking for a file in a folder.


Code:
if [ -f $FILE ];
then
echo 0
else
echo 1
fi

Is there any way we can check for files which are starting with GL*.csv.What I am trying to do is , I have to check in a folder for the GL*.csv files if there are any files they I have to retrun a code of 0 if there are no files then the return code should be 1.., the folder can have 1 file or more than 1 and it can have zero files also.

Pls give me some logic to do this.

--Wangkc
# 2  
Old 09-28-2011
Code:
ls GL*.csv &> /dev/null
if [ "$?" -eq "0" ]
then
  echo "Pattern found"
else
  echo "Not found"
fi

&>filename # Redirect both stdout and stderr to file "filename." # This operator is now functional, as of Bash 4, final release
# 3  
Old 09-28-2011
That's better than what I was thinking. Smilie I'm not sure what the "&>" is for when > will do, though. And you can reduce it to:

Code:
if ls GL*.gsv > /dev/null 2> /dev/null
then
        echo "Pattern exists"
else
        echo "Pattern doesn't exist"
fi

# 4  
Old 09-28-2011
One hiddeous kludge could be
Code:
[~/tmp]$ result=$(echo $(for i in test.* ; do if [ -e $i ] ; then echo 1; fi;done |uniq ))
[~/tmp]$ if [ 0$result -gt 0 ] ; then echo OK;fi
OK
[~/tmp]$

EDIT: and it seems the longer you think about it, the worse the solution Smilie
# 5  
Old 09-28-2011
In some case, if you have a huge number of files (exceeding MAX_ARGS) that match, then the command ls GL*.csv may behave erroneously:
you may encounter the error message : "arg list too long"

So you would get "Not found" whereas a bunch of such files would exist.

Up to you to see if you want to enforce your code against such case Smilie
# 6  
Old 09-28-2011
Thanks all for the replies.. I will try these., and some times I have more than 1000 files, so need to think about exceeding MAX_ARGS Smilie
# 7  
Old 09-28-2011
If you need to worry about MAX_ARGS, you have to use find.

Code:
COUNT=$(find ./folder -name 'GL*.csv' 2> /dev/null | wc -l)

if [ "$COUNT" -gt 0 ]
then
        echo "Found $COUNT files matching pattern"
else
        echo "No files matching pattern"
fi


Last edited by Corona688; 09-28-2011 at 01:24 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Script to keep checking the directory for files.

Hello Folks, Looking for a script which can keep doing ls to the directories and once file landed to the directory then ,read the files do further calculation and exit. Kindly guide. Regards, Sadique (3 Replies)
Discussion started by: sadique.manzar
3 Replies

3. Shell Programming and Scripting

Checking of file exist in different folder

Hi All, Seeking for your assistance to compare if the file in working directory is found on the DIR2 directory and if not found move the file in DIR2. ex. WORKING_DIR=/home/dir1/ file1.txt DIR2=/home/admin/users file1.txt what i did was found_nonempty='' for file in... (4 Replies)
Discussion started by: znesotomayor
4 Replies

4. Shell Programming and Scripting

Checking files in folder using starting string for filename

Hi, How do i check if there are any files present in the folder with some specific starting string. For eg :- I have used this where Source_File is filename parameter. if then return 2 fi But in my case the source file name is not constant. The only constant thing is... (10 Replies)
Discussion started by: chetancrsp18
10 Replies

5. Shell Programming and Scripting

Script should keep checking for availability of files.

Hi, I want my script to check for availability of few files contineously for some time. Lets say from 10:00 AM to 10:30 AM script should keep checking on a particular location for each file one by one. If all the files are available then only it should complete the next processing part, else... (2 Replies)
Discussion started by: amit.mathur08
2 Replies

6. Shell Programming and Scripting

Continously checking folder and executing files

Hello All, I want to make a script which continously checks one folder all the time that is there is any file in it or not, and if it found any file in it than execute that file with the following command. apxrcv -text < filename > outputfile Actually my requirement is that i will put... (4 Replies)
Discussion started by: wakhan
4 Replies

7. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

8. Shell Programming and Scripting

Checking if any folder is opened or not?

Alogorithm: ============= Whenever any user open any specific directory: 1) cd /usr/lib The prompt should show an Alert message like 2) echo "You have opened this folder" How should I write shell script this one?.. It should be irrespective of users and... (11 Replies)
Discussion started by: Niroj
11 Replies

9. Shell Programming and Scripting

Checking Files with a Shell Script

Hey everyone, I'm writing a shell script that needs to loop thru a directory and check a defined type of files(.df). I use "checkfile.x" which is a compiled program to check those files. Sintaxis : checkfile.x DatefileName.df The program displays something like this: File: kanswer.df -... (1 Reply)
Discussion started by: Testing_Yorsh
1 Replies

10. UNIX for Dummies Questions & Answers

checking missing files in side a folder

Dear all, Every hour i am receiving several data files to one folder for 24 hours each day.But some times some hours i do not have the files because of some problem.So i want to check the files inside the folder at the end of the day wether how many files i received in each hour like this.so i... (4 Replies)
Discussion started by: Nayanajith
4 Replies
Login or Register to Ask a Question