Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

เชี่ยวชาญ Linux File System: เข้าใจ /bin, /usr/bin, /sbin และอื่นๆ

ทำไมโปรแกรมบางตัวอยู่ใน /bin, /sbin, /usr/bin, /usr/sbin?

คุณเคยสงสัยไหมว่า ทำไมคำสั่ง less อยู่ใน /usr/bin ไม่ใช่ /bin, /sbin, หรือ /usr/sbin? ความแตกต่างของไดเรกทอรีเหล่านี้คืออะไร?

บทความนี้จะอธิบายโครงสร้าง Linux Filesystem และความหมายของไดเรกทอรีระดับสูง (High-level directories) ตามมาตรฐาน Filesystem Hierarchy Standard (FHS)

1. / – Root Directory (ไดเรกทอรีราก)

  • ทุกไฟล์และไดเรกทอรีเริ่มต้นจากที่นี่
  • เฉพาะ root user เท่านั้นที่มีสิทธิ์เขียน (Write) ตรงใต้ /
  • หมายเหตุ: /root คือ Home directory ของ root user ไม่ใช่ /

2. /bin – User Binaries (ไบนารีผู้ใช้)

  • เก็บ Executable binaries (ไฟล์รันได้)
  • คำสั่ง Linux พื้นฐานที่ต้องใช้ใน Single-user mode
  • คำสั่งที่ผู้ใช้ทุกคนใช้ได้
  • ตัวอย่าง: ps, ls, ping, grep, cp, mv, rm, cat, bash, sh

3. /sbin – System Binaries (ไบนารีระบบ)

  • เก็บ Executable binaries เช่นเดียวกับ /bin
  • แต่คำสั่งในนี้ใช้โดย System Administrator สำหรับบำรุงรักษาระบบ
  • ตัวอย่าง: iptables, reboot, fdisk, ifconfig, swapon, fsck, mkfs, shutdown, init

4. /etc – Configuration Files (ไฟล์การตั้งค่า)

  • เก็บไฟล์ Config ที่โปรแกรมทั้งหมดต้องการ
  • รวม Shell scripts เริ่มต้น/ปิดโปรแกรม (Startup/Shutdown scripts)
  • ตัวอย่าง: /etc/resolv.conf, /etc/logrotate.conf, /etc/passwd, /etc/fstab, /etc/ssh/sshd_config

5. /dev – Device Files (ไฟล์อุปกรณ์)

  • เก็บ Device files (Special files)
  • รวม Terminal devices, USB, และอุปกรณ์ที่ต่อกับระบบ
  • ตัวอย่าง: /dev/tty1, /dev/usbmon0, /dev/sda, /dev/null, /dev/zero, /dev/random

6. /proc – Process Information (ข้อมูลกระบวนการ)

  • Pseudo filesystem (ไม่ได้เก็บบนดิสก์จริง)
  • เก็บข้อมูล Process ที่กำลังทำงาน เช่น /proc/{pid}/ มีข้อมูล Process ID นั้น
  • Virtual filesystem ด้วยข้อมูลข้อความเกี่ยวกับทรัพยากรระบบ เช่น /proc/uptime, /proc/cpuinfo, /proc/meminfo

7. /var – Variable Files (ไฟล์ที่เปลี่ยนแปลง)

  • var = variable files (ไฟล์ที่คาดว่าจะโต/เปลี่ยนแปลง)
  • รวม: System logs (/var/log), Packages/Database (/var/lib), Emails (/var/mail), Print queues (/var/spool), Lock files (/var/lock), Temp files ต้องเก็บข้าม Reboot (/var/tmp)

8. /tmp – Temporary Files (ไฟล์ชั่วคราว)

  • ไดเรกทอรีสำหรับ Temporary files ที่ระบบและผู้ใช้สร้าง
  • ไฟล์ในนี้จะถูกลบเมื่อ Reboot ระบบ
  • ทุกคนเขียนได้ (Sticky bit ตั้งไว้ ป้องกันลบของคนอื่น)

