Removing files based on name and content


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Removing files based on name and content
# 8  
Old 01-10-2012
sorry.. i quickly created a directory structure in my machine.. so i put the folder name as wrong..

Also, when you pass the directory name as argument to your script, make sure you pass the absolute path

change it to

Code:
 
../b/$i ---> a2/$i

# 9  
Old 01-10-2012
BASH

Hi,

I think this is what you are looking for,

Code:
#! /usr/bin/bash
 
dir1=$1;
dir2=$2;
base=$3;
 
if [ $# != 3 ]
then
echo "Script Usage: $0 dir1 dir2 basedir";
exit;
fi
for file1 in `ls $dir1`
do
   for file2 in `ls $dir2`
   do
      if [ "$file1" = "$file2" ]
      then
        `\rm -rf "$base/$file1"`;
         echo "$base/$file1";
      fi
      if [ -s "$dir1/$file1" -a -s "$dir2/$file2" ]
      then
         a=`diff "$dir1/$file1" "$dir2/$file2" | wc -l`;
         if [ $a = 0 ]
         then
            `\rm -rf "$base/$file1"`;
         fi
      fi
   done
done

Cheers,
RangaSmilie
# 10  
Old 01-10-2012
@rangarsan
Sorry, but your script is really dangerous and contains serious design errors and basic syntax mistakes.
Strongly advise that the O/P ignore this post.
# 11  
Old 01-10-2012
Quote:
Originally Posted by methyl
@rangarsan
Sorry, but your script is really dangerous and contains serious design errors and basic syntax mistakes.
Strongly advise that the O/P ignore this post.
I have written this code in sun solaries with bash and executed it. Its working fine in that. what is the syntax error do you found and you mean dangerous code. may i know what it would be? This is the requirement which every one found in the first post. please revert if you have any quries.

Cheers,
RangaSmilie
# 12  
Old 01-10-2012
@rangarasan

Danger:
The main issue is that the script will issue "rm -rf" on any unique directory which your "ls" finds. You should not have the "-r" parameter in this script.
Even if it is a file, the script deletes the files from the wrong directory.
Part of this is pandeesh's really vague specification of the third parameter. I actually believe that he meant to "move" not "remove" the files to the third directory.

Design:
Quote:
for file1 in `ls $dir1`
for file2 in `ls $dir2`
Any names containing space characters will get split into partial names.
itkamaraj's code does not have this problem, but it does have other problems.
The script repeatedly searches $dir2 for matching names one-by-one (inefficient and avoidable).
There is no check that what is output by "ls" is actually a file. It could be a directory in which case we have a disaster.

Syntax:
Quote:
`\rm -rf "$base/$file1"`;
This is unix not Oracle. There is never a reason to end a line in Bourne-type script with a semi-colon. You have many lines like that.
Never use backticks in a standalone line. It will not always behave.
Presumably the "\rm" is to avoid an alias for "rm" on your system.
That single line contains more errors than I have seen in a single line for some time.



Forgot to mention that pandeesh should probably have been looking at the unix "dircmp" command.

Last edited by methyl; 01-10-2012 at 02:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split content based on keywords

I need to split the file contents with multiple rows based on patterns Sample: Input: ABC101testXYZ102UKMNO1092testing ABC999testKMNValid Output: ABC101test XYZ102U KMN1092testing ABC999test KMNValid In this ABC , XYZ and KMN are patterns (6 Replies)
Discussion started by: Jairaj
6 Replies

2. Shell Programming and Scripting

Split content based on keywords

I need to split the file contents with multiple rows based on patterns Sample: Input: ABC101testXYZ102UKMNO1092testing ABC999testKMNValid Output: ABC101test XYZ102U KMN1092testing ABC999test KMNValid In this ABC , XYZ and KMN are patterns Continue here./mod] Please read forum... (1 Reply)
Discussion started by: Jairaj
1 Replies

3. Shell Programming and Scripting

Move multiple files 4rm Source to different target folders based on a series num in the file content

Dear Experts my scenario is as follows... I have one source folder "Source" and 2 target folders "Target_123456" & "Target_789101". I have 2 series of files. 123456 series and 789101 series. Each series has got 3 types of fiels "Debit", "Refund", "Claims". All files are getting... (17 Replies)
Discussion started by: phani333
17 Replies

4. Shell Programming and Scripting

List the files after sorting based on file content

Hi, I have two pipe separated files as below: head -3 file1.txt "HD"|"Nov 11 2016 4:08AM"|"0000000018" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" head -3 file2.txt "HD"|"Nov 15 2016 2:18AM"|"0000000019" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" I want to list the... (6 Replies)
Discussion started by: Prasannag87
6 Replies

5. Shell Programming and Scripting

Split a file in more files based on score content

Dear All, I have the following file tabulated: ID distanceTSS score 8434 571269 10 10122 393912 9 7652 6 10 4863 1451 9 8419 39 2 9363 564 21 9333 7714 22 9638 8334 9 1638 1231 11 10701 918 1000 6587 32056 111 What I would like to do is the following, create 100 new files based... (5 Replies)
Discussion started by: paolo.kunder
5 Replies

6. Shell Programming and Scripting

Split the file based on the content

Arun kumar something somehting Enterting in to the line . . . . Some text text Finshing the sentence Some other text . . . . Again something somehting Enterting in to the line . . . . . . Again text text Finshing the sentence (6 Replies)
Discussion started by: arukuku
6 Replies

7. Shell Programming and Scripting

multiplication of two files based on the content of the first column

Hi, This is something that probably it is more difficult to explain than to do. I have two files e.g. FILE1 A15 8.3102E+00 3.2000E-04 A15 8.5688E+00 4.3000E-05 B13 5.1100E-01 1.9960E+00 B16 5.1100E-01 2.3000E-03 B16 8.6770E-01 1.0000E-07 B16 9.8693E-01 3.4000E-05... (4 Replies)
Discussion started by: f_o_555
4 Replies

8. UNIX for Dummies Questions & Answers

Removing lines that are (same in content) based on columns

I have a file which looks like AA BB CC DD EE FF GG HH KK AA BB GG HH KK FF CC DD EE AA BB CC DD EE UU VV XX ZZ AA BB VV XX ZZ UU CC DD EE .... I want the script to give me only one line based on duplicate contents: AA BB CC DD EE FF GG HH KK AA BB CC DD EE UU VV XX ZZ (7 Replies)
Discussion started by: adsforall
7 Replies

9. Shell Programming and Scripting

Searching and Removing File Content

Hi, I am trying to search a character in a file and remove it from that file.... My file looks something like this: test1.txt ckj12300_00|123|var1|10.2 ckj00200_12|444|var2|11.2 ckj00200_14|4556|var3|33.5 c00200_00_000|4558|var4|33.5 ckj00200_14|4553|var5|33.5... (7 Replies)
Discussion started by: rkumar28
7 Replies
Login or Register to Ask a Question