Checking if two files have the same content not working

Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
So I have a script that iterates through all the files in a directory and checks whether the files contain the same content or not with any other files in the same directory:
Check()
if [ -e "new.txt" ]
then
rm new.txt
fi
for item in "$2"/*
do
if [ ! -d "$item" ]
then
diff "$1" "$item">new.txt
if [ -s "new.txt" ]
then
echo "$1 $item"
echo "Yes"
fi
fi
done
Iterate()
for item in "$2"/*
do
if [ ! -d "$item" ]
then
Check $item $2
fi
done
Iterate $1 $2
And the bash
bash script.sh asd /home/ljuben
However when I run the script it always echoes "Yes" eve if the files don't contain the same content.
And ideas?
bash shell-script
add a comment |Â
up vote
-1
down vote
favorite
So I have a script that iterates through all the files in a directory and checks whether the files contain the same content or not with any other files in the same directory:
Check()
if [ -e "new.txt" ]
then
rm new.txt
fi
for item in "$2"/*
do
if [ ! -d "$item" ]
then
diff "$1" "$item">new.txt
if [ -s "new.txt" ]
then
echo "$1 $item"
echo "Yes"
fi
fi
done
Iterate()
for item in "$2"/*
do
if [ ! -d "$item" ]
then
Check $item $2
fi
done
Iterate $1 $2
And the bash
bash script.sh asd /home/ljuben
However when I run the script it always echoes "Yes" eve if the files don't contain the same content.
And ideas?
bash shell-script
2
@Jesse_b I don't think that's quite true; unless the-soption is given,diffwill produce an empty file if the files are the same: however[ -s "new.txt" ]will return true ifnew.txtexists and is not empty (inverting the OP's intended logic)
â steeldriver
Jun 15 at 15:34
Regardless, as @Jesse_b and @Kusalananda have pointed out, it would be simpler and better practice to check the exit status of your comparison command rather than creating and testing a file: e.g.if ! cmp -s "$1" "$item"; then echo "Different"; fi
â steeldriver
Jun 15 at 15:51
What isasdon your command line and why don't you use that argument in your script?
â Kusalananda
Jun 15 at 16:18
It's nothing. I started it because I thought it must be made with two arguments but then I realized that I can make it with one
â David Mathers
Jun 15 at 16:22
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
So I have a script that iterates through all the files in a directory and checks whether the files contain the same content or not with any other files in the same directory:
Check()
if [ -e "new.txt" ]
then
rm new.txt
fi
for item in "$2"/*
do
if [ ! -d "$item" ]
then
diff "$1" "$item">new.txt
if [ -s "new.txt" ]
then
echo "$1 $item"
echo "Yes"
fi
fi
done
Iterate()
for item in "$2"/*
do
if [ ! -d "$item" ]
then
Check $item $2
fi
done
Iterate $1 $2
And the bash
bash script.sh asd /home/ljuben
However when I run the script it always echoes "Yes" eve if the files don't contain the same content.
And ideas?
bash shell-script
So I have a script that iterates through all the files in a directory and checks whether the files contain the same content or not with any other files in the same directory:
Check()
if [ -e "new.txt" ]
then
rm new.txt
fi
for item in "$2"/*
do
if [ ! -d "$item" ]
then
diff "$1" "$item">new.txt
if [ -s "new.txt" ]
then
echo "$1 $item"
echo "Yes"
fi
fi
done
Iterate()
for item in "$2"/*
do
if [ ! -d "$item" ]
then
Check $item $2
fi
done
Iterate $1 $2
And the bash
bash script.sh asd /home/ljuben
However when I run the script it always echoes "Yes" eve if the files don't contain the same content.
And ideas?
bash shell-script
edited Jun 15 at 15:16
Jesse_b
10.2k22658
10.2k22658
asked Jun 15 at 15:03
David Mathers
333
333
2
@Jesse_b I don't think that's quite true; unless the-soption is given,diffwill produce an empty file if the files are the same: however[ -s "new.txt" ]will return true ifnew.txtexists and is not empty (inverting the OP's intended logic)
â steeldriver
Jun 15 at 15:34
Regardless, as @Jesse_b and @Kusalananda have pointed out, it would be simpler and better practice to check the exit status of your comparison command rather than creating and testing a file: e.g.if ! cmp -s "$1" "$item"; then echo "Different"; fi
â steeldriver
Jun 15 at 15:51
What isasdon your command line and why don't you use that argument in your script?
â Kusalananda
Jun 15 at 16:18
It's nothing. I started it because I thought it must be made with two arguments but then I realized that I can make it with one
â David Mathers
Jun 15 at 16:22
add a comment |Â
2
@Jesse_b I don't think that's quite true; unless the-soption is given,diffwill produce an empty file if the files are the same: however[ -s "new.txt" ]will return true ifnew.txtexists and is not empty (inverting the OP's intended logic)
â steeldriver
Jun 15 at 15:34
Regardless, as @Jesse_b and @Kusalananda have pointed out, it would be simpler and better practice to check the exit status of your comparison command rather than creating and testing a file: e.g.if ! cmp -s "$1" "$item"; then echo "Different"; fi
â steeldriver
Jun 15 at 15:51
What isasdon your command line and why don't you use that argument in your script?
â Kusalananda
Jun 15 at 16:18
It's nothing. I started it because I thought it must be made with two arguments but then I realized that I can make it with one
â David Mathers
Jun 15 at 16:22
2
2
@Jesse_b I don't think that's quite true; unless the
-s option is given, diff will produce an empty file if the files are the same: however [ -s "new.txt" ] will return true if new.txt exists and is not empty (inverting the OP's intended logic)â steeldriver
Jun 15 at 15:34
@Jesse_b I don't think that's quite true; unless the
-s option is given, diff will produce an empty file if the files are the same: however [ -s "new.txt" ] will return true if new.txt exists and is not empty (inverting the OP's intended logic)â steeldriver
Jun 15 at 15:34
Regardless, as @Jesse_b and @Kusalananda have pointed out, it would be simpler and better practice to check the exit status of your comparison command rather than creating and testing a file: e.g.
if ! cmp -s "$1" "$item"; then echo "Different"; fiâ steeldriver
Jun 15 at 15:51
Regardless, as @Jesse_b and @Kusalananda have pointed out, it would be simpler and better practice to check the exit status of your comparison command rather than creating and testing a file: e.g.
if ! cmp -s "$1" "$item"; then echo "Different"; fiâ steeldriver
Jun 15 at 15:51
What is
asd on your command line and why don't you use that argument in your script?â Kusalananda
Jun 15 at 16:18
What is
asd on your command line and why don't you use that argument in your script?â Kusalananda
Jun 15 at 16:18
It's nothing. I started it because I thought it must be made with two arguments but then I realized that I can make it with one
â David Mathers
Jun 15 at 16:22
It's nothing. I started it because I thought it must be made with two arguments but then I realized that I can make it with one
â David Mathers
Jun 15 at 16:22
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
Your script seems to not use its first argument. It is passed to the Iterate function, and then never seen again.
But the real issue is that you run diff on every combination of two files, and then look at the size of the diff. The size of the diff will be non-zero for files that are different. Your script is therefore reporting Yes for every combination of files that are different, not the same.
You also needlessly run the diff between file A and B twice (A vs. B and then later B vs. A). You can get by this by generating the file list only once, and then iterating over that.
Alternative script:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directoryn' "$0" >&2
exit 1
fi
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# skip over non-regular files
if [ ! -f "$1" ]; then
shift
continue
fi
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same contentn' "$item" "$name"
fi
done
done
You may still have your two functions if you wish:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directoryn' "$0" >&2
exit 1
fi
check ()
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same contentn' "$item" "$name"
fi
done
iterate ()
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# only process regular files
if [ -f "$1" ]; then
check "$@" # checks the first item against the rest
fi
shift # get rid of the first item
done
iterate "$1"
Notice how we don't let the check function generate its own file list. Instead we pass the list of files to it.
For the lazy:
fdupes -1 /some/directory
add a comment |Â
up vote
-1
down vote
You want to use diff with the -s option:
-s, --report-identical-files
report when two files are the same
You also don't need to create a file with the output, you can just test on the exit of the diff command:
Check()
if [ -e "new.txt" ]
then
rm new.txt
fi
for item in "$2"/*
do
if [ ! -d "$item" ]
then
if diff -s "$1" "$item"
then
echo "$1 $item"
echo "Yes"
fi
fi
done
As Kusalananda pointed out cmp is probably a better option and is more portable. You could use:
if cmp -s "$1" "$item"
then
echo "$1 $item"
echo "Yes"
fi
2
See alsocmp -swhich is more portable.
â Kusalananda
Jun 15 at 15:23
Nope it still doesn't work
â David Mathers
Jun 15 at 15:40
1
"Still doesn't work" doesn't help much. What about it doesn't work?
â Jesse_b
Jun 15 at 15:41
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Your script seems to not use its first argument. It is passed to the Iterate function, and then never seen again.
But the real issue is that you run diff on every combination of two files, and then look at the size of the diff. The size of the diff will be non-zero for files that are different. Your script is therefore reporting Yes for every combination of files that are different, not the same.
You also needlessly run the diff between file A and B twice (A vs. B and then later B vs. A). You can get by this by generating the file list only once, and then iterating over that.
Alternative script:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directoryn' "$0" >&2
exit 1
fi
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# skip over non-regular files
if [ ! -f "$1" ]; then
shift
continue
fi
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same contentn' "$item" "$name"
fi
done
done
You may still have your two functions if you wish:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directoryn' "$0" >&2
exit 1
fi
check ()
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same contentn' "$item" "$name"
fi
done
iterate ()
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# only process regular files
if [ -f "$1" ]; then
check "$@" # checks the first item against the rest
fi
shift # get rid of the first item
done
iterate "$1"
Notice how we don't let the check function generate its own file list. Instead we pass the list of files to it.
For the lazy:
fdupes -1 /some/directory
add a comment |Â
up vote
2
down vote
Your script seems to not use its first argument. It is passed to the Iterate function, and then never seen again.
But the real issue is that you run diff on every combination of two files, and then look at the size of the diff. The size of the diff will be non-zero for files that are different. Your script is therefore reporting Yes for every combination of files that are different, not the same.
You also needlessly run the diff between file A and B twice (A vs. B and then later B vs. A). You can get by this by generating the file list only once, and then iterating over that.
Alternative script:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directoryn' "$0" >&2
exit 1
fi
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# skip over non-regular files
if [ ! -f "$1" ]; then
shift
continue
fi
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same contentn' "$item" "$name"
fi
done
done
You may still have your two functions if you wish:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directoryn' "$0" >&2
exit 1
fi
check ()
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same contentn' "$item" "$name"
fi
done
iterate ()
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# only process regular files
if [ -f "$1" ]; then
check "$@" # checks the first item against the rest
fi
shift # get rid of the first item
done
iterate "$1"
Notice how we don't let the check function generate its own file list. Instead we pass the list of files to it.
For the lazy:
fdupes -1 /some/directory
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Your script seems to not use its first argument. It is passed to the Iterate function, and then never seen again.
But the real issue is that you run diff on every combination of two files, and then look at the size of the diff. The size of the diff will be non-zero for files that are different. Your script is therefore reporting Yes for every combination of files that are different, not the same.
You also needlessly run the diff between file A and B twice (A vs. B and then later B vs. A). You can get by this by generating the file list only once, and then iterating over that.
Alternative script:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directoryn' "$0" >&2
exit 1
fi
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# skip over non-regular files
if [ ! -f "$1" ]; then
shift
continue
fi
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same contentn' "$item" "$name"
fi
done
done
You may still have your two functions if you wish:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directoryn' "$0" >&2
exit 1
fi
check ()
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same contentn' "$item" "$name"
fi
done
iterate ()
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# only process regular files
if [ -f "$1" ]; then
check "$@" # checks the first item against the rest
fi
shift # get rid of the first item
done
iterate "$1"
Notice how we don't let the check function generate its own file list. Instead we pass the list of files to it.
For the lazy:
fdupes -1 /some/directory
Your script seems to not use its first argument. It is passed to the Iterate function, and then never seen again.
But the real issue is that you run diff on every combination of two files, and then look at the size of the diff. The size of the diff will be non-zero for files that are different. Your script is therefore reporting Yes for every combination of files that are different, not the same.
You also needlessly run the diff between file A and B twice (A vs. B and then later B vs. A). You can get by this by generating the file list only once, and then iterating over that.
Alternative script:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directoryn' "$0" >&2
exit 1
fi
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# skip over non-regular files
if [ ! -f "$1" ]; then
shift
continue
fi
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same contentn' "$item" "$name"
fi
done
done
You may still have your two functions if you wish:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directoryn' "$0" >&2
exit 1
fi
check ()
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same contentn' "$item" "$name"
fi
done
iterate ()
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# only process regular files
if [ -f "$1" ]; then
check "$@" # checks the first item against the rest
fi
shift # get rid of the first item
done
iterate "$1"
Notice how we don't let the check function generate its own file list. Instead we pass the list of files to it.
For the lazy:
fdupes -1 /some/directory
edited Jun 15 at 17:15
answered Jun 15 at 16:40
Kusalananda
101k13199312
101k13199312
add a comment |Â
add a comment |Â
up vote
-1
down vote
You want to use diff with the -s option:
-s, --report-identical-files
report when two files are the same
You also don't need to create a file with the output, you can just test on the exit of the diff command:
Check()
if [ -e "new.txt" ]
then
rm new.txt
fi
for item in "$2"/*
do
if [ ! -d "$item" ]
then
if diff -s "$1" "$item"
then
echo "$1 $item"
echo "Yes"
fi
fi
done
As Kusalananda pointed out cmp is probably a better option and is more portable. You could use:
if cmp -s "$1" "$item"
then
echo "$1 $item"
echo "Yes"
fi
2
See alsocmp -swhich is more portable.
â Kusalananda
Jun 15 at 15:23
Nope it still doesn't work
â David Mathers
Jun 15 at 15:40
1
"Still doesn't work" doesn't help much. What about it doesn't work?
â Jesse_b
Jun 15 at 15:41
add a comment |Â
up vote
-1
down vote
You want to use diff with the -s option:
-s, --report-identical-files
report when two files are the same
You also don't need to create a file with the output, you can just test on the exit of the diff command:
Check()
if [ -e "new.txt" ]
then
rm new.txt
fi
for item in "$2"/*
do
if [ ! -d "$item" ]
then
if diff -s "$1" "$item"
then
echo "$1 $item"
echo "Yes"
fi
fi
done
As Kusalananda pointed out cmp is probably a better option and is more portable. You could use:
if cmp -s "$1" "$item"
then
echo "$1 $item"
echo "Yes"
fi
2
See alsocmp -swhich is more portable.
â Kusalananda
Jun 15 at 15:23
Nope it still doesn't work
â David Mathers
Jun 15 at 15:40
1
"Still doesn't work" doesn't help much. What about it doesn't work?
â Jesse_b
Jun 15 at 15:41
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
You want to use diff with the -s option:
-s, --report-identical-files
report when two files are the same
You also don't need to create a file with the output, you can just test on the exit of the diff command:
Check()
if [ -e "new.txt" ]
then
rm new.txt
fi
for item in "$2"/*
do
if [ ! -d "$item" ]
then
if diff -s "$1" "$item"
then
echo "$1 $item"
echo "Yes"
fi
fi
done
As Kusalananda pointed out cmp is probably a better option and is more portable. You could use:
if cmp -s "$1" "$item"
then
echo "$1 $item"
echo "Yes"
fi
You want to use diff with the -s option:
-s, --report-identical-files
report when two files are the same
You also don't need to create a file with the output, you can just test on the exit of the diff command:
Check()
if [ -e "new.txt" ]
then
rm new.txt
fi
for item in "$2"/*
do
if [ ! -d "$item" ]
then
if diff -s "$1" "$item"
then
echo "$1 $item"
echo "Yes"
fi
fi
done
As Kusalananda pointed out cmp is probably a better option and is more portable. You could use:
if cmp -s "$1" "$item"
then
echo "$1 $item"
echo "Yes"
fi
edited Jun 15 at 15:36
answered Jun 15 at 15:15
Jesse_b
10.2k22658
10.2k22658
2
See alsocmp -swhich is more portable.
â Kusalananda
Jun 15 at 15:23
Nope it still doesn't work
â David Mathers
Jun 15 at 15:40
1
"Still doesn't work" doesn't help much. What about it doesn't work?
â Jesse_b
Jun 15 at 15:41
add a comment |Â
2
See alsocmp -swhich is more portable.
â Kusalananda
Jun 15 at 15:23
Nope it still doesn't work
â David Mathers
Jun 15 at 15:40
1
"Still doesn't work" doesn't help much. What about it doesn't work?
â Jesse_b
Jun 15 at 15:41
2
2
See also
cmp -s which is more portable.â Kusalananda
Jun 15 at 15:23
See also
cmp -s which is more portable.â Kusalananda
Jun 15 at 15:23
Nope it still doesn't work
â David Mathers
Jun 15 at 15:40
Nope it still doesn't work
â David Mathers
Jun 15 at 15:40
1
1
"Still doesn't work" doesn't help much. What about it doesn't work?
â Jesse_b
Jun 15 at 15:41
"Still doesn't work" doesn't help much. What about it doesn't work?
â Jesse_b
Jun 15 at 15:41
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f450019%2fchecking-if-two-files-have-the-same-content-not-working%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
@Jesse_b I don't think that's quite true; unless the
-soption is given,diffwill produce an empty file if the files are the same: however[ -s "new.txt" ]will return true ifnew.txtexists and is not empty (inverting the OP's intended logic)â steeldriver
Jun 15 at 15:34
Regardless, as @Jesse_b and @Kusalananda have pointed out, it would be simpler and better practice to check the exit status of your comparison command rather than creating and testing a file: e.g.
if ! cmp -s "$1" "$item"; then echo "Different"; fiâ steeldriver
Jun 15 at 15:51
What is
asdon your command line and why don't you use that argument in your script?â Kusalananda
Jun 15 at 16:18
It's nothing. I started it because I thought it must be made with two arguments but then I realized that I can make it with one
â David Mathers
Jun 15 at 16:22