배쉬 원 라이너:template_*.txt를 foo_*.txt로 복사하시겠습니까?
-
09-06-2019 - |
문제
세 개의 파일(template_*.txt)이 있다고 가정해 보겠습니다.
- template_x.txt
- template_y.txt
- template_z.txt
세 개의 새 파일(foo_*.txt)에 복사하고 싶습니다.
- foo_x.txt
- foo_y.txt
- foo_z.txt
하나의 명령으로 이를 수행하는 간단한 방법이 있습니까?
cp --enableAwesomeness template_*.txt foo_*.txt
해결책
for f in template_*.txt; do cp $f foo_${f#template_}; done
다른 팁
[01:22 PM] matt@Lunchbox:~/tmp/ba$
ls
template_x.txt template_y.txt template_z.txt
[01:22 PM] matt@Lunchbox:~/tmp/ba$
for i in template_*.txt ; do mv $i foo${i:8}; done
[01:22 PM] matt@Lunchbox:~/tmp/ba$
ls
foo_x.txt foo_y.txt foo_z.txt
내가 선호하는 방법 :
for f in template_*.txt
do
cp $f ${f/template/foo}
done
"I-don-the-the-the-substitution-syntax"방법 :
for i in x y z
do
cp template_$i foo_$
done
이것은 작동해야합니다 :
for file in template_*.txt ; do cp $file `echo $file | sed 's/template_\(.*\)/foo_\1/'` ; done
for i in template_*.txt; do cp -v "$i" "`echo $i | sed 's%^template_%foo_%'`"; done
파일 이름에 펑키 한 캐릭터가 있으면 깨질 것입니다. '-v'를 제거하면 (if) 안정적으로 작동한다는 확신이 있습니다.
Bash 나 CP에서는 아무것도 모르지만 PERL 스크립트를 사용하여 이런 종류의 작업을 수행하는 간단한 방법이 있습니다.
($op = shift) || die "Usage: rename perlexpr [filenames]\n";
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}
그 다음에:
rename s/template/foo/ *.txt
또 다른 방법 :
$ ls template_*.txt | sed -e 's/^template\(.*\)$/cp template\1 foo\1/' | ksh -sx
나는 항상 Imagemagick에 깊은 인상을 받았습니다 전환하다 이미지 형식으로 기대하는 프로그램 :
$ convert rose.jpg rose.png
배치 변환을 허용하는 자매 프로그램이 있습니다.
$ mogrify -format png *.jpg
분명히 이것들은 이미지 변환으로 제한되지만 흥미로운 명령 줄 인터페이스가 있습니다.
제휴하지 않습니다 StackOverflow