how to compare contents and mode for 2 linux folders? [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
Given 2 folders in centos : /folder1 and /folder2 and each folder has some files and subfolders inside.
I use beyond compare 3 to compare the contents but don't know how to compare the file mode and owner at the same time .
Thanks for any help!

If it didn't have to be done all at once, you could first diff <(cd /folder1; ls -lR) <(cd /folder2; ls -lR) | grep '^[<>]' to get owner/mode differences, and then diff -r /folder1 /folder2 to get content differences.
If you really want it to be done all at once, you could generate a list for each directory that includes name, owner, mode, and checksum, and compare the two. This will only tell you which files are different, not what the changes in them are, though.
diff \
<(find /folder1 -printf '%P\t%u:%g\t%M' \( \
-type b -exec stat -c '\tb:%t:%T\n' -- '{}' \; -o \
-type c -exec stat -c '\tc:%t:%T\n' -- '{}' \; -o \
-type d -printf '/\n' -o \
-type p -printf '|\n' -o \
-type f -printf '\t' -exec sum -- '{}' \; -o \
-type l -printf '\t-> %l\n' -o \
-type s -printf '=\n' -o \
-printf '\t???\n' \) | sort) \
<(find /folder2 -printf '%P\t%u:%g\t%M' \( \
-type b -exec stat -c '\tb:%t:%T\n' -- '{}' \; -o \
-type c -exec stat -c '\tc:%t:%T\n' -- '{}' \; -o \
-type d -printf '/\n' -o \
-type p -printf '|\n' -o \
-type f -printf '\t' -exec sum -- '{}' \; -o \
-type l -printf '\t-> %l\n' -o \
-type s -printf '=\n' -o \
-printf '\t???\n' \) | sort) | \
grep '^[<>]'

Related

Moving photos and renaming them if they exist?

I'm trying to move photos from directories to one directory with find. It works good:
find /origin/path \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.JPG' -o -iname '*.JPEG' -o -iname '*.PNG' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.GIF' \) -type f -exec mv -nv -t /final/path -- {} +;
How to rename files if they have the same name (but different photos)?
You can use the --backup=t option for mv. This will append an increasing numbered suffix to files whose target already exists.
$ find /tmp/test -type f
/tmp/test/dir2/image.jpg
/tmp/test/dir3/image.jpg
/tmp/test/dir1/image.jpg
/tmp/test/dir4/image.jpg
$ mkdir /tmp/test2
$ find /tmp/test -iname '*.jpg' -print0 | xargs -0 mv -v --backup=t --target-directory=/tmp/test2
‘/tmp/test/dir2/image.jpg’ -> ‘/tmp/test2/image.jpg’
‘/tmp/test/dir3/image.jpg’ -> ‘/tmp/test2/image.jpg’ (backup: ‘/tmp/test2/image.jpg.~1~’)
‘/tmp/test/dir1/image.jpg’ -> ‘/tmp/test2/image.jpg’ (backup: ‘/tmp/test2/image.jpg.~2~’)
‘/tmp/test/dir4/image.jpg’ -> ‘/tmp/test2/image.jpg’ (backup: ‘/tmp/test2/image.jpg.~3~’)
$
sorry i didn't because i'm on windows now, but below script should do it
files_list=$(find /origin/path \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.JPG' -o -iname '*.JPEG' -o -iname '*.PNG' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.GIF' \) -type f)
for file in ${files_list}
do
counter=0
while true
do
if [[ ! -a ${file} ]]
then
mv "${file}" "/final/path/${file}"
break
else
file="${file}${counter}"
(( counter++ ))
fi
done
done

Exclude a subpath in find command [duplicate]

This question already has answers here:
How do I exclude a directory when using `find`?
(46 answers)
Closed 7 years ago.
I have the following find command:
find /mnt/F_11 -type f \( -iname '*.xls' -o -iname '*.xlsx' /)
How would I find all items in /mnt/F_11 but not in /mnt/f_11/DONOTENTER/?
In other words, I would want it to search:
YES /mnt/F_11
YES /mnt/F_11/somepath/
YES /mnt/F_11/somepath/other/
NO /mtn/F_11/DONOTENTER/
Use -prune to avoid recursing down branches you don't want to follow.
find /mnt/F_11 -name DONOTENTER -prune -o \
-type f \( -iname '*.xls' -o -iname '*.xlsx' \) -print
Note the explicit -print at the end -- this is important, as otherwise the implicit print action covers both branches.

Linux find with prune and negation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to grep all files in a directory except for
subdirectories of lib
images (png and jpg)
I'm doing it in a shell script, passing the arguments to grep, no problem.
This command excludes the subdirectories of lib
find src \
-name lib -prune -o \
-type f -exec grep -P "$#" {} +
and this one excludes the images
find src \
! -name "*.jpg" ! -name ".png" \
-type f -exec grep -P "$#" {} +
Put together as
find src \
-name lib -prune -o \
! -name "*.jpg" ! -name ".png" \
-type f -exec grep -P "$#" {} +
it fails to exclude the images. Any idea what's going on?
It fails to exclude png images because you left out the * in -name "*.png".
A generally useful approach is to filter results via grep after a pipe, this decreases complexity in the original command, so:
find [simplified find options] | egrep -v ".jpg|.png"

Pass a large variable into the diff command via bash

I am writing a script which does a checksum (md5sum) on a forum web directory.
It is a bash script. With the idea being to do a checksum on all the files in the directory, and then compare it to a text file which has a list of checksums.
The script works if I pass it into a text file, and then do a diff command between the text file and my list of known checksums, but I would like to not have it write to a text file and then have to remove the text file at the end of the script, hence why I am using a variable
The script below fails with the error:
/usr/bin/diff: Argument list too long
cd /var/www/html/forum/
VAR1=$(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "files\|that\|change")
/usr/bin/diff "${VAR1}" "/root/scripts/forum_checkum_original.txt"
How can I pass my variable along so that I can runn the diff command on it?
EDIT: with the help of the user devnull (thank you again) here is the completed and working script:
cd /var/www/html/forum/
MAIL=$(/usr/bin/diff <(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "files\|that\|change") /root/scripts/forum_checkum_original.txt)
if [[ -n $(/usr/bin/diff <(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "files that change") /root/scripts/forum_checkum_original.txt) ]]; then
echo "$MAIL" | mail -s "Forum Checksum" yourmailaddress#yourdomain.com
else
echo "no files have been changed"
fi
diff compares files, not variables. Use Process Substitution instead.
An equivalent of what you're trying to do would be:
/usr/bin/diff <(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "bidorbuy.log") /root/scripts/forum_checkum_original.txt
If you want to keep it in a variable you can give diff the variable as a filedescriptor by doing:
diff <(echo "$MAIL") "/root/scripts/forum_checkum_original.txt"

how to let wc command recursivly? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Let it counting *.h *.cpp in Sub directory.
If you want it seperate per file:
find -type f \( -name "*.h" -o -name "*.cpp" \) -exec wc {} \;
if you want the accumulated sum:
find -type f \( -name "*.h" -o -name "*.cpp" \) -exec cat {} \; | wc -l
bash 4
shopt -s globstar
wc **/*.{cpp,h}
I think find and xargs is clearer and easier to work with instead of find -exec but it's style choice.
find . -name "*.h" -or -name "*.cpp" | xargs wc
Use zsh instead of bash:
wc **/*.(cpp|h)
This will expand out to all the .cpp and .h files in the current directory and all subdirectories.

Resources