파일이름 필터링 해 스크립트에 사용하기

최연수·2022년 9월 28일
0
$ find to/path/ -mindepth 1 -maxdepth 1 | grep -o '\[[0-9]*\]' | grep -o '[0-9]*' | sed 's/^/https:\/\/example.com\//g' | xargs -n 1 ./download.sh

폴더 이름 형식: foobar123 [1234]
필요한 정보: 대괄호 안 UID
필요한 형식: https://example.com/UID

  1. 폴더 리스팅(-type d optional) find to/path/ -mindepth 1 -maxdepth 1
foobar123 [1234]
foobar456 [4567]
...
  1. 파이프라인으로 받은 대괄호 안에 있는 숫자 추출 grep -o '\[[0-9]*\]'
[1234]
[4567]
...
  1. 추출한 결과에서 대괄호 제거한 숫자만 추출 grep -o '[0-9]*'
1234
4567
...
  1. 스크립트에서 이용할 url 생성 sed 's/^/https:\/\/example.com\//g'
https://example.com/1234
https://example.com/4567
...
  1. xargs 이용 스크립트에 한줄씩 전달
./download.sh https://example.com/1234
./download.sh https://example.com/4567
...

TODO: sed, awk 등 이용해서 grep 파이프라인 단계 줄여보기

0개의 댓글