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

Top 50 คำสั่ง Unix พื้นฐาน: คู่มือปฏิบัติจริง

Top 50 คำสั่ง Unix พื้นฐาน: คู่มือปฏิบัติจริงบทความนี้ให้ตัวอย่างปฏิบัติจริงสำหรับ 50 คำสั่งที่ใช้บ่อยที่สุดใน Linux / UNIX

นี่ไม่ใช่รายการที่ครบครันแน่นอน แต่ควรให้คุณ Jumpstart บนคำสั่ง Linux ทั่วไปบางส่วน Bookmark บทความนี้ไว้สำหรับอ้างอิงในอนาคต

ผมพลาดคำสั่ง Linux ที่ใช้บ่อยไหม? ทิ้งความคิดเห็นบอกผมได้เลย

1. ตัวอย่างคำสั่ง tar

สร้าง Tar Archive ใหม่

$ tar cvf archive_name.tar dirname/ 

Extract จาก Tar Archive ที่มีอยู่

$ tar xvf archive_name.tar 

ดู Tar Archive ที่มีอยู่

$ tar tvf archive_name.tar 

ตัวอย่าง tar เพิ่มเติม: The Ultimate Tar Command Tutorial with 10 Practical Examples

2. ตัวอย่างคำสั่ง grep

ค้นหาสตริงที่กำหนดในไฟล์ (Case Insensitive Search)

$ grep -i "the" demo_file 

พิมพ์บรรทัดที่ตรง พร้อม 3 บรรทัดหลังจากนั้น

$ grep -A 3 -i "example" demo_text 

ค้นหาสตริงที่กำหนดในไฟล์ทั้งหมดแบบ Recursive

$ grep -r "ramesh" * 

ตัวอย่าง grep เพิ่มเติม: Get a Grip on the Grep! – 15 Practical Grep Command Examples

3. ตัวอย่างคำสั่ง find

ค้นหาไฟล์ใช้ชื่อไฟล์ (Case Insensitive Find)

# find -iname "MyCProgram.c" 

รันคำสั่งบนไฟล์ที่ Find ค้นพบ

$ find -iname "MyCProgram.c" -exec md5sum {} \; 

ค้นหาไฟล์ว่างทั้งหมดใน Home Directory

# find ~ -empty 

ตัวอย่าง find เพิ่มเติม: Mommy, I found it! — 15 Practical Linux Find Command Examples

4. ตัวอย่างคำสั่ง ssh

Login เข้า Remote Host

ssh -l jsmith remotehost.example.com 

Debug SSH Client

ssh -v -l jsmith remotehost.example.com 

แสดง SSH Client Version

$ ssh -V OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003 

ตัวอย่าง ssh เพิ่มเติม: 5 Basic Linux SSH Client Commands

5. ตัวอย่างคำสั่ง sed

เมื่อคุณคัดลอกไฟล์ DOS ไป Unix คุณอาจพบ \r\n ท้ายทุกบรรทัด ตัวอย่างนี้แปลงรูปแบบไฟล์ DOS เป็น Unix โดยใช้คำสั่ง sed

$sed 's/.$//' filename 

พิมพ์เนื้อหาไฟล์ในลำดับย้อนกลับ

$ sed -n '1!G;h;$p' thegeekstuff.txt 

เพิ่มหมายเลขบรรทัดสำหรับบรรทัดที่ไม่ใช่ Empty Lines ทั้งหมดในไฟล์

$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /' 

ตัวอย่าง sed เพิ่มเติม: Advanced Sed Substitution Examples

6. ตัวอย่างคำสั่ง awk

ลบบรรทัดซ้ำด้วย awk

$ awk '!($0 in array) { array[$0]; print }' temp 

พิมพ์ทุกบรรทัดจาก /etc/passwd ที่มี UID และ GID เหมือนกัน

$awk -F ':' '$3==$4' passwd.txt 

พิมพ์เฉพาะ Field ที่กำหนดจากไฟล์

$ awk '{print $2,$5;}' employee.txt 

ตัวอย่าง awk เพิ่มเติม: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

7. ตัวอย่างคำสั่ง vim

ไปที่บรรทัดที่ 143 ของไฟล์

$ vim +143 filename.txt 

ไปที่ Match แรกของที่ระบุ

$ vim +/search-term filename.txt 

เปิดไฟล์ใน Read-only Mode

$ vim -R /etc/passwd 

ตัวอย่าง vim เพิ่มเติม: How To Record and Play in Vim Editor

8. ตัวอย่างคำสั่ง diff

ละเว้น White Space ขณะเปรียบเทียบ

# diff -w name_list.txt name_list_new.txt 2c2,3 < John Doe --- > John M Doe > Jason Bourne 

ตัวอย่าง diff เพิ่มเติม: Top 4 File Difference Tools on UNIX / Linux – Diff, Colordiff, Wdiff, Vimdiff

9. ตัวอย่างคำสั่ง sort

เรียงไฟล์ในลำดับ Ascending

$ sort names.txt 

เรียงไฟล์ในลำดับ Descending

$ sort -r names.txt 

เรียงไฟล์ passwd โดย Field ที่ 3

$ sort -t: -k 3n /etc/passwd | more 

10. ตัวอย่างคำสั่ง export

เพื่อดู Environment Variables ที่เกี่ยวกับ Oracle

$ export | grep ORACLE declare -x ORACLE_BASE="/u01/app/oracle" declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0" declare -x ORACLE_SID="med" declare -x ORACLE_TERM="xterm" 

เพื่อ Export Environment Variable:

$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0 

11. ตัวอย่างคำสั่ง xargs

คัดลอกรูปภาพทั้งหมดไปยัง External Hard Drive

# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory 

ค้นหารูปภาพ JPG ทั้งหมดในระบบและ Archive

# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz 

ดาวน์โหลด URL ทั้งหมดที่กล่าวถึงในไฟล์ url-list.txt

# cat url-list.txt | xargs wget –c 

12. ตัวอย่างคำสั่ง ls

แสดงขนาดไฟล์ในรูปแบบ Human Readable (เช่น KB, MB ฯลฯ)

$ ls -lh -rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz 

จัดลำดับไฟล์ตามเวลาแก้ไขล่าสุด (Reverse Order) ด้วย ls -ltr

$ ls -ltr 

การจัดประเภทไฟล์แบบ Visual ด้วย Special Characters ใช้ ls -F

$ ls -F 

ตัวอย่าง ls เพิ่มเติม: Unix LS Command: 15 Practical Examples

13. คำสั่ง pwd

pwd คือ Print Working Directory อะไรอีกที่จะพูดเกี่ยวกับ pwd ที่ดีเก่า ที่พิมพ์ชื่อ Directory ปัจจุบันมามาหลายทศวรรษแล้ว

14. ตัวอย่างคำสั่ง cd

ใช้ "cd -" เพื่อ Toggle ระหว่าง Directory สองอันสุดท้าย

ใช้ "shopt -s cdspell" เพื่อแก้ไขชื่อ Directory ที่พิมพ์ผิดโดยอัตโนมัติขณะ cd

ตัวอย่าง cd เพิ่มเติม: 6 Awesome Linux cd command Hacks

15. ตัวอย่างคำสั่ง gzip

เพื่อสร้างไฟล์ *.gz ที่บีบอัด:

$ gzip test.txt 

เพื่อ Uncompress ไฟล์ *.gz:

$ gzip -d test.txt.gz 

แสดงอัตราส่วนการบีบอัดของไฟล์ที่บีบอัดด้วย gzip -l

$ gzip -l *.gz compressed uncompressed ratio uncompressed_name 23709 97975 75.8% asp-patch-rpms.txt 

16. ตัวอย่างคำสั่ง bzip2

เพื่อสร้างไฟล์ *.bz2 ที่บีบอัด:

$ bzip2 test.txt 

เพื่อ Uncompress ไฟล์ *.bz2:

bzip2 -d test.txt.bz2 

ตัวอย่าง bzip2 เพิ่มเติม: BZ is Eazy! bzip2, bzgrep, bzcmp, bzdiff, bzcat, bzless, bzmore Examples

