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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 11-11-2013
neutronscott's Unix or Linux Image
neutronscott neutronscott is offline Forum Advisor  
script kiddie
 
Join Date: Jun 2011
Last Activity: 23 February 2015, 11:01 AM EST
Location: McMurdo Station, Antarctica
Posts: 822
Thanks: 28
Thanked 246 Times in 232 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 Unix and Linux 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,Unix or Linux Image

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

Code:
[ -e "$csv" ]

Thanks again.
    #4  
Old Unix and Linux 11-11-2013
vbe's Unix or Linux Image
vbe vbe is online now Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 5 March 2015, 9:15 AM EST
Location: Switzerland - GE
Posts: 6,015
Thanks: 182
Thanked 451 Times in 422 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 10:32 AM.