Monday, August 29, 2016

Finding same file size in a directory

young@ubuntu-16:~/test$ cat find_same_size.sh
#!/usr/bin/env bash
#set -x
#This is small script can find same size of files.
find_same_size(){

if [[ -z $1 || ! -d $1 ]]
then
 echo "Usage $0 directory_name" ;
 exit $?
else
dir_name=$1;
echo "current directory is $1"



for i in $(find $dir_name -type f); do
  ls -fl $i
done | awk '{f=""
        if(NF>9)for(i=9;i<=NF;i++)f=f?f" "$i:$i; else f=$9;
        if(a[$5]){ a[$5]=a[$5]"\n"f; b[$5]++;} else a[$5]=f} END{for(x in b)print a[x] }' | xargs stat -c "%s  %n" #For just list files
 fi
}




find_same_size $1




#Example usage
young@ubuntu-16:~/test$ bash find_same_size.sh tttt/
current directory is tttt/
26  tttt/iss
26  tttt/issue2
1654  tttt/pass
1654  tttt/passwd

#We can delete duplicated files by using like below.
#First delete line that starts with alphabet. Only find the line that begins with numeric. and
#then with md5sum command if the files are really same or not
young@ubuntu-16:~/test$ bash find_same_size.sh tttt/ | awk '{ if($1 !~ /^([[:alpha:]])+/) print $2}' | xargs md5sum
1e7672cbc2f76c3e9daad0e290e711b9  tttt/iss
1e7672cbc2f76c3e9daad0e290e711b9  tttt/issue2
cc44dec6bfd51d296c89d55e7c38b933  tttt/pass
cc44dec6bfd51d296c89d55e7c38b933  tttt/passwd







#-w32 means 32bytes; -d deletes duplicated ones that created most recently among same md5sums. Finally with xargs we can do rm -vf
young@ubuntu-16:~/test$ bash find_same_size.sh tttt/ | awk '{ if($1 !~ /^([[:alpha:]])+/) print $2}' | xargs md5sum | uniq -w32 -d | xargs rm -vf
removed 'tttt/iss'
removed 'tttt/pass'


Wednesday, August 17, 2016

Bash-sum of each array-Debug on

#!/usr/bin/env bash
#This script is very simple example that sum each array numbers.
#debug on
set -x
var="15899"
echo "first is ${var:0:1}"
echo "second is ${var:1:1}"
echo "third is ${var:2:1}"
echo "fourth is ${var:3:1}"
echo "fifth is ${var:4:1}"

sum=0
start=0
while (( $start < ${#var} ))
do
   sum=$(( $sum+${var:start++:1} ))
done
echo -e "Total sum is \e[;35m $sum \e[;0m"



#Just testing for fun.
young@ubuntu-16:~$ bash sum.sh
+ var=15899
+ echo 'first is 1'
first is 1
+ echo 'second is 5'
second is 5
+ echo 'third is 8'
third is 8
+ echo 'fourth is 9'
fourth is 9
+ echo 'fifth is 9'
fifth is 9
+ sum=0
+ start=0
+ ((  0 < 5  ))
+ sum=1
+ ((  1 < 5  ))
+ sum=6
+ ((  2 < 5  ))
+ sum=14
+ ((  3 < 5  ))
+ sum=23
+ ((  4 < 5  ))
+ sum=32
+ ((  5 < 5  ))
+ echo -e 'Total sum is  \e[;35m 32 \e[;0m'
Total sum is   32
young@ubuntu-16:~$