2007. 11. 3. 20:59 Unix

확장자 바꾸기

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                             UNIX GURU UNIVERSE
                                UNIX HOT TIP
 
                       Unix Tip 2334 - May 23, 2006
 
                   http://www.ugu.com/sui/ugu/show?tip.today
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
확장자 바꾸기
 
여러 파일들의 확장자를 변경하고자 할때 다음의 명령어로는 수행할 수 없다.
 
       % mv *.abc *.def
 
하지만 다음 쉘 스크립트를 이용하여 원하는 작업을 하는데 사용할 수 있다.
 
***
 
다음 쉘 스크립트를 이용하면 확장자가 .abc인 파일(*.abc)을 모두
확장자가 .def인 파일(*.def)변경할 수 있다.
 
#!/bin/sh
for f in *.abc; do
    mv $f `basename $f .abc`.def
done
 
스크립트가 어떻게 작동하는지 살펴보자.
 
for f in *.abc; do
 
.abc로 끝나는 파일을 찾아 순서대로 파일명을 $F에 대입한다.
 
 mv $f `basename $f.abc`.def
 
`basename $f .abc`는 $f의 파일명을 받아 따라오는 .abc를 떼어내고,
.def를 앞의 결과에 덧붙이면 결과적으로 명령은 다음과 같이 된다.
 
"mv file.abc file.def"
 
done
 
위의 "for" 루프를 종료한다.
 
"csh" 또는 "tcsh"에서 위의 작업을 하려면 다음과 같이 할 수 있다.
 
foreach f in ( *.abc )
   mv $f `basename $f .abc`.def
end
 
----------------------------------------------------------
 
CHANGE THE SUFFIX
 
If you want to change the suffix of multiple files, you can't do:
 
       % mv *.abc *.def
 
However the following shell script can be used to do the required
opperation:
 
***
 
Change all *.abc file to *.def the following shell script would work:
 
#!/bin/sh
for f in *.abc; do
 mv $f `basename $f .abc`.def
done
 
How it works:
 
 for f in *.abc; do
 
Set up a look for all files ending in .abc, and each time around setup
$f as the filename
 
 mv $f `basename $f.abc`.def
 
`basename $f .abc` takes the filename in $f and removes any trailing
occurences of .abc, we then append .def to the result and the resulting
command becomes
"mv file.abc file.def"
 
  done
 
Ends the "for" loop above.
 
Under "csh" or "tcsh" a similar thing could be done with:
 
 foreach f in ( *.abc )
   mv $f `basename $f .abc`.def
 end
 
This tip generously supported by: pwain@liberate.com
반응형
Posted by She쥐포s

블로그 이미지
She쥐포s
Yesterday
Today
Total

달력

 « |  » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

글 보관함