Sponsored Content
Top Forums Shell Programming and Scripting Script to compare files in 2 folders and delete the large file Post 302999860 by jim mcnamara on Thursday 29th of June 2017 12:58:03 AM
Old 06-29-2017
IF I understand:

You have one filename with several different extensions (or in windows, file types):
example filename.aa filename.qb filename.abcd and maybe more.

If this is correct you need to aggregate all of the complete filenames by just the part before the dot in the filename.

What you need for input is
Code:
 the filename with no directory name and without a type
 size of the file in bytes
 the full filename  (directory/filename.filetype)

Output has to be the full filename and maybe the size, but only for the largest file in bytes.

You then LOOK at the output to make sure you did not screw up somehow, right?

Then finally you feed the full filenames in the output file to the rm command.

So:
Code:
# get all the filenames in one place -> /tmp/list
find /path/to/directory1 /path/to/directory2 -type f > /tmp/list
#  you now have all the file names
#
# rewrite /tmp/list to have the correct values
while read fname   # fname is the complete file name
do
      shortfile=$(basename $fname)
      shortfile=${shortfile%%.*}
      size=(stat -c '%s' $fname)
      
      print " $shortfile $size $fname"
done < /tmp/list > /tmp/next

# /tmp/next has the data, so let's sort and aggregate it -  assuming no spaces in the shortfile name
# sort by shortfile

sort -k1 -k2n -o /tmp/next /tmp/next

# aggregate
# awk fields are $1 - shortfile, $2 - size,  $3 - fullname
awk '{ 
         arr($1)=$3 " " $2  # note that the last values to be stored for shortfile
                                   # come from  the last time shortfile is in the file
                                   
        }
         END { for (i in arr) {print arr(i)} }
        ' /tmp/next > /tmp/final
        
# delete ONLY after you check /tmp/final
while read fname
do 
     rm $fname
done < /tmp/final

This code is meant more to learn from than production. Others will show you how to make it more efficient. You need to understand this one first.
This User Gave Thanks to jim mcnamara For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare data in 2 files and delete if file exist

Hi there, I have written a script called "compare" (see below) to make comparison between 2 files namely test_put.log and Output_A0.log #!/bin/ksh while read file do found="no" while read line do echo $line | grep $file > /dev/null if then echo $file found found="yes" break fi... (3 Replies)
Discussion started by: lweegp
3 Replies

2. Shell Programming and Scripting

Looking for Large Files and Folders

Hello, On one of my UNIX boxes, there are many folders. I'm looking for a way / command that will search for and display folder names / location / size so I can do some cleanups. How can I do this? (8 Replies)
Discussion started by: bbbngowc
8 Replies

3. Shell Programming and Scripting

Script to Compare a large number of files.

I have a large Filesystem on an AIX server and another one on a Red Hat box. I have syncd the two filesystems using rsysnc. What Im looking for is a script that would compare to the two filesystems to make sure the bits match up and the number of files match up. its around 2.8 million... (5 Replies)
Discussion started by: zippdawg2001
5 Replies

4. Shell Programming and Scripting

Compare files in two folders and delete missing ones

I do not know much about shell scripting so I am at a loss here. If someone can help me, that would be great! I have two directories /dir1 /dir2 I need to delete all files from /dir1 and that does not have a correspondent file in /dir2. It should NOT check file suffixes in /dir2 . Why?... (20 Replies)
Discussion started by: kaah
20 Replies

5. Shell Programming and Scripting

Shell Script to delete files within a particular time frame under multiple sub folders

Greetings! I'm looking for starting information for a shell script. Here's my scenario: I have multiple folders(100) for example: /www/test/applications/app1/logs /www/test/applications/app2/logs Within these folders there are log files files that need to be deleted after a month. ... (3 Replies)
Discussion started by: whysolucky
3 Replies

6. Shell Programming and Scripting

Need to delete large set of files (i.e) close to 100K from a directory based on the input file

Hi all, I need a script to delete a large set of files from a directory under / based on an input file and want to redirect errors into separate file. I have already prepared a list of files in the input file. Kndly help me. Thanks, Prash (36 Replies)
Discussion started by: prash358
36 Replies

7. Shell Programming and Scripting

Linux Script to compare two folders and copy missing files

Hi, I need help in shell scripting. If someone can help me, that would be great! Problem. I want Linux Script to compare two folders and copy missing files. Description. I have two directories /dir1 /dir2 I need to copy all distinct/new/unique/missing files from /dir1 and that... (1 Reply)
Discussion started by: S.Praveen Kumar
1 Replies

8. Shell Programming and Scripting

Script to delete folders and files from a prompt

Hi Everyone, I work for GE Money IVR as a DB analyst and the environment on which I work is Solaris 5.0 server and Oracle 11g. I got a project in which I have to clean up the folders and files which are not used in DB. I copied an existing script and edited it, dont know this is the... (5 Replies)
Discussion started by: habeeb506
5 Replies

9. UNIX for Advanced & Expert Users

Help with creating script to delete log files/folders

Hi I am new to Linux / scripting language. I need to improve our Linux servers at work and looking to claim some space my deleting log files/ folders on a 5 day basis. Can someone help me with creating a script to do so. Any sample script will be helpful.:b: Regards (2 Replies)
Discussion started by: sachinksl
2 Replies

10. Shell Programming and Scripting

Script to compare partial filenames in two folders and delete duplicates

Background: I use a TV tuner card to capture OTA video files (.mpeg) and then my Plex Media Server automatically optimizes the files (transcodes for better playback) and places them in a new directory. I have another Plex Library pointing to the new location for the optimized .mp4 files. This... (2 Replies)
Discussion started by: shaky
2 Replies
DH_MOVEFILES(1) 						     Debhelper							   DH_MOVEFILES(1)

NAME
dh_movefiles - move files out of debian/tmp into subpackages SYNOPSIS
dh_movefiles [debhelperoptions] [--sourcedir=dir] [-Xitem] [file...] DESCRIPTION
dh_movefiles is a debhelper program that is responsible for moving files out of debian/tmp or some other directory and into other package build directories. This may be useful if your package has a Makefile that installs everything into debian/tmp, and you need to break that up into subpackages. Note: dh_install is a much better program, and you are recommended to use it instead of dh_movefiles. FILES
debian/package.files Lists the files to be moved into a package, separated by whitespace. The filenames listed should be relative to debian/tmp/. You can also list directory names, and the whole directory will be moved. OPTIONS
--sourcedir=dir Instead of moving files out of debian/tmp (the default), this option makes it move files out of some other directory. Since the entire contents of the sourcedir is moved, specifying something like --sourcedir=/ is very unsafe, so to prevent mistakes, the sourcedir must be a relative filename; it cannot begin with a `/'. -Xitem, --exclude=item Exclude files that contain item anywhere in their filename from being installed. file ... Lists files to move. The filenames listed should be relative to debian/tmp/. You can also list directory names, and the whole directory will be moved. It is an error to list files here unless you use -p, -i, or -a to tell dh_movefiles which subpackage to put them in. NOTES
Note that files are always moved out of debian/tmp by default (even if you have instructed debhelper to use a compatibility level higher than one, which does not otherwise use debian/tmp for anything at all). The idea behind this is that the package that is being built can be told to install into debian/tmp, and then files can be moved by dh_movefiles from that directory. Any files or directories that remain are ignored, and get deleted by dh_clean later. SEE ALSO
debhelper(7) This program is a part of debhelper. AUTHOR
Joey Hess <joeyh@debian.org> 11.1.6ubuntu2 2018-05-10 DH_MOVEFILES(1)
All times are GMT -4. The time now is 07:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy