command-line 学习:目录操作

使用的电脑为 mac 。

参考:Learn Enough Command Line to Be Dangerous

目录结构

在 Unix 中的所有目录都是 / 的子目录。目录以 / 开头和分隔目录。

/Users/yourname/ruby/projects~/ruby/projects 是一样的。

除了用户目录之外,每个 Unix 系统都有用于计算机正常操作的程序的系统目录。超级用户才能修改系统文件或者系统目录,root。以root身份登录的超级用户很强大,以至于被认为是一种不好的形式; 被替代的做法是作为 root 执行的任务通常应该使用 sudo 命令。

sudo !! 会以 sudo 形式运行上一个命令。
!ls 会运行 上一个 ls 命令。

sudo 命令让普通的用户可以像超级yo可以执行命令

创建目录

mkdir 是 make directory 的缩写.

$ mkdir text_files

创建过目录,我们可以使用通配符把所有的文本文件都转移到这个目录里面。

$ mv *.txt text_files/

我们可以通过 ls 确认一下:

$ ls text_files/

ls 一个目录,可以把它包含的所有内容都展示出来,我们可以用 -d 来仅仅展示目录。

$ ls -d text_files/

这个常常和 -l 一起使用。

$ ls -ld text_files/

我们可以通过 cd 切换目录。

cd 支持 Tab 自动补全。

可以只输入部分,再按 Tab 键即可自动补全。如 cd tex⇥

cd 输入之后,我们可以用 print working directory 命令,缩写 pwd,还有 ls 一起配合使用来看我们是否进入了正确的目录。

$ pwd 看看你是否在正确的目录结构
$ ls 看看目录中的内容,就可以确定您是都正确进入

切换目录

使用 cd 是最主要的切换目录的方式,但还有很多别的形式值得我们了解。想去上级目录,可以使用 cd ..

如:

1
2
3
4
5
$ pwd
/Users/mhartl/text_files
$ cd ..
$ pwd
/Users/mhartl

如果只输入 cd ,将会进入用户的根目录。和 cd ~ 是一样的。

如果我们在其他的目录下,使用 cd ~/text_files 这种形式可以快速进入这个目录中。

cd . 代表当前目录。最常用的是移动文件或者复制文件到当前目录中。

如:

1
2
3
4
5
6
7
$ pwd
/Users/mhartl/text_files
$ cd ~/second_directory
$ ls
$ cp ~/text_files/sonnets.txt .
$ ls
sonnets.txt

在上面,第一次输入 ls 没有东西输出,是因为,刚刚创建的 second_directory 里面还是空的。

另外一个用 . 的是和 find 一起使用。想 grep 一样的强大。
如:

1
2
3
4
5
$ cd
$ find . -name '*.txt'
./text_files/sonnet_1.txt
./text_files/sonnet_1_reversed.txt
./text_files/sonnets.txt

这是找到当前目录下以及它们的子目录中所有匹配 *.txt 的文件。
还有使用 . 的地方是 open .,在 mac 系统中总是可以生效。

open 命令可以打开目录或者文件,比如 open foo.pdf 将会用默认的 pdf 预览器打开 foo 这个 PDF 文件, open . 则是在 finder 中打开。

最后一个切换命令是 cd -, 它会切换到上一个目录中。
如:

1
2
3
4
5
6
7
$ pwd
/Users/mhartl/second_directory
$ cd ~/text_files
$ pwd
/Users/mhartl/text_files
$ cd -
/Users/mhartl/second_directory

混合命令。

如:

$ ./configure ; make ; make install

这个混合命令,会先执行 make 在执行 make install

更好一点的写法是:

$ ./configure && make && make install

一个用 ; 分隔开,一个用 && 分隔开。使用 && 的话,只有上一个命令执行成功了才会执行下一个命令。而使用 ; 分隔,不管上一个命令是否执行成功,都会执行下一个命令。

重命名,复制和删除目录

重命名:

1
2
3
4
5
$ mkdir foo
$ mv foo/ bar/
$ cd foo/
-bash: cd: foo: No such file or directory
$ cd bar/

text_files 目录及所有内容 复制到 foobar 中:

1
2
3
4
5
6
$ cd
$ mkdir foobar
$ cd foobar/
$ cp -r ../text_files .
$ ls
text_files

如果仅仅是复制目录下的所有内容到此目录中,要加上 /,如:

1
2
3
$ cp -r ../text_files/ .
$ ls
sonnet_1.txt sonnet_1_reversed.txt sonnets.txt text_files

如果你只是想复制文件,使用 * 来表示,如:
$ cp ../text_files/* . 把 text_files 下的所有文件复制到当前目录下。

删除目录 rmdir,很难执行成功。如:

1
2
3
$ cd
$ rmdir second_directory
rmdir: second_directory/: Directory not empty

当删除目录的时候,99% 的记录会出现上面这个错误,因为 rmdir 目录必须要是空的。可以使用一个更强大,但同时也更危险的命令 remove recursive force,即 rm -rf,这会删除这个目录,以及这个目录里的所有文件及子目录,而且没有再次的确认提示。
如:

1
2
3
$ rm -rf second_directory/
$ ls second_directory
ls: second_directory: No such file or directory

如果你想找到含有 sesquipedalian 这个词的文件,但是你又忘记了文件的名字了,你可以使用:

1
2
$ grep -r sesquipedalian text_files
text_files/foo/long_word.txt:sesquipedalian

由于大小写不重要,建议在递归的情况下添加 -i 选项,如下:

1
2
$ grep -ri sesquipedalian text_files
text_files/foo/long_word.txt:sesquipedalian

总结

Command Description Example
mkdir Make directory with name $ mkdir foo
pwd Print working directory $ pwd
cd Change to $ cd foo/
cd ~/ cd relative to home $ cd ~/foo/
cd Change to home directory $ cd
cd - Change to previous directory $ cd && pwd && cd -
. The current directory $ cp ~/foo.txt .
.. One directory up $ cd ..
find Find files & directories $ find . -name foo*.*
cp -r Copy recursively $ cp -r ~/foo .
rmdir Remove (empty) dir $ rmdir foo/
rm -rf Remove dir & contents $ rm -rf foo/
grep -ri Grep recursively (case-insensitive) $ grep -ri foo bar/