LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-22-2013, 02:44 PM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
How to search for list of files with same names as untar'd file and do a diff?


Ok I need to do an extract of a tar.gz to a temp dir
then I need to get these file names and see if the same files are in another directory...

Where should I start - I got as far as unzipping to a temp dir...what now?

How do I create an array of these filenames and do a find on the other directory and then do a diff to see if they are equal?

Help please.

Code:
echo "COMPARE files from TAR"
                          echo "echo $file1........."
                         echo " echo $file2........."
                           echo " echo $file3......."
                             echo "compare file "$2" with file in directory"
                             echo "make a temp dir if one exists check is empty"
                             echo "if not empty script fails."
                             echo "Restore "$2" to temp directory "
                             echo "Do a find of filename in source directory"
                             echo "IF file no-exists echo out FILE NO-EXISTS"
                             echo "IF file exists compare content and print size"
                             echo "IF identical print identical"
                             echo "IF not print different content, current size, archived size"
    if [[ ! -d "$RESTORE_COMP_DIRECTORY" ]]; then
           mkdir -p "$RESTORE_COMP_DIRECTORY"
    fi
    if [[ "$(ls -A $RESTORE_COMP_DIRECTORY)" ]]; then
    echo "Dir "$RESTORE_COMP_DIRECTORY" is not empty exiting"
    exit
    else
 
Old 03-22-2013, 02:57 PM   #2
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 215

Rep: Reputation: 66
Something simple..

cd /path/to/temp/dir
for i in `ls`;do find / -name $i 2>/dev/null; done
 
Old 03-22-2013, 02:59 PM   #3
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 215

Rep: Reputation: 66
Sorry, maybe I should explain...

CHANGE DIRECTORIES TO UNTAR TEMP DIR:
cd /path/to/temp/dir

GRAB THE NAME TO EACH FILE IN DIR, AND SEARCH FOR FILE OF SAME NAME STARTING AT THE ROOT OF THE FILESYSTEM. 2>/dev/null will surepess errors (Permission denied), so that is optional.
for i in `ls`;do find / -name $i 2>/dev/null; done
 
Old 03-23-2013, 03:53 AM   #4
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by savona View Post
Sorry, maybe I should explain...

CHANGE DIRECTORIES TO UNTAR TEMP DIR:
cd /path/to/temp/dir

GRAB THE NAME TO EACH FILE IN DIR, AND SEARCH FOR FILE OF SAME NAME STARTING AT THE ROOT OF THE FILESYSTEM. 2>/dev/null will surepess errors (Permission denied), so that is optional.
for i in `ls`;do find / -name $i 2>/dev/null; done
That is great so then how would I do the following properly - copy the find output to new tmp file, and compare the files in each directory

mkdir $TMP_COMP_DIRECTORY
cd $RESTORE_COMP_DIRECTORY
for i in `ls`;do
NAME=${find / -name $i}
cp $NAME $TMP_COMP_DIRECTORY


diff --brief <(sort $TMP_COMP_DIRECTORY) <(sort $RESTORE_COMP_DIRECTORY) >/dev/null
comp_value=$?

if [ $comp_value -eq 1 ]
then
echo "do something because they're different"
else
echo "do something because they're identical"
fi

I am going a little dizzy looking at the below -

$ ArrayDIFF=()
$ for i in "${ArrayFILE1[@]}"; do
> skip=
> for j in "${ArrayFILE2[@]}"; do
> [[ $i == $j ]] && { skip=1; break; }
> done
> [[ -n $skip ]] || ArrayDIFF+=("$i")
> done
$ declare -p ArrayDIFF
#
 
Old 03-23-2013, 09:45 AM   #5
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 215

Rep: Reputation: 66
So let's go back to the beginning because I think this is getting confusing.

You want to:

1) untar an archive to a temp directory.
2) Check the rest of the filesystem to see if any of the files in the tar archive are duplicate names of any files anywhere else on the system
3) Check to see if the files are the same, not just named the same.

Here is the script, I just created a quick test on my machine and it worked fine.

Code:
#!/bin/bash
#UNTAR file to temp dir
tar xvf tarfile.tar /var/tmp/test/

#Change to temp dir
cd /var/tmp/test/

# See if any of the files are duplicate names
for i in `ls`; do 
	f=`find / -name $i 2>/dev/null`
		if [ -z $f ]; then
			continue	
		else
                        echo "Found duplicate for $i"
			echo "diff output for $i and $f"
			diff $i $f
		fi
done
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Create populated files with unique file names from a list geokker Linux - Server 6 05-16-2012 03:50 PM
[SOLVED] untar a list of tar files? volt220 Linux - General 1 01-05-2011 04:43 PM
Need advice on a script to search many files for list of terms, append hits to list jimmy the saint Programming 1 07-11-2010 03:59 AM
How to search for missing files and pass their names on to another shell script djslothario Linux - Newbie 3 08-07-2009 12:59 AM
untar a list of tar files? Rotwang Linux - General 6 10-10-2006 01:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration