Hướng dẫn này giải thích cách dùng lệnh cd trong Linux để thay đổi thư mục bạn hiện đang ở, trong shell.
Chúng ta đã trước đây đề cập cách nhìn quanh trong thư mục với ls. Nhưng bây giờ bạn cần bắt đầu di chuyển giữa các thư mục. Chúng ta sẽ làm dễ dàng với cd, change directory. Hãy bắt đầu với điều đơn giản, làm sao chúng ta tìm hiểu thêm về tiện ích tích hợp đơn giản như cd? Nếu bạn thử:
man cd
Nó sẽ báo, “No manual entry for cd”. Có thể. Thay vào đó cho tiện ích tích hợp, dùng help.
help cd
Cú pháp CD
Bây giờ chúng ta đã chạy help hữu ích. Chúng ta có output sau.
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.
Thật nói? Đó là lần đầu tôi nhìn qua help printout của cd. Hãy đi qua các cách dùng cơ bản.
cd ~
Dấu tilde là ký tự đặc biệt tham chiếu đến thư mục home hiện tại của bạn. Khi bạn dùng cd ~, nó sẽ đưa bạn đến home. Nếu bạn muốn biết đó là gì:
echo $HOME
Sẽ in ra thư mục home của bạn.
cd ..
.. cũng là tham chiếu đặc biệt. Nó tham chiếu đến thư mục cha của thư mục làm việc hiện tại. Gõ cd .. sẽ đưa bạn đến thư mục trên. Bạn có thể thêm cấp lên thêm bằng cách gõ:
cd ../..
Điều đó sẽ lên hai cấp.
cd /
/ đại diện cho root của hệ thống bạn. Gõ “cd /” sẽ đưa bạn đến root nếu bạn có quyền đi đó.
Kết luận
Đó là nó! Mẹo làm việc nice và đơn giản cho shell.