Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Wednesday, June 19, 2013

Compare the same file in two folders and remove it.

有時在整理照片或文件時, 需要比對2個資料匣, 把重覆的檔案拿掉.

Dwonload Source Here

function usage(){
    echo "Find the same file in two folders and remove it."
    echo "usage : ./comp-rm.sh target-dir source-dir"
    echo "remove the same files in target-dir."
}

if [ $# -ne 2 ]; then
    usage
    exit 1
fi

target_dir=$1
source_dir=$2
f_list1=$(find "$target_dir" -type f)
f_list2=$(find "$source_dir" -type f)


for i in $f_list1; do
    echo $f_list2 | grep $(basename $i) >/dev/null && hit_str+=$i";"
done

if [ -z $hit_str ]; then
    echo "list is empty.."
    exit 0
fi

export IFS=";"
count=0
for hit_file in $hit_str; do
    echo "$hit_file"
    let count++
done

echo "Do you want to remove these files ($count)?"
read -p "Press 'Ctrl+C' stop, 'Enter' key to continue..."

for hit_file in $hit_str; do
    echo "removing... $hit_file"
    rm -f $hit_file
done

exit 0

Thursday, December 30, 2010

symbolic link files

Search files in folder and create symbolic link.

#!/bin/bash

ST1=arm-none-linux-gnueabi-
ST2=arm-linux-
for name_old in `ls | grep $ST1`
do
        name_new=`echo $name_old | sed s/$ST1/$ST2/g`
        echo "ln -s $name_old $name_new"
done