While loop a file containing list of file names until the files are found?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop a file containing list of file names until the files are found?
# 1  
Old 04-18-2015
While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found, there is no need to look for that file again. If a file is not found I need to continue the loop for 2 hours to keep checking for files and if still not found then exit the script with error code( other than 0).

Control file(source files.txt)
Code:
PAS.txt
ESA.txt
HRA.txt

......

Code:
while read line
do
   test -e /data/project/US/$line
done < /home/user/ctlfile.txt

Moderator's Comments:
Mod Comment code tags please
# 2  
Old 04-18-2015
What operating system and shell (including version) are you using. It would be easy with some shells to create an array of unfound files.

With other shells, you'll need to use a file to keep track of unfound files.

Last edited by Don Cragun; 04-25-2015 at 03:40 AM.. Reason: Fix typo (s/files/shells/).
# 3  
Old 04-18-2015
Thank you.

I,m using Red Hat Enterprise Linux 6.5 and Korn shell.
# 4  
Old 04-18-2015
Hi, it can be done with arrays, like Don Cragun suggested. One way would be to use associative arrays (it could also be done with regular arrays). For example:

Code:
#!/bin/ksh
typeset -A files

while read f
do
  files["$f"]=
done < /home/user/ctlfile.txt

until [ ${#files[@]} -eq 0 ]
do
  for f in "${!files[@]}"
  do
    if [ -e "/data/project/US/$f" ]; then
      unset files["$f"]
    fi
  done 
  sleep 1
done

printf "%s\n" "All files are present."

This is just a rudimentary example you could try, checks would need to be added and some extra code to break it off after two hours ...



--
Note that the method you are proposing has a potential flaw, if it can happen that a file is placed in the directory and suequently removed then it will still be wrongfully reported as present...
So it may be advisable to have an extra final check for all files that need to be present..

Last edited by Scrutinizer; 04-19-2015 at 04:04 AM..
# 5  
Old 04-24-2015
Could I throw in a totally different suggestion? If you have a WANTED file with the required names and all the files should be in a single directory, could you follow this logic?:-
  • List all files in the directory
  • Use your WANTED file with grep to generate a list of all files that do exist.
  • Use grep again to check those still missing.
Something like this perhaps:-
Code:
WANTED=/tmp/my_file_list
cd $Directory
missing=1
until [ $missing -eq 0 ]
do
   ls -1 | grep -f $WANTED > /tmp/current
   missing=`grep -cvf /tmp/current $WANTED`
   sleep 7200
done

printf "All files have arrived."

It might mean fewer reads of the directory, although it does generate an intermediate file, so maybe, maybe not. There also might be confusion if your files are too similarly named, e.g. FileA arrives, but FileA1 is still missing, and this might not be spotted. You might need to mark the beginning and end of each line in the WANTED file with ^ & $ respectively.

Just an alternate option to theorise on perhaps.



Robin
# 6  
Old 04-25-2015
Robin, while fgrep -x eliminates the problems with grep, the sleep must be 1 in order to finish early. And then there is great overhead compared to the array solution (that is even optimized by removing once matched elements).
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to list files names and sizes in a directory and output result to the file?

Hi , I'm trying to list the files and output is written to a file. But when I execute the command , the output file is being listed. How to exclude it ? /tmp file1.txt file2.txt ls -ltr |grep -v '-' | awk print {$9, $5} > output.txt cat output.txt file1.txt file2.txt output.txt (8 Replies)
Discussion started by: etldeveloper
8 Replies

2. Shell Programming and Scripting

Finding a string in a list of files, print file names

I'm interested in writing a report script using BASH that searches all of the files in a particular directory for a keyword and printing a list of files containing this string... In fact this reporting script would have searches for multiple keywords, so I'm interested in making multiple... (2 Replies)
Discussion started by: chemscripter904
2 Replies

3. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

4. Shell Programming and Scripting

List the file names available on FTP server before selecting the required file

Below is my script code.which shows the environment name and then fetch the file from the ftp server but I am facing one issue.The script should be run in both way.We can pass the arguments with script and select the environment name then file name.Here the issue is I am not able to list the files... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

5. Shell Programming and Scripting

Print file names in a loop

OS : RHEL 6.1 Shell : Bash I have lots of files in /tmp/stage directory as show below. Using a loop, I need to print all the filenames in this directory except those ending with a number. How can I do this ? # pwd /tmp/stage # # # ls -l * -rw-r--r--. 1 root root 0 Oct 7 18:38 stmt1... (2 Replies)
Discussion started by: kraljic
2 Replies

6. Shell Programming and Scripting

Grep string in files and list file names that contain the string

Hi, I have a list of zipped files. I want to grep for a string in all files and get a list of file names that contain the string. But without unzipping them before that, more like using something like gzcat. My OS is: SunOS test 5.10 Generic_142900-13 sun4u sparc SUNW,SPARC-Enterprise (8 Replies)
Discussion started by: apenkov
8 Replies

7. Shell Programming and Scripting

Copying List of Files Under Different File Names ?!

Hi to all here , Excuse me for the lamer question but I need UNIX command for copying List of Files Under Different File Names ! for example I have this list: new_001.jpg new_002.jpg new_003.jpg ..... I want to copy all files that start with "new_" to the same directory but under... (7 Replies)
Discussion started by: boboweb_
7 Replies

8. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

9. Shell Programming and Scripting

File Names in a Variable in a loop

Hi All , I am having confusion in a shell script. Please guide me. I need to get multiple files (number of files vary time to time, file names are separated by '|') using FTP get from the remote server. Actually, i call the FTP function in a loop. At the last step, i need to move all the get... (3 Replies)
Discussion started by: spkandy
3 Replies

10. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies
Login or Register to Ask a Question