The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
removing extra files in dos namishtiwari Shell Programming and Scripting 11 08-27-2009 11:27 AM
csh script for deleting extra spaces in text file hertingm Shell Programming and Scripting 2 03-12-2009 04:22 PM
Deleting the similar lines avi.skynet Shell Programming and Scripting 2 02-02-2009 05:09 AM
Comparing similar columns in two different files ragavhere Shell Programming and Scripting 13 04-16-2008 09:53 AM
Appending text to a number of similar filenames Djaunl UNIX for Dummies Questions & Answers 4 10-10-2007 12:16 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-30-2009
smpatil4 smpatil4 is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 2
Deleting extra files with similar filenames

Hello,

I have a large amount of files under a root directory, with several sub-directories, and many of these sub-directories have similar files with similar names. I need to clean this up.

The filenames are of the format:


Code:
/path/to/dir/subdir/file name.dat
/path/to/dir/subdir/file name 1.dat

I want to keep only
Code:
/path/to/dir/filename.dat

and remove the other file. I have tried some tools including fslint, but it didn't work because the actual content of the files may vary slightly.

Help in creating a bash script or similar to weed out the unneeded files would be highly appreciated.

Thanks!
  #2 (permalink)  
Old 09-30-2009
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 722
If you have only single level subdirectories you could use

Code:
rm /path/to/dir/*/filename.dat

to delete all occurences of "filename.dat" in the subdirectories immediately underneath /path/to/dir/
or if you want also remove similar files you could e.g. try something like:

Code:
ls /path/to/dir/*/file*name*.dat

if it lists exactly the files you need to remove you can

Code:
rm /path/to/dir/*/file*name*.dat


Last edited by Scrutinizer; 09-30-2009 at 05:32 PM..
  #3 (permalink)  
Old 10-01-2009
smpatil4 smpatil4 is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 2
Unfortunately its not that straightforward.

For one, I don't know the file names beforehand. Also, the sub-directories are nested, up to three levels deep. There's a total of 30,000 files with an estimated 10,000 duplicates.

Let me try and clarify: I want to delete duplicate files. These duplicate files are in the same folder as the original and have a " 1" at the end of the filename. Some originals may also have the " 1" at the end of the filename, but duplicates for them don't exist.
  #4 (permalink)  
Old 10-01-2009
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 722
OK, that is quite a different requirement . Assuming the duplicates always have " 1" at the end, followed by the extension ".dat", you could try this ksh/bash code:


Code:
BASEDIR='/path/to/dir'
ext='.dat'
duplext=' 1'
find "$BASEDIR" -type d | while read dir; do
  find $dir -name "*$ext" -maxdepth 1 -type f | while read file; do
    duplicate="${file%$ext}$duplext$ext"
    if [[ -f "$duplicate" ]]; then
      rm "$duplicate"
    fi
  done
done


Last edited by Scrutinizer; 10-01-2009 at 05:36 PM..
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0