UNIX command

พื้นฐานการใช้งานคำสั่งบนระบบ UNIX (Basic UNIX commands)

คำสั่งพื้นฐานสำหรับการใช้งานบนระบบ UNIX ไม่ว่าจะเป็น FreeBSD หรือ Linux หรืออื่นๆ จำเป็นต้องใช้คำสั่งเพื่อควบคุมการทำงานของเครื่องแม่ข่ายดังกล่าว ผ่าน shell บางคำสั่งของ UNIX Command อาจจำเป็นต้องใช้ผู้ใช้งานระดับสูงสุดที่เรียกว่า root เพราะเป็นคำสั่งที่อันตรายเนื่องจากเกี่ยวข้องกับระบบไฟล์ (file system) หรือระบบรักษาความปลอดภัย (security) ในบทความนี้จะอธิบายคำสั่งพื้นฐานที่สำคัญ ที่ผู้ดูแลระบบจำเป็นต้องใช้งานบ่อยๆ (คำสั่งบนระบบ UNIX อักษรพิมพ์เล็กและพิมพ์ใหญ่ ถือว่าเป็นคนละตัว ดังนั้นเวลาใช้จะต้องตรวจสอบให้ดี) โดยแยกเป็นกลุ่มคำสั่ง มีรายละเอียดดังต่อไปนี้

