Sponsored Content
Top Forums Shell Programming and Scripting Help with file compare and move script Post 303026442 by Don Cragun on Wednesday 28th of November 2018 01:52:57 AM
Old 11-28-2018
Quote:
Originally Posted by Chubler_XL
This demo program might help you to your solution:
Code:
dir1="holiday.jpg camping.jpg beach.jpg"
dir2="xmas.jpg holiday.jpg"

for FN in $dir1
do
    found=0
    for FO in $dir2
    do
        if [ $FN = $FO ]
        then
            found=1
        fi
    done

    if [ $found -eq 1 ]
    then
        echo "Duplicate $FN"
    else
        echo "Unique $FN"
    fi
done

Output:
Code:
Duplicate holiday.jpg
Unique camping.jpg
Unique beach.jpg

Hi Chubler_XL,
Your example works fine for the example you've chosen, but, as mattz40 mentioned in post #4 in this thread, it won't work if one or more of the filenames in the lists in the expansions of $dir1 and $dir2 contain a <space> character.

Hi mattz40,
Maybe you would want to try something more like:
Code:
#!/bin/bash
# Compare file names in source and target directories
# Move duplicates from source to duplicates directory (not yet implemented)
# Move remaining files in source to target directory (not yet implemented)
# Only care about filenames; not checksum, date, time

dir1="/mnt/nas/source"
dir2="/mnt/nas/target"
dir3="/mnt/nas/Duplicates"

for Path1 in "$dir1"/*.*
do	File1="${Path1##*/}"
	found=0
	for Path2 in "$dir2"/*.*
	do	if [ "$File1" = "${Path2##*/}" ]
		then	found=1
			break
		fi
	done
	if [ $found -eq 1 ]
	then	echo "Duplicate: \"$File1\""
	else	echo "Unique: \"$File1\""
	fi
done

Note that the comment line in your sample code:
Code:
# Only care about files names, not upper lower case, checksum, date, time

has been modified because this code does care about case in filenames. You can modify the code if you want to allow case-insensitive matches, but that is not the way normal UNIX/Linux/BSD filesystems work.

If the directory /mnt/nas/source contains the files:
Code:
birds and bees.jpg
flowers.jpg
hive.jpg

and the directory /mnt/nas/target contains the files:
Code:
beehive.jpg
birds and bees.jpg
flowers.jpg

then the above code will produce the output:
Code:
Duplicate: "birds and bees.jpg"
Duplicate: "flowers.jpg"
Unique: "hive.jpg"

Note also that the dir1 and dir2 variables now contain the full pathnames of the source and target directories; not lists of words contained in filenames in those directories. The Path1 and Path2 variables contain absolute pathnames of a file in the source and target directories, respectively and the File1 variable contains the filename of the last component of the pathname in the expansion of $Path1.

Note that bakunin gave an excellent explanation of what was wrong in your if [ ... ] expression. But I have to disagree with one point. The standard test expression and [ expression ] string equality operator is a single <equals-sign>, not the double <equals-sign> that is used in the C Language. Some shells will accept both, some shells will give you a syntax error is you use the double <equals-sign>, and some manual pages for some shells will say that the single <equals-sign> form is deprecated, but that is not what the shell standards say. I don't know of any shells that do not accept the single <equals-sign> form that is required by the standards.

(Some shells also have a [[ expression ]] in which [[ is a shell keyword; not the name of utility used to evaluate expressions. In the shells that understand [[ expression ]], the string equality operator in expression in this form is the double <equals-sign> in all shells that I've used. And some shells accept a single <equal-sign> operator in this form with a similar, but not always identical, meaning.)

Last edited by Don Cragun; 11-28-2018 at 02:56 AM.. Reason: Fix typo: s,!/bin/bash,#!/bin/bash,
These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Compare & Move between 2 servers

Greetings - I am a newbie in shell scripts. I have been thru the whole forum but there has been no similar query posed. The objective of my system is to have a unified filebase system. I am using RSync to synchronise files between the location & central server with both of them having the... (4 Replies)
Discussion started by: evolve
4 Replies

2. UNIX for Dummies Questions & Answers

Compare directories then move similar ones

I would like to know how to compare a listing of directories that begin with the same four numbers ie. /1234cat /1234tree /1234fish and move all these directories into one directory Thanks in advance (2 Replies)
Discussion started by: tgibson2
2 Replies

3. Shell Programming and Scripting

script to move text in file?

ok i asked around to a few ppl and they said to use sed or awk to do what i want.. but i cant figure out how to use it like that.. anyway i have a text file that is 10k lines long.. i need to move text from the end of a line after the ? and move it to the front of the line then add a | after it.... (3 Replies)
Discussion started by: wckdkl0wn
3 Replies

4. Shell Programming and Scripting

Script to move the first line of a file to the end

I'm rather new to scripting, and despite my attempts at finding/writing a script to do what I need, I have not yet been successful. I have a file named "list.txt" of arbitrary length with contents in the following format: /home/user/Music/file1.mp3 /home/user/Music/file2.mp3... (21 Replies)
Discussion started by: Altay_H
21 Replies

5. Shell Programming and Scripting

script to move two lines to the end of a file

My input file is multiline file and I am writing a script to search for a pattern and move the line with the pattern and the next line to the end of the file. Since I am trying to learn awk, I thought I would try it. My input looks like the following: D #testpoint 1 510.0 D #testpoint2 ... (5 Replies)
Discussion started by: banjo25
5 Replies

6. Shell Programming and Scripting

A script that will move a file to a directory with the same name and then rename that file

Hello all. I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b: I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.:confused: I have all of my music stored in... (7 Replies)
Discussion started by: marcozd
7 Replies

7. Shell Programming and Scripting

Script to compare substrings of multiple filenames and move to different directory

Hi there, I am having trouble with a script I have written, which is designed to search through a directory for a header and payload file, retrieve a string from both filenames, compare this string and if it matches make a backup of the two files then move them to a different directory for... (1 Reply)
Discussion started by: alcurry
1 Replies

8. Shell Programming and Scripting

Shell script to get the latest file from the file list and move

Hi, Anybody help me to write a Shell Script Get the latest file from the file list based on created and then move to the target directory. Tried with the following script: got error. A=$(ls -1dt $(find "cveit/local_ftp/reflash-parts" -type f -daystart -mtime -$dateoffset) | head... (2 Replies)
Discussion started by: saravan_an
2 Replies

9. Shell Programming and Scripting

Move file in to directory- script

Hi In directory /mnt/upload I have about 100 000 files (*.png) that have been created during the last six months. Now I need to move them to right folders. eg: file created on 2014-10-10 move to directory /mnt/upload/20141010 file created on 2014-11-11 move to directory /mnt/upload/20141111... (6 Replies)
Discussion started by: primo102
6 Replies

10. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies
All times are GMT -4. The time now is 02:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy