한 단계로 다른 디렉터리에서 파일 이름을 복사하고 접두사를 추가하는 방법은 무엇입니까?

한 단계로 다른 디렉터리에서 파일 이름을 복사하고 접두사를 추가하는 방법은 무엇입니까?

한 디렉터리에서 다른 디렉터리로 여러 파일을 복사하고 이름을 바꾸고 싶습니다. 특히, 나는 다음과 같은 것을 원합니다:

/tmp/tmp.XXX/aaa.original.txt
/tmp/tmp.XXX/bb5.original.txt
/tmp/tmp.XXX/x2x.original.txt

에게 복사

/root/hello/dump-aaa.txt
/root/hello/dump-bb5.txt
/root/hello/dump-x2x.txt

비슷한 것을 시도했지만 작동하지 않습니다.

  • cp /tmp/tmp.XXX/*.original.txt /root/hello/*.txt
  • find /tmp/tmp.XXX/ -name '*.original.txt' | xargs -i cp /root/hello/dump-{}.txt
  • for f in /tmp/tmp.XXX/*.original.txt; do cp -- "$f" "/root/hello/dump-$f.txt"; done

일반적으로 위 코드의 결과는 다음과 같습니다.실수:

cp: cannot create regular file '/root/hello/dump-/tmp/tmp.XXX/aaa.original.txt.txt': No such file or directory

답변1

bash해결책:

for f in /tmp/tmp.XXX/*.original.txt; do 
    bn="${f##*/}"   # extracting file basename
    cp "$f" "/root/hello/dump-${bn%%.*}.txt"
done

관련 정보