Shell Scripting on files in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Scripting on files in directory
# 1  
Old 08-11-2015
Shell Scripting on files in directory

Hi,

The requirement is below:

There are 5 files in a unix directory and i need to pick up latest file from the directory that i can do like this
Code:
ls -lrt | tail -1

and this file can be processed. now the actual requirement is some times what will happen the second third...on files i.e(
Code:
ls -lrt | tail -2, -3...

) did not processed(may missed to process some times). Now what i required is the loop has to loop and check for all the 5 files and compare with the already processed files and if any file was not processed(missed file) has to pick and process next run.

Please provide some looping solution compare with already processed list.

Many thanks in advance.


Thanks,
Praveen

Last edited by vbe; 08-11-2015 at 01:48 PM.. Reason: code tags please not icode
# 2  
Old 08-11-2015
Code:
ls /directory > tmp.tmp   # get five files
awk ' FNR==NR {arr[$0]++; next}
        { arr[$0]++}
        END{for(i in arr) {if(arr[i]==1 {print i}  }}  ' tmp.tmp  mylist_of_files

tmp.tmp has the names of the five files, mylist_of_files has the names of all processed files.

BTW: how are your sure the files you want to process are completely written?
IF a process is writing them the file will show up in ls, it just will not necessarily have all the records it is supposed to have.

When you have file lists you also have to be sure about how they are "named" in the file. The above script
will treat /directory/file100 and file100 as completely different files.
# 3  
Old 08-11-2015
If the file listfile contains the files you want to process (sans directory), then the post-processing could be something like:
Code:
targetdir=/.../directory
watchdir=$targetdir/.watchdir
listfile=/.../listfile

cd $targetdir
mkdir -p $watchdir

while sleep ${delay}; do
  for file in $(< ${listfile}); do
    if [[ ! -e ${watchdir}/${file} -o ${file} -nt ${watchdir}/${file} ]]; then
        touch -r ${file} ${watchdir}/${file}
        process ${file}
    fi
  done
done

But as Jim_McNamara rightly pointed out, processing a file that is being written too is the path to sadness - you have a very good chance of reading an incomplete file.

What is creating the source files?
# 4  
Old 08-11-2015
When 'parsing' files that have chances that they are still beeing written,
i would fallback on checking their last change (using stat), and only if the change is older than <insert reasonable timeout> i'd start reading it.

The timeout could be anything from 5 secs to 5 hours.

hth
# 5  
Old 08-11-2015
Or you can wait until the file is closed, something like:
Code:
lsof +r filename > /dev/null
process filename

Or logrotate could be used.

There are many many ways you can get this done, depending on the type of file and how it is being written.

Can additional details be provided?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. 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

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

5. Shell Programming and Scripting

Compare two files using shell scripting

Hi, I need to compare two files using shell scripting. Say like: File1 AAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBB CCCCCCCCCCCCCCCCCCCCCCCCC eeeeeeeeeeeeeeeeeeeeeeeee DDDDDDDDDDDDDDDDDDDDDDDDDDDD File2 BBBBBBBBBBBBBBBBBBBBB DDDDDDDDDDDDDDDDDDDDDDDDDDDD AAAAAAAAAAAAAAAAAAAA ... (6 Replies)
Discussion started by: roshParab
6 Replies

6. Shell Programming and Scripting

how to find files and replace them in a directory in Shell scripting

I have a directory /java/unix/data In data directory i have so many files from which i want to find some files who look alike below.(there are number of such below such files as well different files too in the data directory) -68395#svg.xml -56789#ghi.xml -67894#gjk.org -56734#gil.txt I... (6 Replies)
Discussion started by: pratima.kumari
6 Replies

7. UNIX for Dummies Questions & Answers

shell scripting with files- how to?

Hi, I'm trying to create a shell script that shows a report of each real user of the system showing for each user the list of files that is in $home and its subdirectories (sorted by hour of modification), clasified by file type, and finally showing the total size and number of files in that... (1 Reply)
Discussion started by: ubu-user
1 Replies

8. Shell Programming and Scripting

Help ME with Shell scripting log files

I currently have a script that was written a while back and it generates it's own log file. I had to recently alter this script so that it would execute another script in parallel 10 times such as the following example; echo "It is currently: ";date int=0; cap=10; while((int <... (2 Replies)
Discussion started by: afbuisiii
2 Replies

9. Shell Programming and Scripting

shell scripting with directory

I have a directory /ndata/nmk I want to have 4 copies of my daily database backups like /ndata/nmk/copy1 /ndata/nmk/copy2 till copy4 . where copy1-copy4 are directories having my db backups . Once my db backups reach 4 directories like copy4 i want to again write from copy1 to copy4 . ... (3 Replies)
Discussion started by: suku123
3 Replies

10. Shell Programming and Scripting

rename files using shell scripting

Hi all, i want to rename some files in my directory using korn shell scripting. 1) i want to rename files who have no extension so that they will have the format: filename.extension and 2) i want the files who has extension initially, to stay the same (they will not be... (4 Replies)
Discussion started by: gfhgfnhhn
4 Replies
Login or Register to Ask a Question