17. ตัวอย่างคำสั่ง unzip

เพื่อ Extract ไฟล์ *.zip ที่บีบอัด:

$ unzip test.zip 

ดูเนื้อหาของไฟล์ *.zip (โดยไม่ Unzip):

$ unzip -l jasper.zip Archive: jasper.zip Length Date Time Name -------- ---- ---- ---- 40995 11-30-98 23:50 META-INF/MANIFEST.MF 32169 08-25-98 21:07 classes_ 15964 08-25-98 21:07 classes_names 10542 08-25-98 21:07 classes_ncomp 

18. ตัวอย่างคำสั่ง shutdown

Shutdown ระบบและปิดพลังงานทันที

# shutdown -h now 

Shutdown ระบบหลัง 10 นาที

# shutdown -h +10 

Reboot ระบบด้วยคำสั่ง shutdown

# shutdown -r now 

บังคับ Filesystem Check ระหว่าง Reboot

# shutdown -Fr now 

19. ตัวอย่างคำสั่ง ftp

ทั้ง ftp และ secure ftp (sftp) มีคำสั่งคล้ายกัน เพื่อเชื่อมต่อ Remote Server และดาวน์โหลดหลายไฟล์ ทำดังนี้

$ ftp IP/hostname ftp> mget *.html 

เพื่อดูชื่อไฟล์บน Remote Server ก่อนดาวน์โหลด ใช้ mls ftp command ดังนี้

ftp> mls *.html - /ftptest/features.html /ftptest/index.html /ftptest/othertools.html /ftptest/samplereport.html /ftptest/usage.html 

ตัวอย่าง ftp เพิ่มเติม: FTP and SFTP Beginners Guide with 10 Examples

20. ตัวอย่างคำสั่ง crontab

ดู Crontab Entry สำหรับ User เฉพาะ

# crontab -u john -l 

กำหนด Cron Job ทุก 10 นาที

*/10 * * * * /home/ramesh/check-disk-space 

ตัวอย่าง crontab เพิ่มเติม: Linux Crontab: 15 Awesome Cron Job Examples

21. ตัวอย่างคำสั่ง service

คำสั่ง Service ใช้รัน System V Init Scripts คือ แทนที่จะเรียก Scripts ใน /etc/init.d/ ด้วย Full Path คุณสามารถใช้คำสั่ง Service ได้

ตรวจสอบสถานะ Service:

# service ssh status 

ตรวจสอบสถานะ Services ทั้งหมด

service --status-all 

Restart Service

# service ssh restart 

22. ตัวอย่างคำสั่ง ps

คำสั่ง ps ใช้แสดงข้อมูลเกี่ยวกับ Process ที่กำลังรันในระบบ

แม้จะมี Arguments หลายอย่างที่ส่งให้ ps command ได้ ต่อไปนี้คือบางอย่างที่พบบ่อย

เพื่อดู Process ที่กำลังรันอยู่

$ ps -ef | more 

เพื่อดู Process ที่กำลังรันอยู่ในโครงสร้าง Tree Option H ยืนยัน Process Hierarchy

$ ps -efH | more 

23. ตัวอย่างคำสั่ง free

คำสั่งนี้ใช้แสดง Free, Used, Swap Memory ที่มีในระบบ

Output คำสั่ง free ปกติ Output แสดงเป็น Bytes

$ free total used free shared buffers cached Mem: 3566408 1580220 1986188 0 203988 902960 -/+ buffers/cache: 473272 3093136 Swap: 4000176 0 4000176 

หากคุณต้องการตรวจสอบเร็วว่า RAM ของระบบมีกี่ GB ใช้ Option -g -b แสดงเป็น Bytes -k เป็น Kilo Bytes -m เป็น Mega Bytes

$ free -g total used free shared buffers cached Mem: 3 1 1 0 0 0 -/+ buffers/cache: 0 2 Swap: 3 0 3 

หากคุณต้องการดู Total Memory (รวม Swap) ใช้ Switch -t ซึ่งจะแสดง Total Line ดังนี้

