![한 단계로 다른 디렉터리에서 파일 이름을 복사하고 접두사를 추가하는 방법은 무엇입니까?](https://linux55.com/image/121033/%ED%95%9C%20%EB%8B%A8%EA%B3%84%EB%A1%9C%20%EB%8B%A4%EB%A5%B8%20%EB%94%94%EB%A0%89%ED%84%B0%EB%A6%AC%EC%97%90%EC%84%9C%20%ED%8C%8C%EC%9D%BC%20%EC%9D%B4%EB%A6%84%EC%9D%84%20%EB%B3%B5%EC%82%AC%ED%95%98%EA%B3%A0%20%EC%A0%91%EB%91%90%EC%82%AC%EB%A5%BC%20%EC%B6%94%EA%B0%80%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
한 디렉터리에서 다른 디렉터리로 여러 파일을 복사하고 이름을 바꾸고 싶습니다. 특히, 나는 다음과 같은 것을 원합니다:
/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