Conditional deletion of files based on extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional deletion of files based on extension
# 1  
Old 10-29-2015
Apple Conditional deletion of files based on extension

Hello All,

I have some files like file, file.chk, file.sem and file.temp in huge. I would like to delete some files based on following criteria.

1. Unconditionally delete .sem and .temp files
2. If we found the actual file, don't remove .chk file, otherwise remove .chk file as well

for example I have:
Code:
file1.20150912.001
file1.20150912.001.chk
file2.20150913.002.sem
file2.20150913.002.temp
file3.20150914.003.chk
file4.20150914.004
file4.20150914.004.chk
file4.20150914.004.sem
file4.20150914.004.temp
file5.20150915.005.sem
file6.20150916.006.temp
file7.20150917.007

I should straight a way delete following files:
Code:
file2.20150913.002.sem
file2.20150913.002.temp
file3.20150914.003.chk
file4.20150914.004.sem
file4.20150914.004.temp
file5.20150915.005.sem
file6.20150916.006.temp

Should NOT delete following files.
Code:
file1.20150912.001
file1.20150912.001.chk
file4.20150914.004
file4.20150914.004.chk
file7.20150917.007

for this I have write the script as following:

Code:
#!/bin/bash
cd /apps/NAS/it/phome
find ./*/in -type f \( -name '*.sem' -o -name '*.temp' \) |
while read -r dfile
do
        echo rm -f "$dfile"
        CHK=${dfile/.sem/.chk}
        FILE=${dfile%.*}
        [ -f "$CHK" ] && [ ! -f "$FILE" ] && echo rm -f "$CHK"
done

But no luck, its not deleting .chk files. Could you please help me to fix if there is any issue in my above code?
Thanks in advance, appreciate your patience.
Vasu

Last edited by VasuKukkapalli; 10-29-2015 at 03:10 PM..
# 2  
Old 10-29-2015
How can it construct file3.20150914.003.chk when there is no file3.20150914.003.sem?
I suggest you have a separate loop for the .chk files.
# 3  
Old 10-29-2015
I don't understand the 2nd requirement.
Quote:
Originally Posted by VasuKukkapalli
If we found the actual file, don't remove .chk file, otherwise remove .chk file as well
What is the actual file in the above quote that determines whether or not the .chk file is supposed to be removed.

With files with names like:
Code:
base
base.chk
base.sem
base.temp

it looks like your current code will remove base.sem and base.temp unconditionally, and will remove base.chk if and only if base.sem existed and base does not exist.

If you want to remove base.chk if an only if base does not exist (in addition to removing base.sem and base.chk), try something more like:
Code:
#!/bin/bash
cd /apps/NAS/it/phome
find ./*/in -type f \( -name '*.sem' -o -name '*.temp' -o -name '*.chk' \) |
while read -r dfile
do	case "$dfile" in
	(*.sem|*.temp)	echo rm -f "$dfile";;
	(*.chk)		FILE=${dfile%.*}
			[ ! -f "$FILE" ] && echo rm -f "$dfile";;
	esac
done

NOTE: -o added before -name '*.chk' as suggested by MadeInGermany in post #5 in this thread.

Last edited by Don Cragun; 10-29-2015 at 05:17 PM.. Reason: Add missing -o.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 10-29-2015
I am sorry for the typo Don, If actual file present, don't remove .chk file, if the actual file not present, remove the .chk file

for example, if i have file1, file1.chk, file2, file3.chk, in that case, remove only file3.chk.

Hope this clarifies.
# 5  
Old 10-29-2015
Don, your find is missing a -o
--
With separate finds; the first rm can be efficiently handled within the find:
Code:
...
find ./*/in -type f \( -name '*.sem' -o -name '*.temp' \) -exec echo rm -f {} +
find ./*/in -type f \( -name '*.chk' \) |
while read -r dfile
do
    FILE=${dfile%.*}
    [ ! -f "$FILE" ] && echo rm -f "$dfile"
done

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 10-29-2015
Thank you So much Don, Its working now!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

2. UNIX for Dummies Questions & Answers

Deletion of list of user based on a text file In LDAP UNIX server

Dear All, It would be really nice, if you could help me to write a script for deletion of list of user( more than 15000 users) stored in a file and sorted by email address( i need deletion of only a particular type of mail address). Is the any script to write and take the file as input and... (3 Replies)
Discussion started by: Chand
3 Replies

3. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

4. Programming

Python Conditional Statements Based on Success of Last Command

In shell scripting, I can create a conditional statement based on the success or failure (exit status)of a command such as: pinger() { ping -c 2 $remote_host >/dev/null 2>&1 ping_stat=$? } pinger if ]; then echo "blahblahblah" exit 0 fi how is this done using Python using... (3 Replies)
Discussion started by: metallica1973
3 Replies

5. Shell Programming and Scripting

Perl: Conditional replace based on previous and current value in a line

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (9 Replies)
Discussion started by: naveen@
9 Replies

6. Shell Programming and Scripting

Adding lines to files based on file extension

I have posted this before but did not get many replies, so here it goes again. I have several files name like this If the file extension is 1a, I woould like to add at the beggining of the file the following sequence If the file extension is 1b, thn the entry that should be added is the next... (2 Replies)
Discussion started by: Xterra
2 Replies

7. Programming

Conditional Compilation based on Environmental Variable in Unix

I want to be able to access an environment variable to control how a program is compiled. So: export MY_VERSN=9 Then ideally, within my C++ code, I would have #if MY_VERSN = 9 iret = FRED9() #else iret = FRED() #endif The way I thought I could do it is that in the script that... (2 Replies)
Discussion started by: BrighterLater
2 Replies

8. UNIX for Dummies Questions & Answers

conditional deletion of log files

i am a newbie and learning the ropes.........want to know how can i include a piece of code in a script (which redirects log files to a company standard out file) to delete the log files which are empty but should retain only those which has some process information in it......this should happen... (3 Replies)
Discussion started by: sonali007
3 Replies

9. Shell Programming and Scripting

Regarding deletion of old files

Hi, I have a list of directories which contain old files that are to be deleted. I put the list of all directories in a txt file and it is being read by a script Iam searching for the files older than 60 days using mtime and then deleting it But all the files are getting deleted... (3 Replies)
Discussion started by: Chidvilas
3 Replies

10. UNIX for Advanced & Expert Users

file extension based software execution

Hi Experts, I am working in HP-UX 11.0 workstaion. How can i make a file to be executed with the corresponding software to be invoked or executed. as in Windows. ie., if a file index.html has to be double clicked to invoke the Netscape Navigator. What i have to do to achieve this... (2 Replies)
Discussion started by: anent
2 Replies
Login or Register to Ask a Question