ramesh@ramesh-laptop:~$ free -t total used free shared buffers cached Mem: 3566408 1592148 1974260 0 204260 912556 -/+ buffers/cache: 475332 3091076 Swap: 4000176 0 4000176 Total: 7566584 1592148 5974436 

24. ตัวอย่างคำสั่ง top

คำสั่ง top แสดง Top Processes ในระบบ (Default เรียงตาม CPU Usage) เพื่อเรียง Top Output ตาม Column ใดๆ กด O (Upper-case O) จะแสดง Column ทั้งหมดที่คุณสามารถเรียงได้ ดังนี้

Current Sort Field: P for window 1:Def Select sort field via field letter, type any other key to return a: PID = Process Id v: nDRT = Dirty Pages count d: UID = User Id y: WCHAN = Sleeping in Function e: USER = User Name z: Flags = Task Flags ........ 

เพื่อแสดงเฉพาะ Process ที่เป็นของ User เฉพาะ ใช้ Option -u ตัวอย่างต่อไปจะแสดงเฉพาะ Top Processes ของ User Oracle

$ top -u oracle 

ตัวอย่าง top เพิ่มเติม: Can You Top This? 15 Practical Linux Top Command Examples

25. ตัวอย่างคำสั่ง df

แสดงการใช้พื้นที่ดิสก์ของ File System Default df -k แสดง Output เป็น Bytes

$ df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 29530400 3233104 24797232 12% / /dev/sda2 120367992 50171596 64082060 44% /home 

df -h แสดง Output ในรูปแบบ Human Readable คือ Size จะแสดงเป็น GB

ramesh@ramesh-laptop:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 29G 3.1G 24G 12% / /dev/sda2 115G 48G 62G 44% /home 

ใช้ Option -T เพื่อแสดง Type ของ File System

ramesh@ramesh-laptop:~$ df -T Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/sda1 ext4 29530400 3233120 24797216 12% / /dev/sda2 ext4 120367992 50171596 64082060 44% /home 

26. ตัวอย่างคำสั่ง kill

ใช้คำสั่ง Kill เพื่อ Terminate Process ก่อนได้ Process ID ด้วย ps -ef แล้วใช้ kill -9 เพื่อ Kill Linux Process ที่กำลังรัน ดังนี้ คุณยังสามารถใช้ killall, pkill, xkill เพื่อ Terminate Unix Process ได้

$ ps -ef | grep vim ramesh 7243 7222 9 22:43 pts/2 00:00:00 vim $ kill -9 7243 

ตัวอย่าง kill เพิ่มเติม: 4 Ways to Kill a Process – kill, killall, pkill, xkill

27. ตัวอย่างคำสั่ง rm

ขอ Confirmation ก่อนลบไฟล์

$ rm -i filename.txt 

มีประโยชน์มากขณะให้ Shell Metacharacters ใน Argument ชื่อไฟล์

พิมพ์ชื่อไฟล์และขอ Confirmation ก่อนลบไฟล์

$ rm -i file* 

ตัวอย่างต่อไป ลบไฟล์และ Directory ทั้งหมดภายใต้ Directory Example แบบ Recursive นี่ยังลบ Directory Example เองด้วย

$ rm -r example 

28. ตัวอย่างคำสั่ง cp

Copy file1 ไป file2 พร้อม Preserve Mode, Ownership และ Timestamp

$ cp -p file1 file2 

Copy file1 ไป file2 หาก file2 มีอยู่ Prompt Confirmation ก่อน Overwrite

$ cp -i file1 file2 

29. ตัวอย่างคำสั่ง mv

Rename file1 เป็น file2 หาก file2 มีอยู่ Prompt Confirmation ก่อน Overwrite

$ mv -i file1 file2 

Note: mv -f คือตรงข้าม จะ Overwrite file2 โดยไม่ Prompt

mv -v จะพิมพ์ว่ากำลังเกิดอะไรขึ้นระหว่าง Rename ซึ่งมีประโยชน์ขณะระบุ Shell Metacharacters ใน Argument ชื่อไฟล์

