Daily Tips : การเปลี่ยนชื่อไฟล์พร้อมกันที่ละหลายๆ ไฟล์

mrgill   | March 23, 2010   |   1,077 Views

วันนี้พี่ที่บริษัทโทรมาเพราะมีปัญหาแตกไฟล์ที่ archive ไว้ไม่ได้เพราะติดปัญหาชื่อไฟล์มีเครื่องหมายโคลอน ” : ” อยู่ในไฟล์ด้วย เพราะ shell script ที่ผมเขียนไว้ให้ archive ไฟล์ตั้งให้มันสร้างชื่อไฟล์เป็นชั่วโมงและแสดงนาทีโดยผมเลือกใช้ ” : ” กั้นระหว่างหน่วยชั่วโมงและนาที ผมลอง ssh เข้าไปดูแล้วก็พบว่ามีไฟล์เยอะมากครั้นจะนั่ง rename ทีละไฟล์ก็คงลำบากแน่ๆ ก็เลย เข้า google แล้ว search วิธีการ rename ไฟล์ที่ละหลายๆ ไฟล์ก็ไปเจออยู่ที่ http://www.cyberciti.biz/tips/renaming-multiple-files-at-a-shell-prompt.html ทำตามเสร็จก็เลยคิดว่าเอามาเขียนไว้กันลืมด้วยดีกว่า

สมมติว่าผมมีไฟล์ file_XX:xx.txt อยู่ ซึ่งผมสร้างมาโดยคำสั่ง touch

mrgill@mrgill-laptop:~/lab_command$ ls file_15:35.txt file_16:30.txt file_18:55.txt

ถ้าหากผมใช้คำสั่ง rename หรือ mv ในการเปลี่ยนชื่อไฟล์ file_XX:xx.txt ให้เป็น file_XX_xx.txt ก็คงต้องทำ 3 ครั้ง แต่ด้วยวิธีต่อไปนี้เราสามารถเปลี่ยนชื่อไฟล์ทั้งหมดให้เป็น file_XX_xx.txt ได้ในคำสั่งเดียว โดย

mrgill@mrgill-laptop:~/lab_command$ rename "s/\:/_/g" *.txt

เมื่อ ” \: ” คือสิ่งที่เราต้องการจะเปลี่ยน และ ” _ ” คือสิ่งที่เราจะเอามาแทน ” : ” และ *.txt คือนามสุกลของไฟล์ที่เราต้องการจะเปลี่ยน
ผลที่ได้คือ

mrgill@mrgill-laptop:~/lab_command$ ls file_15_35.txt file_16_30.txt file_18_55.txt

ลองนำไปประยุกต์ใช้กันดู

Comments

Category : Daily command,Daily tips,Linux

Tags: command, multile files, regula expression, rename

Related Posts

การ config squid3 ให้เก็บชื่อ hostname ของ client ใน access.log ฮากลิ้งลิงเล่นกล

6 Comments Leave a Comment

  • 1. Tweets that mention Daily&hellip  |  March 25, 2010 at 3:11 AM

    [...] This post was mentioned on Twitter by Montree Phomkam, Thayodit Khuankoub. Thayodit Khuankoub said: RT @imrGilL: Blog : Daily Tips : การเปลี่ยนชื่อไฟล์พร้อมกันที่ละà.. http://bit.ly/9lbqjq [...]

  • 2. ดูดวงฟรี  |  March 25, 2010 at 9:45 AM

    ขอบคุณมากครับ

  • 3. Keng  |  March 25, 2010 at 3:59 PM

    ลองแล้วแต่เปลี่ยนไม่ได้จากชื่อไฟล์

    File_2010_03_02.txt File_2010_03_03.txt ไปเป็น File-2010-03-02.txt File-2010-03-03.txt

    คำสั่งที่ใช้
    rename “s/_/-/g” *.txt

    ช่วยหน่อย

  • 4. mrgill  |  March 25, 2010 at 6:54 PM

    มันก็ได้นะเพื่อนเก่ง

    mrgill@mrgill-laptop:~/lab_command$ ls file_15_35.txt file_16_30.txt file_18_55.txt mrgill@mrgill-laptop:~/lab_command$ rename "s/_/-/g" *.txt mrgill@mrgill-laptop:~/lab_command$ ls -al total 20 drwxr-xr-x 2 mrgill mrgill 4096 2010-03-25 18:51 . drwxr-xr-x 92 mrgill mrgill 4096 2010-03-25 18:34 .. -rw-r--r-- 1 mrgill mrgill 5 2010-03-23 17:23 file-15-35.txt -rw-r--r-- 1 mrgill mrgill 5 2010-03-23 17:23 file-16-30.txt -rw-r--r-- 1 mrgill mrgill 5 2010-03-23 17:23 file-18-55.txt mrgill@mrgill-laptop:~/lab_command$ ls file-15-35.txt file-16-30.txt file-18-55.txt
    
    	                        
  • 5. Keng  |  March 26, 2010 at 9:48 AM

    ดูตรงนี้ ยังไม่ได้เลย มั้นต้องเซ็ตไรเพิ่มเปล่าอ่ะ

    [keng@host CVSTEST]$ ls
    venda_30_10.txt venda_30_18.txt venda_30_25.txt venda_30_4.txt
    venda_30_11.txt venda_30_19.txt venda_30_26.txt venda_30_5.txt
    venda_30_12.txt venda_30_1.txt venda_30_27.txt venda_30_6.txt
    venda_30_13.txt venda_30_20.txt venda_30_28.txt venda_30_7.txt
    venda_30_14.txt venda_30_21.txt venda_30_29.txt venda_30_8.txt
    venda_30_15.txt venda_30_22.txt venda_30_2.txt venda_30_9.txt
    venda_30_16.txt venda_30_23.txt venda_30_30.txt
    venda_30_17.txt venda_30_24.txt venda_30_3.txt
    [keng@host CVSTEST]$ rename “s/_/-/g” *.txt
    [keng@host CVSTEST]$ ls
    venda_30_10.txt venda_30_18.txt venda_30_25.txt venda_30_4.txt
    venda_30_11.txt venda_30_19.txt venda_30_26.txt venda_30_5.txt
    venda_30_12.txt venda_30_1.txt venda_30_27.txt venda_30_6.txt
    venda_30_13.txt venda_30_20.txt venda_30_28.txt venda_30_7.txt
    venda_30_14.txt venda_30_21.txt venda_30_29.txt venda_30_8.txt
    venda_30_15.txt venda_30_22.txt venda_30_2.txt venda_30_9.txt
    venda_30_16.txt venda_30_23.txt venda_30_30.txt
    venda_30_17.txt venda_30_24.txt venda_30_3.txt
    [keng@host CVSTEST]$
  • 6. mrgill  |  March 26, 2010 at 10:22 AM

    ลองไปเล่นบน centos ละ ไม่ได้เหมือนกัน
    แสดงว่าใช้ได้เฉพาะ debian base
    เดี่ยวค่อยหาวิธีให้นะ

Leave a Comment

(required)

(required), (Hidden)

 

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

TrackBack URL  |  RSS feed for comments on this post.


Chat

Recent Entries

Random Posts

Social Network

Tag Cloud

Calendar

March 2010
S M T W T F S
« Feb   Apr »
 123456
78910111213
14151617181920
21222324252627
28293031  

Twitter: imrgill

RSS LinuxToday

RSS HowToForge

RSS UbuntuClub

RSS Blognone

RSS PowerWindowsBlog

Tag

Categories

Archives

Blogroll

Meta

Get Adobe Flash player