9. /usr – User Programs (โปรแกรมผู้ใช้ ระดับสอง)

  • เก็บ Binaries, Libraries, Documentation, Source code สำหรับโปรแกรมระดับสอง (Non-essential)
  • /usr/bin - Binary สำหรับโปรแกรมผู้ใช้ทั่วไป หาไม่เจอใน /bin ให้หาที่นี่ (เช่น at, awk, cc, less, scp, vim, python3)
  • /usr/sbin - Binary สำหรับ System Admin หาไม่เจอใน /sbin ให้หาที่นี่ (เช่น atd, cron, sshd, useradd, userdel)
  • /usr/lib - Libraries สำหรับ /usr/bin และ /usr/sbin
  • /usr/local - โปรแกรมที่คุณติดตั้งจาก Source เอง (เช่น Apache จาก Source จะอยู่ที่ /usr/local/apache2)
  • /usr/share - Architecture-independent data (man pages, icons, fonts)

10. /home – Home Directories

  • Home directories ของผู้ใช้ทั่วไป เก็บไฟล์ส่วนตัว
  • ตัวอย่าง: /home/john, /home/nikita

11. /boot – Boot Loader Files

  • เก็บไฟล์เกี่ยวกับ Boot Loader
  • Kernel initrd, vmlinux, GRUB files
  • ตัวอย่าง: initrd.img-5.15.0-91-generic, vmlinuz-5.15.0-91-generic, grub.cfg

12. /lib – System Libraries (ไลบรารีระบบ)

  • Library files ที่รองรับ Binaries ใน /bin และ /sbin
  • ชื่อไฟล์เป็น ld* หรือ lib*.so.*
  • ตัวอย่าง: ld-linux-x86-64.so.2, libc.so.6, libm.so.6
  • บนระบบ 64-bit มักมี /lib64 และ /usr/lib64 ด้วย

13. /opt – Optional Add-on Applications

  • opt = optional
  • เก็บ Add-on applications จาก Vendor ต่างๆ
  • ควรติดตั้งภายใต้ /opt/ หรือ Sub-directory เช่น /opt/google/chrome, /opt/lampp

14. /mnt – Mount Directory

  • Temporary mount point สำหรับ Sysadmin mount filesystem ชั่วคราว
  • เช่น mount NFS share, ISO ชั่วคราว

15. /media – Removable Media Devices

  • Temporary mount point สำหรับ Removable devices (อุปกรณ์ถอดได้)
  • ระบบ Auto-mount ที่นี่ เช่น /media/cdrom, /media/usb, /media/user/USB_DRIVE

16. /srv – Service Data

  • srv = service
  • เก็บข้อมูลเฉพาะ Service ของเซิร์ฟเวอร์
  • ตัวอย่าง: /srv/cvs, /srv/www, /srv/ftp, /srv/git

สรุปความแตกต่างสำคัญ: /bin vs /sbin vs /usr/bin vs /usr/sbin

Directoryเนื้อหาผู้ใช้งานเมื่อไหร่ต้องใช้
/binEssential user commandsAll usersSingle-user mode, Basic system operation
/sbinEssential system admin commandsRoot/AdminSystem maintenance, Repair
/usr/binNon-essential user commandsAll usersNormal operation, Applications
/usr/sbinNon-essential system admin commandsRoot/AdminService daemons, Admin tools

กฎทั่วไป: หาคำสั่งใน /bin ก่อน → ไม่เจอหา /usr/bin → หาคำสั่ง Admin ใน /sbin ก่อน → ไม่เจอหา /usr/sbin

บทความที่เกี่ยวข้องและน่าสนใจ

  • 15 ตัวอย่างคำสั่ง Linux Find Command ใช้งานจริง
  • 15 ตัวอย่าง Linux Command Line History
  • 15 ตัวอย่างคำสั่ง Unix LS Command
  • 15 ตัวอย่าง Grep Command ใช้งานจริง