$ mv -v file1 file2 

30. ตัวอย่างคำสั่ง cat

คุณสามารถดูหลายไฟล์พร้อมกันได้ ตัวอย่างต่อไปพิมพ์เนื้อหา file1 ตามด้วย file2 ไปยัง stdout

$ cat file1 file2 

ขณะแสดงไฟล์ คำสั่ง cat -n ต่อไปจะ Prepend หมายเลขบรรทัดให้แต่ละบรรทัดของ Output

$ cat -n /etc/logrotate.conf 1 /var/log/btmp { 2 missingok 3 monthly 4 create 0660 root utmp 5 rotate 1 6 } 

31. ตัวอย่างคำสั่ง mount

เพื่อ Mount File System คุณควรสร้าง Directory และ Mount มันดังนี้

# mkdir /u01 # mount /dev/sdb1 /u01 

คุณยังสามารถเพิ่มเข้า fstab สำหรับ Auto Mounting คือ ทุกครั้งที่ระบบ Restart Filesystem จะถูก Mount

/dev/sdb1 /u01 ext2 defaults 0 2 

32. ตัวอย่างคำสั่ง chmod

คำสั่ง chmod ใช้เปลี่ยน Permissions สำหรับไฟล์หรือ Directory

ให้ Full Access แก่ User และ Group (อ่าน เขียน รัน) บนไฟล์เฉพาะ

$ chmod ug+rwx file.txt 

เพิกถอน Access ทั้งหมดสำหรับ Group (อ่าน เขียน รัน) บนไฟล์เฉพาะ

$ chmod g-rwx file.txt 

Apply File Permissions แบบ Recursive ไปยังไฟล์ทั้งหมดใน Sub-directories

$ chmod -R ug+rwx file.txt 

ตัวอย่าง chmod เพิ่มเติม: 7 Chmod Command Examples for Beginners

33. ตัวอย่างคำสั่ง chown

คำสั่ง chown ใช้เปลี่ยน Owner และ Group ของไฟล์ \

เพื่อเปลี่ยน Owner เป็น Oracle และ Group เป็น DBA บนไฟล์ คือ เปลี่ยน Owner และ Group พร้อมกัน

$ chown oracle:dba dbora.sh 

ใช้ -R เพื่อเปลี่ยน Ownership แบบ Recursive

$ chown -R oracle:dba /home/oracle 

34. ตัวอย่างคำสั่ง passwd

เปลี่ยนรหัสผ่านจาก Command Line ด้วย passwd จะ Prompt รหัสผ่านเก่า ตามด้วยรหัสผ่านใหม่

$ passwd 

Super User สามารถใช้ passwd Reset รหัสผ่านของคนอื่นได้ จะไม่ Prompt รหัสผ่านปัจจุบันของ User

# passwd USERNAME 

ลบรหัสผ่านสำหรับ User เฉพาะ Root User สามารถ Disable รหัสผ่านสำหรับ User เฉพาะได้ หากรหัสผ่านถูก Disable User สามารถ Login ได้โดยไม่ต้องใส่รหัสผ่าน

# passwd -d USERNAME 

35. ตัวอย่างคำสั่ง mkdir

ตัวอย่างต่อไปสร้าง Directory ชื่อ Temp ภายใต้ Home Directory ของคุณ

$ mkdir ~/temp 

สร้าง Nested Directories ด้วยคำสั่ง mkdir เดียว หาก Directory ใดมีอยู่แล้ว จะไม่แสดง Error หาก Directory ใดไม่มีอยู่ จะสร้างมัน

$ mkdir -p dir1/dir2/dir3/dir4/ 

36. ตัวอย่างคำสั่ง ifconfig

ใช้คำสั่ง ifconfig เพื่อดูหรือ Config Network Interface บนระบบ Linux

ดู Interfaces ทั้งหมดพร้อม Status

$ ifconfig -a 

Start หรือ Stop Interface เฉพาะด้วย Up และ Down Command ดังนี้

$ ifconfig eth0 up $ ifconfig eth0 down 

