Deleting a file with no corresponding pair | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Deleting a file with no corresponding pair

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-11-2013
ida1215 ida1215 is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 3 June 2014, 3:14 AM EDT
Posts: 127
Thanks: 63
Thanked 1 Time in 1 Post
Deleting a file with no corresponding pair

Hi, I am working with 2 sets of files (*csv and *asc) and I wanted to delete asc file with no corresponding csv counterpart. I did tried it manually but its been difficult working with a longer list of files.


Code:
sample files in directory
20120601.csv 20120601_f1.asc
20120603.csv 20120602_f1.asc
20120603_f1.asc

desired output
delete 20120602_f1.asc because it has no 20120602.csv file

Thank you very much.
Sponsored Links
    #2  
Old 11-11-2013
neutronscott's Avatar
neutronscott neutronscott is offline Forum Advisor  
script kiddie
 
Join Date: Jun 2011
Last Activity: 24 November 2014, 8:22 PM EST
Location: McMurdo Station, Antarctica
Posts: 822
Thanks: 28
Thanked 245 Times in 231 Posts
We loop across the *.asc files, and figure out what the csv would be by stripping everything after the "_" and tacking .csv to the end... right?

Try this:


Code:
for asc in *.asc; do
  csv=${asc%_*}.csv
  [ -e "$csv" ] || rm -i "$asc"
done

The Following User Says Thank You to neutronscott For This Useful Post:
ida1215 (11-11-2013)
Sponsored Links
    #3  
Old 11-11-2013
ida1215 ida1215 is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 3 June 2014, 3:14 AM EDT
Posts: 127
Thanks: 63
Thanked 1 Time in 1 Post
Hi neuronscott, thank you for this time saver code. its just what I needed,

If its ok, I would like to know, whats this part of the command does?

Code:
[ -e "$csv" ]

Thanks again.
    #4  
Old 11-11-2013
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 19 December 2014, 1:20 PM EST
Location: Switzerland - GE
Posts: 5,980
Thanks: 171
Thanked 440 Times in 412 Posts
the condition is true if the specified file in variable exist...
The Following User Says Thank You to vbe For This Useful Post:
ida1215 (11-11-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pair wise comparisons Diya123 Shell Programming and Scripting 1 06-11-2013 03:59 AM
Sort files as pair file Aswex Shell Programming and Scripting 4 03-21-2012 11:56 AM
Parsing with Name value pair and creating a normalized file smee Shell Programming and Scripting 1 02-15-2012 04:16 AM



All times are GMT -4. The time now is 07:28 AM.