[태그:] bash

  • 중복파일 지우기

    다른 사이트에서 스크랩..

    시스템내 중복되어있는 파일을 정리하는데 가장쉽고 편리한 방법이 아닐까 합니다.
    정규표현식 써가면서 파일싸이즈 비교해서 뽑을 필요없이 아래 패키지 설치하나로 간단히 해결…
    참고
    https://code.google.com/p/fdupes/
    https://github.com/adrianlopezroche/fdupes

    -r 옵션은 하위디렉토리 까지 모두 검색
    -S 중복파일 사이즈 출력

    How to use fdupes?

    fdupes has a rich CLI:
    fdupes -r ./stuff > dupes.txt
    Then, deleting the duplicates was as easy as checking dupes.txt and deleting the offending directories. fdupes also can prompt you to delete the duplicates as you go along.

    fdupes -r /home/user > /home/user/duplicate.txt

    Output of the command goes in duplicate.txt.

    fdupes will compare the size and MD5 hash of the files to find duplicates.

    Unix: How to delete files listed in a file

    This is not very efficient, but will work if you need glob patterns (as in /var/www/*)

    for f in $(cat 1.txt) ; do
    rm $f
    done

    If you don’t have any patterns, you can use xargs like so:

    xargs rm < 1.txt

  • bash 스크립트로 사진정보를 mysql로 업데이트

    몇개월전에 LAMP로 휴대폰으로 찍은 다수의 사진을 관리하는 페이지를 만들었다. 일정 기간이후 사진을 어느 폴더로 이동후, JPG의 EXIF에 대한 정보를 파일로 뽑아내어서, 내가 설정한 DB형식에 맞춰 추가를 해줘야 되는데 이게 좀 노가다성이 있다.

    이를 해결하기 위해서 인터넷 뒤져서 아래와 같은 스크립을 만들었다. cron에 등록해서 하루에 한번씩 해주면 될것 같은데, mysql 서비스에 패스워드를 치는 부분을 사용자 입력을 기다리지 않고 자동으로 해줘야 되는데, 아직 구현하지 못했다. 나중에 보완하면 되는데 언제 할지 모르겠다.

    #!/bin/bash
    mytemp=$(mktemp);
    mytemp2=$(mktemp);
    #mytemp3=$(mktemp);
    filelist=$(mktemp);
    rm $1;
    rm 데이터;
    mysql -D myHome -u now0930 -p -e "select 날자 from picture order by 날자 desc limit 10" &gt; $mytemp;
    #cat $mytemp;
    
    #head -2 $mytemp;
    #sed -n 2p $mytemp &gt; $mytemp2;
    
    #mysql에서 마지막으로 업데이트한 날짜를 추출해서 변수로 기록
    from=$(sed -n 2p $mytemp | cut -d' ' -f1);
    next_from=$(date +%Y-%m-%d -d "$from + 1 day");
    echo "프롬:"$from;
    echo "다음날:"$next_from;
    #sleep 100;
    cat $mytemp2;
    #cut -d ' ' -f1 $mytemp2 &gt; $mytemp3;
    #cat $mytemp3;
    
    #현재 날짜를 변수로 기록
    now=$(date +%Y-%m-%d);
    echo $now;
    
    #start,end를 지움..
    rm start now;
    
    #mysql의 마지막 날짜로 touch를 사용해서 만듦.
    touch --date $next_from ./start
    touch --date $now ./now
    
    echo "start, now 만들어짐";
    find ~/ExtHDD/pictureToWWW/20151110/ -type f -newer ./start ! -newer ./now \
    | grep -v 지울폴더 | grep -ie "jpg\|JPG\|jpeg" &gt; $1;
    echo "파일 찾아짐";
    
    #find ./ -type f -iname "*jpg" -o -iname "*mp4"\
    # -newer ./start -not -newer ./now &gt; $filelist;
    
    #echo $filelist;
    
    # JPG 파일을 입력 받아서 meta 데이터를 출력하는 스크립트..
    
    #1번째 인자..JPG가 입력되어 있는 파일명..
    
    cat $1 |\
    while read CMD; do
    
    ###############
    #루프의 시작..
    ################
    
    #ls
    #eval "exiv2 -ps $1 &gt; temp"
    echo "파일명 확인부";
    echo $CMD
    echo `exiv2 -ps $CMD &gt; temp`
    echo "여기서 실행확인"
    echo `cat temp`
    eval "awk '/File name/{print $4}' temp &gt; 파일명";
    #filename=$(echo `awk 'BEGIN{FS="\/";}{print "/imageOtherPar/"\$6"/"\$7}' temp`)
    filename=$(awk 'BEGIN{FS="/";}{print "/imageOtherPar/"$6"/"$7}' 파일명);
    
    echo "파일명"
    echo $filename
    echo "파일명 출력 완료"
    
    #eval "awk '/File name/{print \$4}' temp
    #$filename = $eval "awk '/File name/{print \$4}' temp"
    
    #eval "exiv2 -px $1 &gt; temp"
    echo `exiv2 -pa $CMD &gt; temp`
    #eval "awk 'BEGIN{print \"EXIF 추출기\";FS=\" \";}/subject/{print \$4,\$5,\$6;}' temp &gt;&gt; 파일명"
    tag=$(eval "awk '/subject/{print \$4,\$5,\$6;}' temp")
    width=$(eval "awk 'BEGIN{FS=\" \";}/Image\.ImageWidth/{print \$4}' temp")
    height=$(eval "awk 'BEGIN{FS=\" \";}/Image\.ImageHeight/{print \$4}' temp")
    if [$height -eq ""]
    then
    height=$(eval "awk /'Image\.ImageLength/{print \$4}' temp")
    fi
    
    date=$(eval "awk 'BEGIN{FS=\" \";}/DateTimeOriginal/{print \$4}' temp")
    time=$(eval "awk 'BEGIN{FS=\" \";}/DateTimeOriginal/{print \$5}' temp")
    #latitudeRef=$(eval "awk 'BEGIN{FS=\" \";}/GPSLatitudeRef/{print \$4}' temp")
    latitude=$(eval "awk 'BEGIN{FS=\" \";}/GPSLatitude/{print \$4}' temp")
    #longitudeRef=$(eval "awk 'BEGIN{FS=\" \";}/GPSLongitudeRef/{print \$4}' temp")
    longitude=$(eval "awk 'BEGIN{FS=\" \";}/GPSLongitude/{print \$4}' temp")
    
    echo "테스트 영역"
    echo "width:"$width
    echo "height:"$height
    echo "종합\n"
    echo $width $height $tag $date $time $latitudeRef $latitude $longitudeRef $longitude
    #echo $tag
    #$tag= $eval "awk 'BEGIN{print \"EXIF 추출기\";FS=\" \";}/subject/{print \$4,\$5,\$6;}' temp"
    #tag= $(awk 'BEGIN{print \"EXIF 추출기\";FS=" ";}/subject/{print \$4,\$5,\$6;}' temp)
    #echo $tag
    
    #echo $filename $tag
    
    echo -e $latitudeRef
    echo -e $latitude
    
    #echo -e $filename "\t" $tag "\t" $date "\t" $time "\t" $width "\t" $height "\t" $latitudeRef "\t" $latitude "\t" $longitudeRef "\t" $longitude  &gt;&gt; 데이터
    
    echo -e " \t"$date" "$time"\t"$tag"\t"$filename"\t""이미지\t"$width"\t"$height"\t"$latitude"\t"$longitude"\t"  &gt;&gt; 데이터
    echo -e "너비" $width "높이" $height
    echo "" &gt; temp
    done
    
    ###############
    #루프의 끝...
    ################
    
    #echo $mytemp;
    rm $mytemp;
    rm $mytemp2;
    #rm $mytemp3;
    rm $filelist;
    
    #아래는 mysql으로 업데이트 하는 부분..
    #home 디렉토리의 .my.cnf파일을 참조해서
    #자동으로 업데이트
    mysql --local_infile=1 myHome -e "LOAD DATA LOCAL INFILE '데이터' INTO TABLE picture CHARACTER SET UTF8 FIELDS TERMINATED BY  '\t' "