ตัวอย่าง ifconfig เพิ่มเติม: Ifconfig: 7 Examples To Configure Network Interface

37. ตัวอย่างคำสั่ง uname

คำสั่ง Uname แสดงข้อมูลสำคัญเกี่ยวกับระบบ เช่น — Kernel Name, Host Name, Kernel Release Number, Processor Type, ฯลฯ

Sample Uname Output จาก Ubuntu Laptop แสดงด้านล่าง

$ uname -a Linux john-laptop 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux 

38. ตัวอย่างคำสั่ง whereis

เมื่อคุณต้องการหาว่า Unix Command เฉพาะอยู่ที่ไหน (เช่น ls command อยู่ที่ไหน?) คุณสามารถรันคำสั่งต่อไปนี้

$ whereis ls ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz 

เมื่อคุณต้องการค้นหา Executable จาก Path อื่นนอกจาก Whereis Default Path คุณสามารถใช้ Option -B และให้ Path เป็น Argument นี่ค้นหา Executable lsmk ใน /tmp Directory และแสดงหากมี

$ whereis -u -B /tmp -f lsmk lsmk: /tmp/lsmk 

39. ตัวอย่างคำสั่ง whatis

คำสั่ง Whatis แสดงคำอธิบายบรรทัดเดียวเกี่ยวกับคำสั่ง

$ whatis ls ls (1) - list directory contents $ whatis ifconfig ifconfig (8) - configure a network interface 

40. ตัวอย่างคำสั่ง locate

ใช้คำสั่ง Locate คุณสามารถค้นหาตำแหน่งของไฟล์เฉพาะ (หรือกลุ่มไฟล์) ได้อย่างรวดเร็ว Locate Command ใช้ Database ที่สร้างโดย Updatedb

ตัวอย่างด้านล่างแสดงไฟล์ทั้งหมดในระบบที่มีคำว่า Crontab

$ locate crontab /etc/anacrontab /etc/crontab /usr/bin/crontab /usr/share/doc/cron/examples/crontab2english.pl.gz /usr/share/man/man1/crontab.1.gz /usr/share/man/man5/anacrontab.5.gz /usr/share/man/man5/crontab.5.gz /usr/share/vim/vim72/syntax/crontab.vim 

41. ตัวอย่างคำสั่ง man

แสดง Man Page ของคำสั่งเฉพาะ

$ man crontab 

เมื่อ Man Page ของคำสั่งอยู่ภายใต้ Section มากกว่าหนึ่ง คุณสามารถดู Man Page สำหรับคำสั่งนั้นจาก Section เฉพาะได้ดังนี้

$ man SECTION-NUMBER commandname 

Section 8 ต่อไปนี้มีอยู่ใน Man Page

  1. General Commands
  2. System Calls
  3. C Library Functions
  4. Special Files (usually devices, those found in /dev) and Drivers
  5. File Formats and Conventions
  6. Games and Screensavers
  7. Miscellaneous
  8. System Administration Commands and Daemons

ตัวอย่าง เมื่อคุณทำ whatis crontab คุณจะสังเกตว่า crontab มี 2 Man Pages (Section 1 และ Section 5) เพื่อดู Section 5 ของ Crontab Man Page ทำดังนี้

$ whatis crontab crontab (1) - maintain crontab files for individual users (V3) crontab (5) - tables for driving cron $ man 5 crontab 

42. ตัวอย่างคำสั่ง tail

พิมพ์ 10 บรรทัดสุดท้ายของไฟล์ Default

$ tail filename.txt 

พิมพ์ N บรรทัดจากไฟล์ชื่อ filename.txt

$ tail -n N filename.txt 

ดูเนื้อหาไฟล์ Real-time ด้วย tail -f นี่มีประโยชน์สำหรับดู Log Files ที่เพิ่มขึ้นเรื่อยๆ คำสั่งสามารถ Terminate ด้วย CTRL-C

$ tail -f log-file 

ตัวอย่าง tail เพิ่มเติม: 3 Methods To View tail -f output of Multiple Log Files in One Terminal

