Script to Compare file size and delete the smaller


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to Compare file size and delete the smaller
# 1  
Old 08-03-2012
Power Script to Compare file size and delete the smaller

I am pretty new to scripting, so I appreciate your advice in advance.
The problem:

100 directories each containing 2 files that have the same extension with random names. The only attribute that discriminates the files is size. I would like to write a script that compares the files for size and deletes the smaller of the two. Thanks! - OSX, X11, bash
# 2  
Old 08-03-2012
Is every pair of extensions unique? If not, it's hard to be 100% sure about anything.
# 3  
Old 08-03-2012
The file names are random. The extension is the same (that is probably windows talk - sorry). I want the script to look in each directory and delete the smaller of the two files.
e.g.,

directory 1

asdfsdfo72392874983.nii.gz
asdfju07r-3828poafljkao.nii.gz

directory n

asdfopiw4-8rtlkjkkfa.nii.gz
ukjfsaoi04t0ifaf';lk'lk.nii.gz
# 4  
Old 08-03-2012
I think I misunderstood. They're in 100 different directories, not in one pile, ergo they can be differentiated. Okay.

Code:
find /path/to/base -type d | while read LINE
do
        set -- $(ls -S "$LINE"/*.gz)
        [ "$#" -eq 2 ] || continue # Ignore folders that don't have 2 files

        echo "Keeping $1"
        echo rm "$2"
done

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-03-2012
Thank you so much! I'm embarrassed to say I've been struggling with that for 2 days. (I have a hard time asking for help Smilie). On the whole the script is beyond my vocabulary, but could you say how you actually made the size comparison here and how you got that output to "$1" and "$2".
# 6  
Old 08-03-2012
Quote:
Originally Posted by Corona688
Code:
set -- $(ls -S "$LINE"/*.gz)

It would probably be better to omit the quotes. The reason I say that is because no quotes makes it clear that the approach isn't intended to handle whitespace and pattern matching characters. As is, casual inspection may instill a false sense of security. Whatever those quotes protect against in the subshell will just bite in the parent shell.

Regards,
Alister
# 7  
Old 08-03-2012
Quote:
Originally Posted by JC_1
Thank you so much! I'm embarrassed to say I've been struggling with that for 2 days. (I have a hard time asking for help Smilie). On the whole the script is beyond my vocabulary, but could you say how you actually made the size comparison here and how you got that output to "$1" and "$2".
ls -S sorts by file size. The largest one comes first.

As for how I get it into $1 $2, try this:

Code:
set -- a b
echo $1
echo $2

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to compare files in 2 folders and delete the large file

Hello, my first thread here. I've been searching and fiddling around for about a week and I cannot find a solution.:confused: I have been converting all of my home videos to HEVC and sometimes the files end up smaller and sometimes they don't. I am currently comparing all the video files... (5 Replies)
Discussion started by: Josh52180
5 Replies

2. Shell Programming and Scripting

Compare file size and then copy/overwrite

// Redhat I have this code working, but need to add one more qualification so that I don't overwrite the files. #!/bin/sh cd /P2/log/cerner_prod/millennium/archive/ for f in * do || continue #If this isn't a regular file, skip it. && continue #If a backup already... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

3. Shell Programming and Scripting

Compress a tar file to smaller size

I have a tar file with name DTT012_GP_20140207.tar and many more with different names of different sizes ranging from 1GB to 4GB. Now my requirement is to extract/not extract these files and then divide it into various parts of size 500MB and save it with different names and then compress... (5 Replies)
Discussion started by: Shaibal_bp
5 Replies

4. UNIX for Advanced & Expert Users

Physical disk IO size smaller than fragment block filesystem size ?

Hello, in one default UFS filesystem we have 8K block size (bsize) and 1K fragmentsize (fsize). At this scenary I thought all "FileSytem IO" will be 8K (or greater) but never smaller than the fragment size (1K). If a UFS fragment/blocksize is allwasy several ADJACENTS sectors on disk (in a ... (4 Replies)
Discussion started by: rarino2
4 Replies

5. UNIX for Dummies Questions & Answers

PSFTP- Compare file size

Hi, I'm using PSFTP to transfer files from one machine to a virtual machine with UBUNTU OS installed on it. I'm trying to find a way to make sure the files that I'm uploading / downloading are being uploaded/ downloaded properly. I want to compare the size of the local file and the remote... (0 Replies)
Discussion started by: sessie
0 Replies

6. Shell Programming and Scripting

zsh compare size pdf and delete bigger?

I have used an script to reduce the size of multiples pdf. This script creates files with the same name but with different extension. The extension of the compressed files is xpdf. Sometimes the "compressed" xpdf are bigger than the "uncompressed"pdf. I want to create a zsh script to compare each... (1 Reply)
Discussion started by: AMDx64BT
1 Replies

7. Shell Programming and Scripting

to write a script to compare the file size in the current directory and previous dir

hi, i am new to this site. i want to write a script to compare the file size of the files in the current dir with the files in the previous directory. the files name will be same, but the filename format will be as xyzddddyymm.txt. the files will arrive with the month end date(i want to... (5 Replies)
Discussion started by: tweety
5 Replies

8. Shell Programming and Scripting

How to compare size of two file which is in one directory

I have two file in a Directory.I want a script which will compare the Size of Two file. Can Anyone Help me on this: linasplg11:/opt/dataout/kk/linasplg11 # cat size -rwxrwxrwx 1 root root 16658 Jan 8 13:58 lina_IP_SIP_1231325621210.xml -rwxr-xr-x 1 root root 16672 Jan 8 14:30... (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies

9. Shell Programming and Scripting

compare file size from a output file from a script

Hi guys, firstly I'm working on SunOS 5.10 Generic_125100-10 sun4u sparc SUNW,Sun-Fire-V240 I've made a script to compress two directory and then send them to an other server via ftp. This is working very well. Inside theis script I decide to log usefull data for troubleshooting in case of... (7 Replies)
Discussion started by: moustik
7 Replies

10. Shell Programming and Scripting

How to compare file size after ftp?

Is possible if I want to campare file size on source and destination after ftp transfer? If anybody know, please explain to me. (1 Reply)
Discussion started by: icemania
1 Replies
Login or Register to Ask a Question