Linux command: rm & rmdir — remove files and directories

Olarik Surinta
olarik
Published in
4 min readFeb 12, 2019

คำสั่งสำหรับการลบไดเรกทอรี (rmdir)

  • สำหรับการลบไดเรกทอรีนั้นสามารถใช้คำสั่ง rmdir เพื่อลบไดเรกทอรี แต่จะทำได้เฉพาะกรณีที่เป็นไดเรกทอรีว่างเท่านั้น (Empty directory)
  • เริ่มต้นด้วยการใช้คำสั่ง tree เพื่อดูข้อมูลไดเรกทอรีและไฟล์
$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ ├── test.txt
│ └── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
├── html
│ └── index.html
├── test.html
├── test.py
├── tmp1
│ ├── index.htm
│ └── index.html
└── weather.py
4 directories, 12 files
  • หากไดเรกทอรีที่ลบนั้นมีข้อมูลอยู่ภายในจะไม่สามารถลบได้ แสดงดังตัวอย่างต่อไปนี้
$ rmdir html
rmdir: failed to remove 'html': Directory not empty

จากตัวอย่างได้ใช้คำสั่ง rmdir เพื่อลบไดเรกทอรีชื่อ html แต่ในกรณีนี้ไดเรกทอรี html มีไฟล์ index.html ทำให้ไดเรกทอรี html ไม่ได้เป็นไดเรกทอรีว่าง ดังนั้นจึงไม่สามารถลบไดเรกทอรีนี้ได้

  • ในกรณีนี้จึงยกตัวอย่างด้วยการสร้างไดเรกทอรีชื่อ tmp2 เพื่อใช้สำหรับทดสอบ และดูโครงสร้างของไดเรกทอรี ด้วยคำสั่งดังต่อไปนี้
$ mkdir tmp2
$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ ├── test.txt
│ └── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
├── html
│ └── index.html
├── test.html
├── test.py
├── tmp1
│ ├── index.htm
│ └── index.html
├── tmp2
└── weather.py
5 directories, 12 files
  • จากนั้นทดสอบลบไดเรกทอรีชื่อ tmp2 ด้วยคำสั่ง rmdir และใช้คำสั่ง tree เพื่อดูการเปลี่ยนแปลง แสดงดังตัวอย่างต่อไปนี้
$ rmdir tmp2
$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ ├── test.txt
│ └── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
├── html
│ └── index.html
├── test.html
├── test.py
├── tmp1
│ ├── index.htm
│ └── index.html
└── weather.py
4 directories, 12 files

สังเกตได้ว่าไดเรกทอรี tmp2 ถูกลบออกไปด้วยคำสั่ง rmdir ทั้งนี้เนื่องจากเป็นไดเรกทอรีเปล่า

คำสั่งสำหรับการลบไฟล์ (rm)

สำหรับการลบไฟล์ในระบบลินุกซ์นั้นใช้คำสั่ง rm ซึ่งสามารถเรียกดูตัวเลือกของคำสั่ง rm ได้ดังตัวอย่างต่อไปนี้

$ rm --help
Usage: rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).
-f, --force ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-I prompt once before removing more than three files, or
when removing recursively; less intrusive than -i,
while still giving protection against most mistakes
--interactive[=WHEN] prompt according to WHEN: never, once (-I), or
always (-i); without WHEN, prompt always
--one-file-system when removing a hierarchy recursively, skip any
directory that is on a file system different from
that of the corresponding command line argument
--no-preserve-root do not treat '/' specially
--preserve-root do not remove '/' (default)
-r, -R, --recursive remove directories and their contents recursively
-d, --dir remove empty directories
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit
By default, rm does not remove directories. Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.
To remove a file whose name starts with a '-', for example '-foo',
use one of these commands:
rm -- -foo
rm ./-fooNote that if you use rm to remove a file, it might be possible to recover
some of its contents, given sufficient expertise and/or time. For greater
assurance that the contents are truly unrecoverable, consider using shred.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/rm>
or available locally via: info '(coreutils) rm invocation'
  • ดังนั้น หากต้องการลบไฟล์เพียงหนึ่งไฟล์สามารถทำได้ดังตัวอย่างต่อไปนี้
$ rm test.py
$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ ├── test.txt
│ └── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
├── html
│ └── index.html
├── test.html
├── tmp1
│ ├── index.htm
│ └── index.html
└── weather.py
4 directories, 11 files
  • จากตัวอย่าง ทำการลบไฟล์ชื่อ test.py โดยขณะที่ลบไฟล์ระบบจะไม่แจ้งเตือนสถานะการทำงาน หากต้องการให้ระบบแจ้งเตือนสถานะการทำงานสามารถทำได้โดยใช้ตัวเลือก (option) -v หรือ --verbose ดังตัวอย่างต่อไปนี้
$ rm -v weather.py 
removed 'weather.py'

จากตัวอย่างข้างต้น ได้ลบไฟล์ weather.py ด้วยคำสั่ง rm -v ซึ่งระบบจะแสดงข้อความแจ้งเตือนการลบไฟล์ให้ได้ทราบ จากนั้นใช้คำสั่ง tree เพื่อตรวจสอบการลบไฟล์

