命令行学习笔记: 基本文件/目录命令
2016-12-11 本文已影响18人
z嘉嘉嘉
学习内容
文件/目录相关基本命令:
cd, mkdir, cp, rm, mv, pwd, ls, tar, zip, find, grep, less, tail, tree
个人总结
-
ls - List
ls
列出当前工作目录内容
➜ ~ ls
Book examples.desktop Music Public wince3+1
Desktop gradle-2.9-bin.zip newRamlProject Templates
Documents IdeaProjects pdf Videos
Downloads logs Pictures WebstormProjects
-
mkdir - Make Directory
mkdir <new-directory-name>
创建一个新的文件夹
➜ ~ mkdir newDir
➜ ~ ls
Book examples.desktop Music Public wince3+1
Desktop gradle-2.9-bin.zip newRamlProject Templates newDir
Documents IdeaProjects pdf Videos
Downloads logs Pictures WebstormProjects
-
pwd – Print Working Directory
pwd
显示当前工作目录
➜ ~ pwd
/home/zhyingjia
-
cd – Change Directory
cd <directory-name>
进入某一文件目录
➜ ~ cd newDir
➜ newDir pwd
/home/zhyingjia/newDir
-
rmdir – Remove Directory
rmdir <directory-name>
删除给定的空目录
➜ ~ rmdir newDir
➜ ~ ls
Book examples.desktop Music Public wince3+1
Desktop gradle-2.9-bin.zip newRamlProject Templates
Documents IdeaProjects pdf Videos
Downloads logs Pictures WebstormProjects
-
rm – Remove
rm <file-name>
删除给定的文件或文件夹
rm -r <directory-name>
递归删除文件夹
➜ newDir mkdir content
➜ newDir ls
content
➜ newDir cd ../
➜ ~ rmdir newDir
rmdir: failed to remove 'newDir': Directory not empty
➜ ~ rm newDir
rm: cannot remove 'newDir': Is a directory
➜ ~ rm -r newDir
➜ ~ ls
Book examples.desktop Music Public wince3+1
Desktop gradle-2.9-bin.zip newRamlProject Templates
Documents IdeaProjects pdf Videos
Downloads logs Pictures WebstormProjects
-
cp – Copy
cp <source-file> <destination-file>
对文件进行复制
cp -r <source-folder> <destination-folder>
对文件夹进行递归复制
➜ newDir touch file1.txt
➜ newDir ls
content file1.txt
➜ newDir cp file1.txt file2.txt
➜ newDir ls
content file1.txt file2.txt
➜ newDir ls
content file1.txt file2.txt
➜ newDir cp file2.txt content
➜ newDir ls
content file1.txt file2.txt
➜ newDir cd content
➜ content ls
file2.txt
-
mv – MoVe
mv <source> <destination>
对文件或文件夹进行移动 / 重命名
➜ newDir ls
content file2.txt
➜ newDir cd content
➜ content ls
➜ content cd ../
// 重命名
➜ newDir mv file2.txt file.txt
➜ newDir ls
content file.txt
// 移动
➜ newDir mv file.txt content
➜ newDir ls
content
➜ newDir cd content
➜ content ls
file.txt
-
cat – concatenate and print files
cat <file>
用于在标准输出(监控器或屏幕)上查看文件内容
Shift + PgUp
向上翻页
Shift + PgDn
向下翻页
➜ content ls
file.txt
➜ content cat file.txt
0 file test data
1 file test data
2 file test data
3 file test data
4 file test data
5 file test data
6 file test data
7 file test data
8 file test data
9 file test data
10 file test data
11 file test data
12 file test data
13 file test data
-
10.tail – print TAIL (from last) >
tail <file-name>
默认在标准输出上显示给定文件的最后10行内容
tail -n N <file-name>
指定在标准输出上显示文件的最后N行内容
➜ content tail file.txt
4 file test data
5 file test data
6 file test data
7 file test data
8 file test data
9 file test data
10 file test data
11 file test data
12 file test data
13 file test data
➜ content tail file.txt -n 3
11 file test data
12 file test data
13 file test data
-
less – print LESS
less <file-name>
按页或按窗口打印文件内容。在查看包含大量文本数据的大文件时是非常有用和高效的。
Ctrl+F
向前翻页
Ctrl+B
向后翻页 -
grep
grep "<string>" <file-name>
在给定的文件中搜寻指定的字符串
grep -i "<string>" <file-name>
在搜寻时会忽略字符串的大小写
grep -r "<string>" <file-name>
在当前工作目录的文件中递归搜寻指定的字符串
➜ ~ grep "1 f" newDir/content/file.txt
1 file test data
11 file test data
➜ ~ grep "0 File" newDir/content/file.txt
➜ ~ grep "0 File" -i newDir/content/file.txt
0 file test data
10 file test data
➜ ~ cd newDir
➜ newDir grep "0 File" -i -r
content/file.txt:0 file test data
content/file.txt:10 file test data
-
Find
find <folder-to-search> -name <file-name>
在给定位置搜寻与条件匹配的文件(区分大小写)
find <folder-to-search> -iname <file-name>
在给定位置搜寻与条件匹配的文件(不区分大小写)
➜ ~ find newDir -name content
newDir/content
➜ ~ find newDir -iname Content
newDir/content
-
tar(打包)
tar -cvf <archive-name.tar> <file1-OR-file2-OR-both-to-archive>
打包对应文件/目录
tar -tvf <archive-to-view.tar>
来查看打包文件的内容
tar -xvf <archive-to-extract.tar>
来提取对应打包文件内容
➜ newDir ls
content
➜ newDir tar -cvf content.tar content
content/
content/file.txt
➜ newDir ls
content content.tar
➜ newDir tar -tvf content.tar
drwxrwxr-x zhyingjia/zhyingjia 0 2016-12-09 22:22 content/
-rw-rw-r-- zhyingjia/zhyingjia 242 2016-12-09 22:22 content/file.txt
➜ newDir rm -rf content
➜ newDir ls
content.tar
➜ newDir tar -xvf content.tar
content/
content/file.txt
➜ newDir ls
content content.tar
-
gzip(压缩)
gzip <filename>
创建压缩文件
gzip -d <filename>
提取压缩文件
➜ content ls
file.txt
➜ content gzip file.txt
➜ content ls
file.txt.gz
➜ content gzip -d file.txt.gz
➜ content ls
file.txt
-
zip(压缩)
zip archive-name.zip filename
压缩一个文件
zip -r archive-name.zip directory-name
递归压缩一个目录
➜ content zip file.zip file.txt
adding: file.txt (deflated 100%)
➜ content ls
file.txt file.zip
➜ content cd ../
➜ newDir ls
content
➜ newDir zip -r content.zip content
adding: content/ (stored 0%)
adding: content/file.zip (stored 0%)
adding: content/file.txt (deflated 100%)
➜ newDir ls
content content.zip
-
unzip(解压缩)
unzip <archive-to-extract.zip>
对.zip文档进行解压
在解压之前,可以使用unzip -l <archive-to-extract.zip>
命令查看文件内容
// 因为解压的时候会有命名冲突,因此将content.zip移动到content下进行操作
➜ newDir mv content.zip content/
➜ newDir ls
content
➜ newDir cd content
➜ content ls
content.zip file.txt file.zip
➜ content unzip -l content.zip
Archive: content.zip
Length Date Time Name
--------- ---------- ----- ----
0 2016-12-10 13:20 content/
193 2016-12-10 13:20 content/file.zip
10240 2016-12-10 13:05 content/file.txt
--------- -------
10433 3 files
➜ content unzip content.zip
Archive: content.zip
creating: content/
extracting: content/file.zip
inflating: content/file.txt
➜ content ls
content content.zip file.txt file.zip
-
tree
tree
树形显示当前目录结构
➜ ~ cd newDir
➜ newDir tree
.
└── content
├── content
│ ├── file.txt
│ └── file.zip
├── content.zip
├── file.txt
└── file.zip
2 directories, 5 files
感受
参数学习需要在实际中学习总结。