디렉토리에 파일 묶음이 있습니다. 동일한 파일을 다른 확장명을 가진 다른 디렉토리에 복사하려고합니다.쉘 명령으로 파일 이름 만 가져 오기
$ pwd
source_path
$ ls
Test File 1.xyz
test File 2.xyz
Blah Blah.xyz
다른 디렉토리에 복사하고 싶습니다.
$ pwd
dest_path
$ ls
Test File 1.abc
test File 2.abc
Blah Blah.abc
디렉토리에 파일 묶음이 있습니다. 동일한 파일을 다른 확장명을 가진 다른 디렉토리에 복사하려고합니다.쉘 명령으로 파일 이름 만 가져 오기
$ pwd
source_path
$ ls
Test File 1.xyz
test File 2.xyz
Blah Blah.xyz
다른 디렉토리에 복사하고 싶습니다.
$ pwd
dest_path
$ ls
Test File 1.abc
test File 2.abc
Blah Blah.abc
무수한 방법이 작업을 수행 할 수 있지만, 아마도 가장 솔직하고 읽기는 문자열 교체 사용하는 것입니다 :
cd source_path
for file in *.xyz; do
cp -av "$file" "dest_path/${file/xyz/abc}"
done
당신은 2 년이
cp "source_path/*.xyz" dest_path
rename 's/\.xyz$/\.abc/' "dest_path/*.xyz"
이 긴 방법을 사용하면 source_path 파일에 필요한 파일 확장자가있을 수 있습니다.
cd $source_path
new_file_ext=".abc"
ls > files.tmp # Put ls output to a temporary file
while read -r line || [[ -n "$line" ]]; do # read the temporary file line by line
file_name=$(echo $line | cut -d'.' -f1) # split the name at the '.' and keep part one
cp $line "$dest_path"/"$file_name""$new_file_ext" # copy file at dest and add new extenssion
done < "files.tmp"
'$ {file/.xyz/.abc}'는 실제 확장 문자열보다 안전 할 수 있습니다 (예 : 'c') 파일 이름에서 쉽게 발견되고 대체 될 수 있습니다. – SLePort
@Kenavoz가 동의했습니다. 또한 문자열의 끝 부분을 보는 덜 사용 된 $ {variable/% foo/bar} 대체가 있습니다. 둘 다 결합하면 꽤 안전해야합니다. – Jameson