43. ตัวอย่างคำสั่ง less

Less มีประสิทธิภาพมากขณะดู Log Files ขนาดใหญ่ เพราะไม่ต้อง Load Full File ขณะเปิด

$ less huge-log-file.log 

หนึ่งครั้งที่คุณเปิดไฟล์ด้วย Less Command คีย์สองตัวต่อไปมีประโยชน์มาก

CTRL+F – Forward one window CTRL+B – Backward one window 

ตัวอย่าง less เพิ่มเติม: Unix Less Command: 10 Tips for Effective Navigation

44. ตัวอย่างคำสั่ง su

สลับไปยัง User Account อื่นด้วยคำสั่ง su Super User สามารถสลับไป User อื่นได้โดยไม่ต้องใส่รหัสผ่านของพวกเขา

$ su - USERNAME 

รันคำสั่งเดียวจาก Account Name อื่น ในตัวอย่างต่อไป John สามารถรันคำสั่ง ls เป็น Username Raj ได้ หากคำสั่ง Executed แล้ว จะกลับไปที่ Account ของ John

[john@dev-server]$ su - raj -c 'ls' [john@dev-server]$ 

Login เข้า User Account ที่ระบุ และ Execute Shell ที่ระบุแทน Default Shell

$ su -s 'SHELLNAME' USERNAME 

45. ตัวอย่างคำสั่ง mysql

MySQL น่าจะเป็น Open Source Database ที่ใช้กันมากที่สุดบน Linux แม้คุณไม่ได้รัน MySQL Database บนเซิร์ฟเวอร์ของคุณ คุณอาจต้องใช้คำสั่ง MySQL (Client) เพื่อเชื่อมต่อ MySQL Database ที่รันบน Remote Server

เพื่อเชื่อมต่อ Remote MySQL Database จะ Prompt Password

$ mysql -u root -p -h 192.168.1.2 

เพื่อเชื่อมต่อ Local MySQL Database

$ mysql -u root -p 

หากคุณต้องการระบุ MySQL Root Password ใน Command Line เอง ใส่ทันทีหลัง -p (โดยไม่มี Space)

46. ตัวอย่างคำสั่ง yum

ติดตั้ง Apache ด้วย Yum

$ yum install httpd 

Upgrade Apache ด้วย Yum

$ yum update httpd 

Uninstall/Remove Apache ด้วย Yum

$ yum remove httpd 

47. ตัวอย่างคำสั่ง rpm

ติดตั้ง Apache ด้วย RPM

# rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm 

Upgrade Apache ด้วย RPM

# rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm 

Uninstall/Remove Apache ด้วย RPM

# rpm -ev httpd 

ตัวอย่าง rpm เพิ่มเติม: RPM Command: 15 Examples to Install, Uninstall, Upgrade, Query RPM Packages

48. ตัวอย่างคำสั่ง ping

Ping Remote Host โดยส่งเพียง 5 Packets

$ ping -c 5 gmail.com 

ตัวอย่าง ping เพิ่มเติม: Ping Tutorial: 15 Effective Ping Command Examples

49. ตัวอย่างคำสั่ง date

ตั้งค่า System Date:

# date -s "01/31/2010 23:59:53" 

หลังจากที่คุณเปลี่ยน System Date แล้ว คุณควร Sync Hardware Clock กับ System Date ดังนี้

# hwclock –systohc # hwclock --systohc –utc 

50. ตัวอย่างคำสั่ง wget

วิธีรวดเร็วและมีประสิทธิภาพในการดาวน์โหลด Software, Music, Video จากอินเทอร์เน็ต คือใช้คำสั่ง wget

$ wget https://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz 

ดาวน์โหลดและเก็บด้วยชื่ออื่น

$ wget -O taglist.zip https://www.vim.org/scripts/download_script.php?src_id=7701 

ตัวอย่าง wget เพิ่มเติม: The Ultimate Wget Download Guide With 15 Awesome Examples

ผมพลาดคำสั่ง Linux ที่ใช้บ่อยไหม? ทิ้งความคิดเห็นบอกผมได้เลย