rsync 소스 디렉토리에 없는 파일을 목표 디렉토리에서 삭제하기

데이터를 처리하는 중에 moving windows와 같은 방식으로 데이터 파일을 관리하고 원격 서버에서 데이터를 보내거나 가져오는 일을 하는 경우가 많습니다. 여러가지 방법으로 이 작업을 할 수 있지만 가장 쉽고 편한 방법은 rsync를 사용하는 것입니다.

rsync의 기본 옵션은 소스 디렉토리에 파일이 없고 목표 디렉토리에 있는 경우에 목표 디렉토리의 파일을 삭제하지 않습니다. 안전을 위해서 기본값으로 설정하지 않은 것인데요.

하지만 여러가지 목적으로 소스 디렉토리에 파일이 삭제되면 목표 디렉토리에서 삭제해버리고 싶을때가 있습니다.

다음과 같이 –delete 옵션을 사용하면 됩니다.

rsync -avzrub --delete /data/source_files user01@server01.euriion.com:/data/dest_files/

하지만 다음과 같이 rsync를 사용할 때 소스 디렉토리에 파일을 지정하게 되면 작동하지 않으므로 주의해야 합니다.

rsync -avzrub --delete /data/source_files/* user01@server01.euriion.com:/data/dest_files/

python 파일의 생성날짜 출력하기

데이터 파일이 생성된 날짜 또는 수정된 날짜를 알아내서 화면이나 웹페이지에 표시해주고 싶은 경우가 있습니다.

Python에서 파일의 생성 날짜를 알아내려면 os.p0ath.getctime() 을 사용하면 됩니다.

하지만 os.path.getctime()은 1382189138.419602 와 같이 정수에 날짜를 소수에 시분초를 표기하는 형식을 사용합니다.

os.path.getctime()

getctime으로 받은 숫자를 우리가 흔히 보는 문자열 형태의 시간 표기법을 사용하면 다음과 같이 하면 됩니다.

from datetime import datetime
datetime.fromtimestamp(1382189138.4196026).strftime('%Y-%m-%d %H:%M:%S')

합쳐서 하면 다음과 같습니다.

datetime.datetime.fromtimestamp(os.path.getctime(model_path)).strftime('%Y-%m-%d %H:%M:%S')

추가로 파일의 수정날짜는 os.path.getmtime() 함수를 사용하면 됩니다.

쉘스크립트 시작 날짜 끝 날짜로 날짜 목록 만들기 / shell script – get date list with begin date and end date

bash shell script로 시작날짜와 끝날짜의 목록으로 날짜목록을 만드는 코드입니다.

여러 날짜에 대해서 일별로 일괄처리를 할 때 자주 쓰는 코드입니다.

#!/usr/bin/env bash

begin_date="20210101"
end_date="20210201"

date_delta_days=$(( ($(date --date="$end_date" +%s) - $(date --date="$begin_date" +%s) )/(60*60*24) ))
for (( i=0; i<=date_delta_days; i++ )); do
    date_str=$(date --date="$begin_date +$i days" +%Y%m%d)
    echo "$date_str"
done

결과

20210101
20210102
20210103
20210104
20210105
20210106
20210107
20210108
20210109
20210110
20210111
20210112
20210113
20210114
20210115
20210116
20210117
20210118
20210119
20210120
20210121
20210122
20210123
20210124
20210125
20210126
20210127
20210128
20210129
20210130
20210131
20210201

git 오류 fatal: unable to connect to cache daemon: Permission denied

git pull 또는 push를 할 때 다음과 같은 오류가 나오는 경우가 있습니다.

fatal: unable to connect to cache daemon: Permission denied

sudo를 사용하거나 권한을 잘못 변경하고 git를 사용하다가 발생한 문제인데 ~/.cach/git/credential 디렉토리의 권한을 현재 사용자에게 소유권을 다시 넘겨주면 해결됩니다.

다음과 같이 합니다.

sudo chown $(whoami) ~/.cache/git/credential/

Go언어 문자열에서 앞 또는 뒤 문자열 제거하기

Trim이라고 하는 그것입니다.

문자열에서 앞 또는 뒷쪽의 특정 문자열을 제거하는 코드입니다.

// Go program to illustrate 
// how to trim a string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "!!Welcome to GeeksforGeeks !!"
    str2 := "@@This is the tutorial of Golang$$"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
  
    // Trimming the given strings
    // Using Trim() function
    res1 := strings.Trim(str1, "!")
    res2 := strings.Trim(str2, "@$")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}

apt-get update failed because certificate verification failed because handshake failed on nodesource

apt install을 하는데 다음과 같은 에러가 나오는 경우가 있습니다.

apt-get update failed because certificate verification failed because handshake failed on nodesource

위 에러는 우분투 리눅스에 설치된 certification에 문제가 생겼기 때문입니다.

다음과 같이 재설치하면 해결됩니다.

sudo apt install ca-certificates