คำสั่งที่เกี่ยวข้องกับไฟล์ (file system commands)

  • ls ใช้สำหรับแสดงรายการไฟล์ ณ ตำแหน่งไดเร็กทอรี่ใดๆ (lists of files)
  • more filename ใช้สำหรับแสดงเนื้อหาไฟล์ครั้งละ 1 จอภาพต่อหนึ่งครั้งการแสดงผล (กรณีที่มีไฟล์ข้อมูลจำนวนมาก การมองอาจเป็นไปได้ยาก จึงจำเป็นต้องหยุดดูครั้งละ 1 หน้าจอ กด Enter เพื่อเลื่อนครั้งละ 1 บรรทัด หรือกด Space bar เพื่อเลื่อนไปยังหน้าจอถัดไป หรือใช้ร่วมกับคำสั่งอื่นๆ เช่น ls | more เป็นต้น)
  • mv filename1 filename2 ใช้สำหรับเปลี่ยนชื่อไฟล์จากชื่อหนึ่งไปเป็นอีกชื่อหนึ่ง นอกจากนั้นยังสามารถใช้ในการเปลี่ยนตำแหน่งของไฟล์ใดๆ ไปยังไดเร็อทอรี่อื่นๆ ตามที่ระบุ ปลายทาง เช่น mv /etc/test /root/test2 เป็นต้น (ความหมาย คือ ย้ายไฟล์ชื่อ test จากไดเร็กทอรี่ etc ไปยังไดเร็กทอรี่ root พร้อมทั้งเปลี่ยนชื่อเป็น test2)
  • cp filename1 filename2 ใช้สำหรับคัดลอกไฟล์
  • rm filename ใช้สำหรับลบไฟล์
  • diff filename1 filename2 ใช้สำหรับเปรียบเทียบไฟล์ 2 ไฟล์ และแสดงสิ่งที่แตกต่างกัน
  • wc filename ใช้สำหรับบอกจำนวนบรรทัด จำนวนคำ และจำนวนตัวอักษรในไฟล์ข้อมูล
  • chmod options filename ใช้สำหรับเปลี่ยนสิทธิ์ในการ อ่าน เขียน และ execute file หรือ ไดเร็กทอรี โดยแบ่งเป็น 3 สิทธิ์ ประกอบด้วย 1) เจ้าของไฟล์ file owner 2) กลุ่มของเจ้าของไฟล์ (group owner) และ 3) ผู้ใช้งานคนอื่นๆ (other user) ซึ่งคำสั่งจะถูกแปลงจากเลขฐาน 8 ในการระบุสิทธิ์แต่ชนิด
  • คำสั่งสำหรับการบีบอัดไฟล์ (File Compression)
    tar options filename
    โครงสร้างคำสั่ง
    tar [-] A –catenate –concatenate | c –create | d –diff –compare | –delete | r –append |
    t –list | –test-label | u –update | x –extract –get [options] [pathname …]
    1. สร้าง archive file จาก file ข้อมูล
    option -c ระบุว่าสร้าง archive file ใหม่
    option -f ระบุว่าตัวแปรต่อไปคือชื่อ file archive
    $ tar -cf archive.tar file1 file22. แสดงข้อมูลภายใน archive file
    option -t ใช้ในการแสดง list ข้อมูล
    option -v ระบุว่าแสดงข้อมูลแบบละเอียด
    option -f ระบุว่าตัวแปรต่อไปคือชื่อ file archive
    $ tar -tvf archive.tar3. แตก file ข้อมูลที่ archive
    option -x ระบุว่าทำการแตกข้อมูล file ออกมา
    option -f ระบุว่าตัวแปรต่อไปคือชื่อ file archive
    $ tar -xf archive.tar4. แตก file ข้อมูลที่ archive ที่มีการ zip
    option -x ระบุว่าทำการแตกข้อมูล file ออกมา
    option -z ระบุว่า file มีการ zip
    option -v ระบุว่าแสดงข้อมูลแบบละเอียด
    option -f ระบุว่าตัวแปรต่อไปคือชื่อ file archive
    $ tar -xzvf archive.tar.gz5. สร้าง archive file จาก directory
    $ tar -cf archive.tar mydir/6. สร้าง archive file แบบ zip จาก directory
    $ tar -czf archive.tar.gz mydir/7. แตก arhive file แบบ zip ไปยัง direcory ปัจจุบัน
    $ tar -zxvf myfile.tar.gz8. แตก archive file โดยระบุเฉพาะ file ข้างในที่ต้องการ
    $ tar -xvf archive.tar documents/work/budget.doc
  • คำสั่งสำหรับการพิมพ์ (printing)
    lpr filename ใช้สำหรับการพิมพ์เอกสาร โดยสามารถใช้ร่วมกับ -P option เพื่อระบุชื่อของเครื่องพิมพ์ แต่ถ้ามีการกำหนดเครื่องพิมพ์ไว้ก่อนหน้าแล้ว (default printer) ก็ไม่จำเป็นต้องระบุชื่อของเครื่องพิมพ์
    lpq ใช้สำหรับตรวจสอบคิวงานของเครื่องพิมพ์
    lprm jobnumber ใช้สำหรับลบหรือยกเลิกคิวงานของเครื่องพิมพ์ โดยจะต้องทราบคิวงานของเครื่องพิมพ์นั้นๆ ก่อน สามารถใช้คำสั่ง lpq เพื่อดูหมายเลขงานของคิวที่จะลบหรือยกเลิก
    genscript ใช้สำหรับแปลงไฟล์ข้อความ (plain text files) เป็นไฟล์ชนิด postscript เพื่อให้ง่ายสำหรับการพิมพ์

คำสั่งที่เกี่ยวข้องกับไดเร็กทอรี (Directories commands)

ไดเร็กทอรี หรือ โฟลเดอร์ บนระบบปฏิบัติการ Macintosh หรือ MS-Windows ใช้เพื่อจัดกลุ่มไฟล์เข้าด้วยกันเป็นหมวดหมู่ในลักษณะโครงสร้างแบบลำดับชั้น โดยมีคำสั่งที่เกี่ยวข้องดังต่อไปนี้

  • mkdir dirname ใช้สำหรับสร้างไดเร็กทอรีใหม่
  • cd dirname ใช้สำหรับเปลี่ยนไดเร็กทอรีปัจจุบัน หากไม่ได้ระบุพารามิเตอร์ของไดเร็กทอรีปลายทาง การใช้ cd จะหมายถึงการทำงาน ณ ไดเร็กทอรีปัจจุบันหรือตำแหน่งของไดเร็กทอรีของผู้ใช้ แต่หากระบุชื่อไดเร็กทอรีปลายทาง ชื่อนั้นก็จะกลายเป็นไดเร็กทอรีการทำงาน (ทั้งนี้ผู้ใช้จะต้องมีสิทธิ์ในการเข้าถึงไดเร็กทอรีน้นๆ ด้วย)
  • pwd ใช้สำหรับบอกชื่อไดเร็กทอรีปัจจุบันที่กำลังเข้าใช้งานอยู่

