Delete File in a Directory Using a Condition

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete File in a Directory Using a Condition
# 1  
Old 09-03-2013
Delete File in a Directory Using a Condition


I have a directory with many files whose creation time is distributed all over the day.

I need ANY 20 files per hour. So, I need
20 files for hour 00
20 files for hour 01
20 files for hour 23

What I have done so far is not great. Here is the code:
# get the Month
a=`echo $(date) | awk -F" " '{print $2}'`
# get the date
b=`echo $(date) | awk -F" " '{print $3}'`
c="$a  $b"

# loop over hours and minutes to generate all possible combination
for (( hh=01; hh<=23; hh++ ))
  for (( mm=01; mm<=59; mm++ ))
    if [ "${#hh}" == 1 ]; then
    if [ "${#mm}" == 1 ]; then
    # this is the final pattern for which I need files
    pattern=" $c $h:$m"
    echo $pattern

Still this is incomplete. What I want to do next is that: inside the loop, grep with the pattern and delete one but all..... So, I have 1 file per minute.

I hope that there is a much better way to do this. I just need any 'x' number of files per hour.

# 2  
Old 09-03-2013
Didn't quite get your requirement.
You need 1 file per minute, yet you said you select 20 files per hour Smilie and what is this pattern?

# 3  
Old 09-03-2013
I need 20 files per hour. But the code I have written is for 60 files per hour.
That's why I told this is not the best solution.

My requirement is:
From a directory having files created at different times, I need 20 files created in the same hour, for every hour.
# 4  
Old 09-03-2013
So, you need only 20 files per hour for every hour and want to delete the remaining files in the directory?

# 5  
Old 09-03-2013
I agree with ahamed101; your requirements are not clear.

Do you want a list of 20 files/per hour from the current working directory for the current date?

Do you want to delete all but 20 files for each hour from the current working directory for today's date? If so, do you care which 20 files are kept?

Note that since you're using today's date, there is no way to guarantee that other files won't be created in hour 23 after you run this script unless you think you can guarantee that this script will run in its entirety during the last clock tick of the day on your system and that no other CPUs will be used to create another file during or after the time when this script runs.
# 6  
Old 09-03-2013
Sorry for the unclear requirements . I did not find better words to explain it.

Yes I need 20 files per hour for every hour in the directory and delete the remaining files. The 20 files could be ANY.

---------- Post updated at 04:44 PM ---------- Previous update was at 04:41 PM ----------

Don, The script that I provided will be run everyday, because the file creation is a continuous process.
# 7  
Old 09-03-2013
Originally Posted by shekhar2010us

---------- Post updated at 04:44 PM ---------- Previous update was at 04:41 PM ----------

Don, The script that I provided will be run everyday, because the file creation is a continuous process.
I was just pointing out that if you run your script at 10pm, there may be a lot more than 20 files in the 22 and 23 hours of that day.

Even if you run your script at 2359, files may still be created in the last minute of the day that will leave you with more than 20 files in the 23 hour by the time you get to midnight. And, if this script is run by cron, there is no guarantee that if you schedule it for 2359 (or even 2330) that it will start running before midnight.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to delete file in directory

Will the below bash delete all the "snps.ivg" in the given directory? Thank you :) find N:\all_array\Samples -maxdepth 1 -type f -name "snps.ivg" -delete (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Delete lines from file based on condition

I want to keep last 2 days data from a file and want to delete others data from the file. Please help me. Sample Input # cat messages-2 Apr 15 11:25:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started. Apr 15 11:25:03 test1 rsyslogd: (re)start Apr 16 19:42:03 test1 kernel:... (2 Replies)
Discussion started by: makauser
2 Replies

3. UNIX for Dummies Questions & Answers

Delete .txt file from current directory

I have created few text file during run time by redirecting the txt file echo USER_LIST_"$(date '+%d%h%Y')".csv > report_location.txt report_location.txt is creating in the same location where I run script home/script When I try to remove the txt file at the end of the... (3 Replies)
Discussion started by: stew
3 Replies

4. Shell Programming and Scripting

Delete records within a file upon a condition

Hi Friends, I have the following file, cat input chr1 1000 2000 chr1 600 699 chr1 701 1000 chr1 600 1710 chr2 900 1800 Now, I would like to see the difference of Record1.Col2 - Record2.Col2 Record1.Col2 - Record2.Col3 Record1.Col3 - Record2.Col2 Record1.Col3 - Record2.Col3 ... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

5. Shell Programming and Scripting

Read column from file and delete rows with some condition..

Hi.... I have a need of script to do delete row whenever condition is true.... 2.16 (3) 1 3 9999 0 (1) (0) 34.42 (4) 1 3 9999 37 (2) (3) 34.38 (4) 1 3 9999 64 (2) (3) 34.4 (4) 1 3 1 ... (13 Replies)
Discussion started by: nex_asp
13 Replies

6. UNIX for Dummies Questions & Answers

Delete records from a big file based on some condition

Hi, To load a big file in a table,I have a make sure that all rows in the file has same number of the columns . So in my file if I am getting any rows which have columns not equal to 6 , I need to delete it . Delimiter is space and columns are optionally enclosed by "". This can be ... (1 Reply)
Discussion started by: hemantraijain
1 Replies

7. Shell Programming and Scripting

Parse tab delimited file, check condition and delete row

I am fairly new to programming and trying to resolve this problem. I have the file like this. CHROM POS REF ALT 10_sample.bam 11_sample.bam 12_sample.bam 13_sample.bam 14_sample.bam 15_sample.bam 16_sample.bam tg93 77 T C T T T T T tg93 79 ... (4 Replies)
Discussion started by: empyrean
4 Replies

8. UNIX for Dummies Questions & Answers

Not able to delete this file/directory/entry

Hello UNIX gurus, I noticed this file or whatever in one of our directories, and somehow I am not able to proceed with my work, without deleting this one. .insert--- 1 siebload intrface 0 Feb 22 01:25 Testfile I am confused, as it doesnt appear to be a file, and on doing any... (2 Replies)
Discussion started by: ppathak1234
2 Replies

9. Shell Programming and Scripting

Moving file to directory based on condition.

Can any one help me to correct following script. I have 2 directories DropZone and ProcessZone. File pattern is *VEHDESCSUM*. Finding the 'no of files' in DropZone directory using ls *VEHDESCSUM* |wc -l If DropZone has more than one file or 0 files then exit 1 If DropZone has one file then... (2 Replies)
Discussion started by: ramanagh
2 Replies

10. Shell Programming and Scripting

i want to delete a file based on existing file in a directory

hi i am having four files in a 1)sampleRej 2)exampleRej 3)samplemain 4)examplemain my requirement is i have to search for the rejected files (sampleRej,exampleRej) in a directory.if these files in that directory then i have to delete the main files... (3 Replies)
Discussion started by: srivsn
3 Replies
Login or Register to Ask a Question