유닉스엔 원칙적으로 확장자의 개념이 없다.
인간이 구분하기 위해 구분해 놓은 것에 불과하다.
편의상 구분해 놓은 구분자를 변경하는 방법에 관한 팁을 소개한다.

---------------------------------------------------------------------
확장자 바꾸기

여러 파일들의 확장자를 변경하고자 할 때 다음과 같은 방식을 이용할 수 없다.

    % mv *.abc *.def

대신 다음의 쉘 스크립트로 원하는 작업을 할 수 있다.

--------- 시작 ---------
#!/bin/sh
for f in *.abc; do
    mv $f `basename $f .abc`.def
done
--------- 끝 ---------

모든 .abc로 끝나는 파일을 찾아 $f를 파일명으로 바꿔가면서(for)

    mv FILENAME `basename $f.abc`.def

명령을 실행한다.

`basename $f .abc` 명령은 파일명중에서 .abc를 제외한 값을 반환한다.
따라서 위의 mv 명령은 다음과 같은 명령으로 완성된다.(file은 .abc를 제외한 파일명)

    mv file.abc file.def

    done

for 루프를 마친다.

csh이나 tcsh에서는 다음과 같이 하여 위의 작업을 할 수 있다.

    foreach f in ( *.abc )
        mv $f `basename $f .abc`.def
    end

--------- 원문 ---------

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

                             UNIX GURU UNIVERSE
                                UNIX HOT TIP

                       Unix Tip 2932 - February 15, 2009
                   http://www.ugu.com/sui/ugu/show?tip.today

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

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

반응형
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

최근에 올라온 글

최근에 달린 댓글

글 보관함