บทช่วยสอนนี้อธิบายวิธีใช้คำสั่ง cd ใน Linux เพื่อเปลี่ยนไดเรกทอรีที่คุณอยู่ในขณะนี้ ภายใน shell
เราเคยครอบคลุมวิธีดูรอบๆ ในไดเรกทอรีด้วย ls แล้ว แต่ตอนนี้คุณต้องเริ่มนำทางรอบๆ ไดเรกทอรี เราจะทำได้ง่ายด้วย cd, change directory เริ่มด้วยสิ่งง่ายๆ เราเรียนรู้เพิ่มเติมเกี่ยวกับ utils แบบ built-in อย่าง cd อย่างไร? หากคุณลอง:
man cd
มันจะรายงาน "No manual entry for cd" น่าจะเป็นไปได้มาก แทนที่จะเป็น built-in utils ให้ใช้ help
help cd
ไวยากรณ์ CD
ตอนนี้เรารัน utility help ที่มีประโยชน์แล้ว เรามีเอาต์พุทดังนี้:
cd: cd [-L|[-P [-e]] [admin@wsxdn.com]] [dir] Change the shell working directory. Change the current directory to DIR. The default DIR is the value of the HOME shell variable. The variable CDPATH defines the search path for the directory containing DIR. Alternative directory names in CDPATH are separated by a colon (:). A null directory name is the same as the current directory. If DIR begins with a slash (/), then CDPATH is not used. If the directory is not found, and the shell option `cdable_vars' is set, the word is assumed to be a variable name. If that variable has a value, its value is used for DIR. Options: -L force symbolic links to be followed: resolve symbolic links in DIR after processing instances of `..' -P use the physical directory structure without following symbolic links: resolve symbolic links in DIR before processing instances of `..' -e if the -P option is supplied, and the current working directory cannot be determined successfully, exit with a non-zero status admin@wsxdn.com on systems that support it, present a file with extended attributes as a directory containing the file attributes The default is to follow symbolic links, as if `-L' were specified. `..' is processed by removing the immediately previous pathname component back to a slash or the beginning of DIR. Exit Status: Returns 0 if the directory is changed, and if $PWD is set successfully when -P is used; non-zero otherwise.
老实说? นั่นคือครั้งแรกที่ฉันดูผ่านการพิมพ์ help ของ cd มาไล่กันดูการใช้งานพื้นฐาน
cd ~
เครื่องหมาย tilde เป็นตัวอักษรพิเศษที่อ้างอิงถึงไดเรกทอรีโฮมปัจจุบันของคุณ เมื่อคุณใช้ cd ~ มันจะส่งคุณไปที่โฮม หากคุณต้องการรู้ว่านั่นคืออะไร:
echo $HOME
จะพิมพ์ไดเรกทอรีโฮมของคุณออกมา
cd ..
.. ก็เป็นการอ้างอิงพิเศษเช่นกัน มันอ้างอิงถึงไดเรกทอรีพาเรนต์ของไดเรกทอรีทำงานปัจจุบันของคุณ การพิมพ์ cd .. จะพาคุณไปไดเรกทอรีด้านบน คุณสามารถเพิ่มระดับขึ้นไปได้โดยพิมพ์:
cd ../..
นั่นจะขึ้นไปสองระดับ
cd /
/ แทนรูทของระบบของคุณ การพิมพ์ "cd /" จะพาคุณไปที่รูท หากคุณมีสิทธิ์ไปถึงที่นั่น
สรุป
แค่นั้นแหละ! เคล็ดลับการทำงานที่ดีและง่ายสำหรับการใช้ shell