ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Ubuntu] 랜덤하게 파일 선택 및 복사
    카테고리 없음 2022. 2. 28. 13:06

    목적

    • 다량의 데이터를 다루고 복잡한 구조의 디렉토리 형태를 다루다보니 특정 갯수의 파일(또는 폴더)이나 특정 형식의 파일를 복사하기 위해선 조금 더 똑똑하게 명령어를 쓸 수 있어야한다.
    • 샘플 데이터를 생성하기 위해 현재 디렉토리에 있는 폴더(*) 중 랜덤하게 100개를 선택해 target 폴더로 복사해보자
    shuf -zn100 -e * | xargs -0 cp -rvt target/
    • shuf shuffles the list of * files in the current directory.
    • -z is to zero-terminate each line, so that files with special characters are treated correctly.
    • -n100 exits shuf after 100 files.
    • xargs -0 reads the input delimited by a null character (from shuf -z) and runs cp.
    • -v is to print every copy verbosely.
    • -t is to specify the target directory.

     

    응용

    for FILE in *; do mkdir ../sample/$FILE; shuf -zn100 -e $FILE/* | xargs -0 cp -rvt ../sample/$FILE; done

     

    출처

    https://unix.stackexchange.com/questions/217712/randomly-copy-certain-amount-of-certain-file-type-from-one-directory-into-anothe

    댓글

Designed by Tistory.