$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ ├── test.txt
│ └── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
├── html
│ └── index.html
├── test.html
└── tmp1
├── index.htm
└── index.html
4 directories, 10 files

การลบไฟล์มากกว่า 1 ไฟล์

  • การลบไฟล์ด้วยคำสั่ง mv นั้นสามารถลบได้ครั้งละมากกว่า 1 ไฟล์ โดยสามารถพิมพ์ชื่อไฟล์ที่ต้องการลบเพิ่มต่อลงไปในคำสั่งได้ ดังตัวอย่างต่อไปนี้
$ rm backup/tmp/weather.py backup/tmp/test.html
  • จากตัวอย่างข้างต้นได้ลบไฟล์ด้วยคำสั่ง rm จำนวนทั้งสิ้น 2 ไฟล์ประกอบด้วยไฟล์ backup/tmp/weather.py และ backup/tmp/text.html ซึ่งทั้งสองไฟล์อยู่ในไดเรกทอรีย่อย backup/tmp
  • สามารถดูผลลัพธ์จากคำสั่ง rm ด้วยการพิมพ์คำสั่ง tree ผลลัพธ์แสดงดังต่อไปนี้
$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ ├── test.txt
│ └── tmp
│ └── index.html
├── html
│ └── index.html
├── test.html
└── tmp1
├── index.htm
└── index.html
4 directories, 8 files

การใช้เครื่องหมาย wildcard ร่วมกับการลบไฟล์

  • เครื่องหมาย wildcard ที่สามารถใช้งานร่วมกับคำสั่ง rm ได้แก่ asterisk (*) และ question mark (?) เป็นต้น
  • ตัวอย่างการใช้เครื่องหมาย wildcard แสดงดังต่อไปนี้
$ rm backup/*.h*
  • จากคำสั่งข้างต้นได้ใช้เครื่องหมาย asterisk (*) ร่วมกับคำสั่ง rm โดยหมายความว่าให้ลบไฟล์ในไดเรกทอรี backup โดยไฟล์ที่จะลบมีชื่ออะไรก็ได้ (แทนด้วยเครื่องหมาย *) และมีนามสกุลที่ขึ้นต้นด้วยตัวอักษร h และตามด้วยตัวอักษรอะไรก็ได้ (แทนด้วยเครื่องหมาย .h*)
  • ดังนั้น ไดเรกทอรี backup ที่มีไฟล์ที่มีนามสกุลที่ขึ้นต้นด้วยตัว h คือ index.htm และ test.html จึงเข้าเงื่อนไขดังกล่าว จึงทำให้ทั้งสองไฟล์ถูกลบออกไป
  • ผลลัพธ์ที่ได้จากคำสั่ง rm backup/*.h* แสดงดังต่อไปนี้
$ tree
.
├── backup
│ ├── test.txt
│ └── tmp
│ └── index.html
├── html
│ └── index.html
└── tmp1
├── index.htm
└── index.html
4 directories, 5 files

การลบไดเรกทอรีที่มีไฟล์หรือไดเรกทอรีย่อย

  • สำหรับการลบไดเรกทอรีที่มีข้อมูลอยู่ด้านในไม่สามารถใช้คำสั่ง rmdir ได้ตามปกติ แสดงดังตัวอย่างต่อไปนี้
$ rmdir backup
rmdir: failed to remove 'backup': Directory not empty
  • จากตัวอย่างข้างต้นได้ทดสอบด้วยการลบไดเรกทอรี backup ซึ่งมีข้อมูลที่เป็นทั้งไฟล์และไดเรกทอรีอยู่ด้านใน ผลลัพธ์ที่ได้คือระบบไม่อนุญาตให้ลบไดเรกทอรี backup
  • ดังนั้นการลบไดเรกทอรีที่มีข้อมูลอยู่ด้านในจะต้องใช้ตัวเลือก (option) แสดงดังตัวอย่างต่อไปนี้
$ rm -vrf backup
removed 'backup/test.txt'
removed 'backup/tmp/index.html'
removed directory 'backup/tmp'
removed directory 'backup'

จากตัวอย่างข้างต้นเลือกใช้ตัวเลือกคือ -vrf ซึ่งมีความหมายดังนี้

  • v คือ verbose หมายถึงให้อธิบายหรือแสดงสิ่งที่ระบบได้ทำ เช่น removed 'backup/test.txt' ระบบได้ทำการลบไฟล์ที่ชื่อ backup/test.txt
  • r คือ recursive หมายถึงการลบไฟล์และไดเรกทอรีย่อยที่อยู่ภายในไดเรกทอรีที่ต้องการลบ
  • f คือ force หมายถึงให้ยกเลิกการลบไฟล์ที่ไม่ปรากฏ

สุดท้ายพิมพ์คำสั่ง tree เพื่อดูผลลัพธ์ที่ได้จากการใช้คำสั่ง rm -vrf

$ tree
.
├── html
│ └── index.html
└── tmp1
├── index.htm
└── index.html
2 directories, 3 files
Photo by Mr Cup / Fabien Barral on Unsplash

--

--