jianghouren@jianghourendeMacBook-Pro ~ % cd test jianghouren@jianghourendeMacBook-Pro test %
在 test 目录跳转到 Downloads 目录
1 2 3 4 5 6 7 8 9 10 11
jianghouren@jianghourendeMacBook-Pro test % cd ~/Downloads jianghouren@jianghourendeMacBook-Pro Downloads %
// cd ~ 是跳到自己的 home 目录。相当于 cd /Users/jianghouren 。 // 若目录名称省略,则变换至使用者的 home 目录,也就说 cd 相当于 cd ~ 。 jianghouren@jianghourendeMacBook-Pro test % cd ~ jianghouren@jianghourendeMacBook-Pro ~ % pwd /Users/jianghouren jianghouren@jianghourendeMacBook-Pro ~ % cd test jianghouren@jianghourendeMacBook-Pro test % cd /Users/jianghouren/Downloads jianghouren@jianghourendeMacBook-Pro Downloads %
pwd:查看当前目录的具体位置。
1 2 3
jianghouren@jianghourendeMacBook-Pro test % pwd /Users/jianghouren/test jianghouren@jianghourendeMacBook-Pro test %
mkdir:创建目录。
1 2 3 4 5 6
jianghouren@jianghourendeMacBook-Pro test % ls // 创建目录 111 jianghouren@jianghourendeMacBook-Pro test % mkdir 111 jianghouren@jianghourendeMacBook-Pro test % ls 111 jianghouren@jianghourendeMacBook-Pro test %
创建多层级目录
1 2 3 4 5 6 7
jianghouren@jianghourendeMacBook-Pro test % mkdir -p 1/2/3 jianghouren@jianghourendeMacBook-Pro test % ls 1 jianghouren@jianghourendeMacBook-Pro test % cd 1 jianghouren@jianghourendeMacBook-Pro 1 % cd 2 jianghouren@jianghourendeMacBook-Pro 2 % cd 3 jianghouren@jianghourendeMacBook-Pro 3 %
cd .. 是返回上层级目录,而 cd - 是返回上次操作的工作目录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
jianghouren@jianghourendeMacBook-Pro test % ls 1 jianghouren@jianghourendeMacBook-Pro test % cd 1/2/3 // 跳到目前目录的上三层 jianghouren@jianghourendeMacBook-Pro 3 % cd ../../.. // 回到上次操作的目录 jianghouren@jianghourendeMacBook-Pro test % cd - ~/test/1/2/3 jianghouren@jianghourendeMacBook-Pro 3 % cd - ~/test jianghouren@jianghourendeMacBook-Pro test % cd - ~/test/1/2/3 jianghouren@jianghourendeMacBook-Pro 3 % cd .. jianghouren@jianghourendeMacBook-Pro 2 % cd .. jianghouren@jianghourendeMacBook-Pro 1 % cd .. jianghouren@jianghourendeMacBook-Pro test %
ls:显示当前目录下的信息。
可通过 ls -alt 来查看(其中,a 指所有的,l 指列表,t 指倒序)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
jianghouren@jianghourendeMacBook-Pro test % ls 111222 jianghouren@jianghourendeMacBook-Pro test % ls -a . .. .DS_Store 111222 jianghouren@jianghourendeMacBook-Pro test % ls -al total 16 drwxr-xr-x 5 jianghouren staff 16031222:43 . drwxr-xr-x+ 55 jianghouren staff 176031222:22 .. -rw-r--r--@ 1 jianghouren staff 614831211:48 .DS_Store drwxr-xr-x 4 jianghouren staff 12831222:13111 drwxr-xr-x 2 jianghouren staff 6431222:43222 jianghouren@jianghourendeMacBook-Pro test % ls -alt total 16 drwxr-xr-x 5 jianghouren staff 16031222:43 . drwxr-xr-x 2 jianghouren staff 6431222:43222 drwxr-xr-x+ 55 jianghouren staff 176031222:22 .. drwxr-xr-x 4 jianghouren staff 12831222:13111 -rw-r--r--@ 1 jianghouren staff 614831211:48 .DS_Store jianghouren@jianghourendeMacBook-Pro test %
代码块中:. 代表当前目录,..代表上级目录。
1 2 3 4 5 6 7 8 9
jianghouren@jianghourendeMacBook-Pro test % cd . jianghouren@jianghourendeMacBook-Pro test % ls -alt total 16 drwxr-xr-x 4 jianghouren staff 12831211:48 . drwxr-xr-x 2 jianghouren staff 6431211:48111 -rw-r--r--@ 1 jianghouren staff 614831211:48 .DS_Store drwxr-xr-x+ 55 jianghouren staff 176031211:44 .. jianghouren@jianghourendeMacBook-Pro test % cd .. jianghouren@jianghourendeMacBook-Pro ~ %
代码块中:drwxr-xr-x,d 代表目录,rwxr-xr-x 代表权限。
1 2 3 4 5 6 7 8 9 10 11 12 13
jianghouren@jianghourendeMacBook-Pro test % cd 111 jianghouren@jianghourendeMacBook-Pro 111 % ls // 新建一个文件,并写入内容。 jianghouren@jianghourendeMacBook-Pro 111 % echo "123456" >> 1.txt jianghouren@jianghourendeMacBook-Pro 111 % ls 1.txt jianghouren@jianghourendeMacBook-Pro 111 % ls -alt total 8 // - 代表这是普通文件。 -rw-r--r-- 1 jianghouren staff 731218:241.txt drwxr-xr-x 3 jianghouren staff 9631218:24 . drwxr-xr-x 4 jianghouren staff 12831211:48 .. jianghouren@jianghourendeMacBook-Pro 111 %
cp:copy 的简写,拷贝文件。
1 2 3 4 5 6 7
jianghouren@jianghourendeMacBook-Pro test % echo "aaaa" >> 5.txt jianghouren@jianghourendeMacBook-Pro test % ls 5.txt jianghouren@jianghourendeMacBook-Pro test % cp 5.txt 1.txt jianghouren@jianghourendeMacBook-Pro test % ls 1.txt 5.txt jianghouren@jianghourendeMacBook-Pro test %
将当前子目录中的 111 目录下的 1.txt 文件,拷贝到当前目录中。
1 2 3 4 5 6
jianghouren@jianghourendeMacBook-Pro test % cp ./111/1.txt . jianghouren@jianghourendeMacBook-Pro test % ls 1.txt 111 jianghouren@jianghourendeMacBook-Pro test % ls ./111 1.txt jianghouren@jianghourendeMacBook-Pro test %
cat:查看文件内容。
1 2 3
jianghouren@jianghourendeMacBook-Pro test % cat 1.txt 123456 jianghouren@jianghourendeMacBook-Pro test %
rm:删除文件或目录。
1 2 3 4 5 6 7 8 9 10 11 12 13
jianghouren@jianghourendeMacBook-Pro test % ls 1.txt 111 // 删除文件。 jianghouren@jianghourendeMacBook-Pro test % rm 1.txt jianghouren@jianghourendeMacBook-Pro test % ls 111 // 这样是不可以删除目录的。 jianghouren@jianghourendeMacBook-Pro test % rm 111 rm: 111: is a directory // 删除目录。r 代表循环,f 代表强制。 jianghouren@jianghourendeMacBook-Pro test % rm -rf 111 jianghouren@jianghourendeMacBook-Pro test % ls jianghouren@jianghourendeMacBook-Pro test %
jianghouren@jianghourendeMacBook-Pro test % id uid=501(jianghouren) ...... // 通过 root 来创建目录。 jianghouren@jianghourendeMacBook-Pro test % sudo mkdir 222 Password: jianghouren@jianghourendeMacBook-Pro test % ls 222 jianghouren@jianghourendeMacBook-Pro test % ls -alt total 16 drwxr-xr-x 4 jianghouren staff 12831219:00 . // 可以看到,这里的创建者是 root。 drwxr-xr-x 2 root staff 6431219:00222 -rw-r--r--@ 1 jianghouren staff 614831211:48 .DS_Store drwxr-xr-x+ 55 jianghouren staff 176031211:44 .. // 通过普通权限来创建目录。 jianghouren@jianghourendeMacBook-Pro test % mkdir 111 jianghouren@jianghourendeMacBook-Pro test % ls -alt total 16 drwxr-xr-x 5 jianghouren staff 16031219:00 . // 可以看到,这里 111 目录和 222 目录的创建者是不同的。 drwxr-xr-x 2 jianghouren staff 6431219:00111 drwxr-xr-x 2 root staff 6431219:00222 -rw-r--r--@ 1 jianghouren staff 614831211:48 .DS_Store drwxr-xr-x+ 55 jianghouren staff 176031211:44 .. jianghouren@jianghourendeMacBook-Pro test %
下面可以看到,222 目录是可以通过普通权限删除的,这是因为 222 目录属于 test 的子目录,而 test 目录是由 jianghouren 创建的,所以可以通过 jianghouren 权限来删除。
1 2 3 4 5 6
jianghouren@jianghourendeMacBook-Pro test % ls 111222 jianghouren@jianghourendeMacBook-Pro test % rm -rf 222 jianghouren@jianghourendeMacBook-Pro test % ls 111 jianghouren@jianghourendeMacBook-Pro test %