คำสั่ง Command Line พื้นฐาน และตัวอย่างการใช้คำสั่งต่างๆ

Mr.Rukpong
Arcadia Software Development
3 min readDec 10, 2018

บทความนี้จะรวมคำสั่งพื้นฐาน Command Line ในระบบปฏิบัติการ Windows ที่ใช้บ่อย และตัวอย่างการใช้งานครับ

เริ่มต้นด้วยการกด start windows ขึ้นมา และกดพิมพ์ cmd ในช่อง search หรือสำหรับ windows 8 ขึ้นไป สามารถกดปุ่ม windows ที่ keyboard แล้วสามารถพิมพ์ cmd ได้เลยครับ windows จะ search หา โปรแกรมให้เลยทันที

หลังจากเจอโปรแกรม Command Line แล้ว เลือกเพื่อเริ่มใช้งาน จะมีหน้าจอดำๆ ขึ้นมา แปลว่าพร้อมใช้งานแล้วครับ เราเริ่มกันเลย

Command Line

1 » cd (Displays the name of or changes the current directory)

C:\Users\User>cd
  • พิมพ์คำสั่ง cd เพื่อแสดง Current Directory
  • พิมพ์คำสั่ง cd ตามด้วย Path จะเปลี่ยน Directory ปัจจุบันไปยัง Path นั้น เช่น
C:\Users\User>cd Downloads
C:\Users\User\Downloads>

หมายถึง เปลี่ยน Directory ไปยัง Downloads จาก Directory ปัจจุบัน

C:\Users\User\Downloads>cd ..
C:\Users\User>
  • พิมพ์คำสั่ง cd .. จะย้อน Directory กลับขึ้นไปหนึ่งขั้น
C:\Users\User>cd \
C:\>
  • พิมพ์คำสั่ง cd \ จะทำให้กลับสู่ Root Directory

** หากต้องการเปลี่ยน Directory ไปอีก Root หนึ่ง เช่นจาก C:\ เปลี่ยนเป็น D:\ จะต้องพิมพ์ตามนี้ก่อนครับ

C:\Users\User>D:

และจะใช้คำสั่ง cd ได้ตามปกติ

D:\>

2 » mkdir (Creates a directory)

C:\Users\User>mkdir tempFolder
  • พิมพ์คำสั่ง mkdir ตามด้วยชื่อ Directory (หรือชื่อ Folder อย่างที่เราคุ้นเคยกัน) จะเป็นการสร้าง Directory ใหม่ขึ้นมา

3 » dir (Displays a list of files and subdirectories in a directory)

C:\Users\User>dir
  • พิมพ์คำสั่ง dir เพื่อแสดงรายละเอียด File และ Directory ที่มีอยู่ใน Path ปัจจุบัน

** สามารถเพิ่ม tag /b เพื่อให้แสดงเฉพาะชื่อ File หรือชื่อ Directory ได้

C:\Users\User>dir /b

4 » echo text > file (Create file or insert some text)

C:\Users\User>echo some text for insert into file > tempFolder\newFile.txt

คำสั่ง echo เป็นคำสั่งแสดงข้อความที่ต่อจากคำสั่ง echo

เครื่องหมาย > กำกับ หมายถึงใส่ข้อความลงไปใน Path\File ที่กำหนด

5 » type (Displays the contents of a text file)

C:\Users\User>type tempFolder\newFile.txt
some text for insert into file
C:\Users\User>
  • พิมพ์คำสั่ง type ตามด้วย Path\File เพื่อแสดงข้อความที่อยู่ใน File นั้นๆ

6 » echo text >> file (Create file or Append some text into file)

C:\Users\User>echo new some text for append new line >> tempFolder\newFile.txt

เครื่องหมาย >> กำกับ หมายถึงใส่ข้อความเพิ่มลงไปใน Path\File ที่กำหนด

  • พิมพ์คำสั่ง type เพื่อดูข้อความ
C:\Users\User>type tempFolder\newFile.txt
some text for insert into file
new some text for append new line
C:\Users\User>

7 » ren (Renames a file or files)

C:\Users\User>ren tempFolder newNameFolder
  • พิมพ์คำสั่ง ren ตามด้วย File หรือ Directory ที่ต้องการเปลี่ยนชื่อ ตามด้วยชื่อใหม่ที่ต้องการเปลี่ยน จะเป็นการเปลี่ยนชื่อ File หรือ Directory นั้นๆ

8 » copy (Copies one or more files to another location)

C:\Users\User>copy newNameFolder\newFile.txt newNameFolder\newFileCopy.txt
  • พิมพ์คำสั่ง copy ตามด้วย File ตามด้วยชื่อ File ใหม่ที่ต้องการ copy
C:\Users\User>cd newNameFolder
C:\Users\User\newNameFolder>dir /b
newFile.txt
newfileCopy.txt
C:\Users\User\newNameFolder>
  • พิมพ์คำสั่ง dir \b เพื่อเช็ค File ใน Directory newNameFolder จะพบว่ามี File ที่เราสั่ง copy ปรากฏอยู่

9 » move (Moves one or more files from one directory to another
directory)

C:\Users\User>move newNameFolder\newFileCopy.txt Desktop\newFileCopy.txt
  • พิมพ์คำสั่ง move ตามด้วย File ตามด้วย Path ปลายทางและชื่อ File (อาจจะตั้งชื่อใหม่ไปด้วยก็ได้)
C:\Users\User>move newNameFolder Desktop\newNameFolder
  • พิมพ์คำสั่ง move ตามด้วย Directory ตามด้วย Path ปลายทางและชื่อ Directory (อาจจะตั้งชื่อใหม่ไปด้วยก็ได้)
C:\Users\User>cd Desktop
C:\Users\User\Desktop>dir /b
newNameFolder
newFileCopy.txt
C:\Users\User\Desktop>

เมื่อพิมพ์คำสั่ง cd เข้ามาที่ Directory Desktop ที่เราย้าย File มา จะพบทั้ง File ที่เราย้ายมา และ Directory ที่เราย้ายมา

10 » del (Deletes one or more files)

C:\Users\User\Desktop>del newFileCopy.txt
  • พิมพ์คำสั่ง del ตามด้วยชื่อ File จะเป็นการลบ File นั้นๆ

11 » rmdir (Removes a directory)

C:\Users\User\Desktop>rmdir newNameFolder
The directory is not empty.
C:\Users\User\Desktop>

หลังจากใช้คำสั่ง rmdir เพื่อลบ Directory จะพบว่าไม่สามารถลบได้เนื่องจาก Directory มี File อยู่ข้างใน เราสามารถบังคับลบ Directory ได้ด้วยคำสั่งดังนี้

C:\Users\User\Desktop>rmdir /s newNameFolder
newNameFolder, Are you sure (Y/N)?

จะมีข้อความให้ยืนยันการลบ Directory อีกที เมื่อเราพิมพ์ y ไปจะสามารถลบ Directory นั้นๆ ได้

** คำสั่งลบ File หรือ Directory หมายถึงการลบอย่างถาวร และไม่อยู่ใน Recycle Bin ด้วย เวลาใช้คำสั่งลบ File หรือ Directory โปรดระวัง!!!

แรกๆ อาจจะใช้งานได้ไม่คล่องนัก แต่ลองฝึกฝนใช้งานบ่อยๆ ก็จะใช้งานจะจำคำสั่งพื้นฐานเหล่านี้ได้ครับ สามารถนำไปต่อยอดในการเขียน Batch Script ได้ ซึ่งจะพูดในบทความต่อๆ ไป

  • คำสั่งต่างๆ สามารถดูวิธีใช้ ได้ด้วยการพิมพ์ /? ตามท้ายคำสั่ง เช่นต้องการจะดูวิธีใช้คำสั่ง rmdir ให้พิมพ์ rmdir /? จะแสดงรายละเอียดการใช้งานทั้งหมดเป็นต้น
C:\Users\User>rmdir /?
Removes (deletes) a directory.
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
/Q Quiet mode, do not ask if ok to remove a directory tree with /SC:\Users\User>
  • ถ้าสนใจและอยากทดลองใช้คำสั่ง Command Line อื่นๆ สามารถดูเพิ่มเติมได้ ด้วยการรันคำสั่งดังนี้
C:\Users\User\>help

--

--