คำสั่งที่ใช้สำหรับการค้นหา (Finding commands)

  • find ใช้สำหรับการค้นหาไฟล์หรือไดเร็กทอรี ตัวอย่างเช่น เมื่อต้องการค้นหาไฟล์ทั้งหมดในระบบ ที่มีชื่อว่า .profile ให้พิมพ์ดังต่อไปนี้
    $ find / -name .profile
    โดยเครื่องหมาย / แทนตำแหน่งของไดเร็กทอรีที่จะเริ่มค้นหา
  • grep string filename(s) ใช้สำหรับการค้นหาข้อความในไฟล์เอกสารต่างๆ
    โครงสร้างคำสั่ง
    grep [OPTIONS] PATTERN [FILE…]
    grep [OPTIONS] [-e PATTERN]… [-f FILE]… [FILE…]
    1. ค้นหาบรรทัดที่มี text ตรงเงือนไข
    grep <คำที่จะใช้คำหา> <ชื่อไฟล์เอกสาร>
    $ grep a test1
    Cat
    Man$ grep an test1
    Man2. ค้นหาบรรทัดที่มี text ตรงเงื่อนไข โดยไม่สนใจตัวเล็กตัวใหญ่
    grep -i <คำที่จะใช้คำหา> <ชื่อไฟล์เอกสาร>$ grep -i “a” test1
    Ant
    Cat
    Man3. ค้นหาบรรทัดที่ไม่มี text ตามเงื่อนไข
    grep -v <คำที่จะใช้คำหา> <ชื่อไฟล์เอกสาร>

    $ grep -vi “a” test1
    Ant
    Bee
    Dog
    Fly

    รายละเอียดเพิ่มเติม
    โดยสรุปคำสั่ง grep เป็นคำสั่งที่ใช้ค้นหาคำในไฟล์เอกสาร ที่ตรงเงื่อนไข และมีการพัฒนาต่อยอดเพื่อให้สามารถทำงานกับ expression ที่ซับซ้อนยิ่งขึ้น เช่น egrep, fgrep และ pgrep เป็นต้น

    คำสั่งที่เกี่ยวข้องกับผู้ใช้งาน (About other people)

  • w ใช้สำหรับแสดงว่าใครเข้าสู่ระบบ (logged in) และกำลังใช้งานอะไรอยู่
  • who ใช้แสดงว่าใครกำลังใช้งานอยู่บ้างในขณะนี้
  • finger username ใช้สำหรับแสดงชื่อผู้ใช้ที่กำลังเข้ามาในระบบ หรือในบางระบบมีการใส่เบอร์โทรศัพท์ ที่อยู่ ดังนั้นเมื่อใช้คำสั่ง finger ก็จะได้ข้อมูลสารสนเทศของผู้ใช้นั้นๆ ด้วย
  • last -1 username ใช้สำหรับแสดงการเข้าสู่ระบบและออกจากระบบของผู้ใช้ในครั้งล่าสุด หากไม่ระบุชื่อผู้ใช้จะแสดงรายการของผู้ใช้ทั้งหมด
  • talk username — lets you have a (typed) conversation with another user
    write username — lets you exchange one-line messages with another user
    elm — lets you send e-mail messages to people around the world (and, of course, read them). It’s not the only mailer you can use, but the one we recommend. See the elm page, and find out about the departmental mailing lists (which you can also find in /user/linguistics/helpfile).
    About your (electronic) self
    whoami — returns your username. Sounds useless, but isn’t. You may need to find out who it is who forgot to log out somewhere, and make sure *you* have logged out.
    finger & .plan files
    of course you can finger yourself, too. That can be useful e.g. as a quick check whether you got new mail. Try to create a useful .plan file soon. Look at other people’s .plan files for ideas. The file needs to be readable for everyone in order to be visible through ‘finger’. Do ‘chmod a+r .plan’ if necessary. You should realize that this information is accessible from anywhere in the world, not just to other people on turing.
    passwd — lets you change your password, which you should do regularly (at least once a year). See the LRB guide and/or look at help password.
    ps -u yourusername — lists your processes. Contains lots of information about them, including the process ID, which you need if you have to kill a process. Normally, when you have been kicked out of a dialin session or have otherwise managed to get yourself disconnected abruptly, this list will contain the processes you need to kill. Those may include the shell (tcsh or whatever you’re using), and anything you were running, for example emacs or elm. Be careful not to kill your current shell – the one with the number closer to the one of the ps command you’re currently running. But if it happens, don’t panic. Just try again 🙂 If you’re using an X-display you may have to kill some X processes before you can start them again. These will show only when you use ps -efl, because they’re root processes.
    kill PID — kills (ends) the processes with the ID you gave. This works only for your own processes, of course. Get the ID by using ps. If the process doesn’t ‘die’ properly, use the option -9. But attempt without that option first, because it doesn’t give the process a chance to finish possibly important business before dying. You may need to kill processes for example if your modem connection was interrupted and you didn’t get logged out properly, which sometimes happens.
    quota -v — show what your disk quota is (i.e. how much space you have to store files), how much you’re actually using, and in case you’ve exceeded your quota (which you’ll be given an automatic warning about by the system) how much time you have left to sort them out (by deleting or gzipping some, or moving them to your own computer).
    du filename — shows the disk usage of the files and directories in filename (without argument the current directory is used). du -s gives only a total.
    last yourusername — lists your last logins. Can be a useful memory aid for when you were where, how long you’ve been working for, and keeping track of your phonebill if you’re making a non-local phonecall for dialling in.
    Connecting to the outside world
    nn — allows you to read news. It will first let you read the news local to turing, and then the remote news. If you want to read only the local or remote news, you can use nnl or nnr, respectively. To learn more about nn type nn, then \tty{:man}, then \tty{=.*}, then \tty{Z}, then hit the space bar to step through the manual. Or look at the man page. Or check out the hypertext nn FAQ – probably the easiest and most fun way to go.
    rlogin hostname — lets you connect to a remote host
    telnet hostname — also lets you connect to a remote host. Use rlogin whenever possible.
    ftp hostname — lets you download files from a remote host which is set up as an ftp-server. This is a common method for exchanging academic papers and drafts. If you need to make a paper of yours available in this way, you can (temporarily) put a copy in /user/ftp/pub/TMP. For more permanent solutions, ask Emma. The most important commands within ftp are get for getting files from the remote machine, and put for putting them there (mget and mput let you specify more than one file at once). Sounds straightforward, but be sure not to confuse the two, especially when your physical location doesn’t correspond to the direction of the ftp connection you’re making. ftp just overwrites files with the same filename. If you’re transferring anything other than ASCII text, use binary mode.
    lynx — lets you browse the web from an ordinary terminal. Of course you can see only the text, not the pictures. You can type any URL as an argument to the G command. When you’re doing this from any Stanford host you can leave out the .stanford.edu part of the URL when connecting to Stanford URLs. Type H at any time to learn more about lynx, and Q to exit.
    Miscellaneous tools
    webster word — looks up the word in an electronic version of Webster’s dictionary and returns the definition(s)
    date — shows the current date and time.
    cal — shows a calendar of the current month. Use e.g., ‘cal 10 1995’ to get that for October 95, or ‘cal 1995’ to get the whole year.
    You can find out more about these commands by looking up their manpages:
    man commandname — shows you the manual page for the command
    For further ways of obtaining help, look at the pages with electronic sources of information and non-electronic sources.

แปลและเรียบเรียงโดย ผศ.ปริญญา น้อยดอนไพร

ที่มา:
https://mally.stanford.edu/~sr/computing/basic-unix.html
httpss://saixiii.com/tar-linux-command/
httpss://saixiii.com/grep-linux-command/
https://www.jobpub.com/articles/showarticle.asp?id=89