序言 本篇文章主要讲解了一些linux常用命令,主要讲解模式是,命令介绍、命令参数格式、命令参数、命令常用参数示例。由于linux命令较多,我还特意选了一些日常使用较为频繁的命令进行讲解,但还是免不了文章很长,建议大家收藏起来,用到的时候不会了再来阅读。当然学习linux命令最好的方法是学会使用linux自带的man手册,所有linux命令规范和使用细则都会在该手册中讲解的很清楚,我在书写的过程中也是参考该手册和日常使用情况。 如果还有哪些是大家希望再补充的命令,可以留言给我,我会持续更新该文章,同时也作为我学习和工作的手册。 Linux初级指令ls ——Listls 介绍 这是我学Linux的第一个命令,相信也是很多人学习Linux的第一个命令。ls全称list. List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified. 列出有关文件的信息(默认为当前目录)。如果没有指定-cftuvSUX或——Sort,则按字母顺序排序。
eg: $ls /etc/hosts
/etc/hosts
复制代码 ls还可以列出指定目录下的文件列表。
image-20191221161638964 ls 参数格式ls [OPTION]... [FILE]... ls命令参数-a 列出指定目录下的所有文件,包括隐藏文件 -c 使用最后一次更改文件状态以进行排序(-t)或长时间打印(-l)的时间 -h 与-l选项一起使用时,请使用单位后缀:Byte、Kilobyte、mete、gb、tb和Petabyte,以便使用以2为基数的大小将数字减少到3或更少 -l 长格式列表。(见下文)。如果输出到终端,则所有文件大小的总和将输出到长清单前面的一行中 -n 以数字形式显示用户和组id,而不是在长(-l)输出中转换为用户或组名。这个选项默认打开-l选项 -o 以长格式列出,但省略组id -s 显示每个文件实际使用的文件系统块的数量,以512字节为单位,其中部分单元四舍五入为下一个整数值 -t 在按照字典顺序对操作数排序之前,先按修改的时间排序(最近修改的是first) -u 使用最后一次访问的时间,而不是最后一次修改文件进行排序 ls 用法示例:$ ls
test tmp
$ ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc test tmp
#可以看到通过ls -a 可以查看当前目录影藏的文件,Linux下(.)开头的文件是隐藏文件。
$ ls -l
total 4
-rw-r--r-- 1 test hero 0 Dec 21 19:54 test
drwxr-xr-x 2 test hero 4096 Dec 21 19:54 tmp
$ ls -lh
total 4.0K
-rw-r--r-- 1 test hero 0 Dec 21 19:54 test
drwxr-xr-x 2 test hero 4.0K Dec 21 19:54 tmp
$ ls -ll
total 4
-rw-r--r-- 1 test hero 0 Dec 21 19:54 test
drwxr-xr-x 2 test hero 4096 Dec 21 19:54 tmp
$ ls -alh
total 28K
drwx------ 3 test hero 4.0K Dec 21 19:54 .
drwxr-xr-x. 19 root root 4.0K Aug 1 10:41 ..
-rw------- 1 test hero 226 Dec 21 19:54 .bash_history
-rw-r--r-- 1 test hero 18 Aug 3 2016 .bash_logout
-rw-r--r-- 1 test hero 193 Aug 3 2016 .bash_profile
-rw-r--r-- 1 test hero 231 Aug 3 2016 .bashrc
-rw-r--r-- 1 test hero 0 Dec 21 19:54 test
drwxr-xr-x 2 test hero 4.0K Dec 21 19:54 tmp
$ ls -o
total 4
-rw-r--r-- 1 test 0 Dec 21 19:54 test
drwxr-xr-x 2 test 4096 Dec 21 19:54 tmp
$ ls -oh
total 4.0K
-rw-r--r-- 1 test 0 Dec 21 19:54 test
drwxr-xr-x 2 test 4.0K Dec 21 19:54 tmp
复制代码pwd —— Print Working Directorypwd介绍 打印当前工作目录的完整路径名。(print name of current/working directory) 参数格式pwd [OPTION]... pwd 用法展示[test@Mfate171193 /home/test] 20:06
$ pwd
/home/test
复制代码touch (change file timestamps)touch介绍Update the access and modification times of each FILE to the current time. A FILE argument that does not exist is created empty, unless -c or -h is supplied.
将每个文件的访问和修改时间更新为当前时间。除非提供-c或-h,否则将不存在的FILE参数创建为空。 touch参数格式touch [OPTION]... FILE... touch命令参数-a 或--time=atime或--time=access或--time=use 只更改存取时间。 -c 或--no-create 不建立任何文档。 -d 使用指定的日期时间,而非现在的时间。 -f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。 -m 或--time=mtime或--time=modify 只更改变动时间。 -r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。 -t 使用指定的日期时间,而非现在的时间。 用法示例#创建三个文件
$ touch test1 test2 test3
#不创建文档
$ touch -c test5
$ ls
test1 test2 test3
#可以看到只创建了test1、test2、test3 , -c不建立任何文件
$ touch -t 201911110000 test1
$ stat test*
#stat命令可以查看文件的详细变更时间,可以test1文件的最后修改时间为201911110000,说明touch -t可以修改文件最后访问时间。这个参数还是很有用的,你可以把你最近访问的时间修改为一个很早的时间。可以做一些有趣的事情,哈哈。
File: ‘test1’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 360736 Links: 1
Access: (0644/-rw-r--r--) Uid: (14060/ localhost) Gid: ( 1001/ hero)
Access: 2019-11-11 00:00:00.000000000 +0800
Modify: 2019-11-11 00:00:00.000000000 +0800
Change: 2019-12-21 20:58:11.290761038 +0800
Birth: -
File: ‘test2’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 360738 Links: 1
Access: (0644/-rw-r--r--) Uid: (14060/ localhost) Gid: ( 1001/ hero)
Access: 2019-12-21 20:56:34.523761038 +0800
Modify: 2019-12-21 20:56:34.523761038 +0800
Change: 2019-12-21 20:56:34.523761038 +0800
Birth: -
File: ‘test3’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 360740 Links: 1
Access: (0644/-rw-r--r--) Uid: (14060/ localhost) Gid: ( 1001/ hero)
Access: 2019-12-21 20:56:34.523761038 +0800
Modify: 2019-12-21 20:56:34.523761038 +0800
Change: 2019-12-21 20:56:34.523761038 +0800
Birth: -
复制代码cat&tac (Concatenate FILE(s), or standard input, to standard output.)cat介绍Concatenate FILE(s), or standard input, to standard output. 将FILE或标准输入连接到标准输出。 cat 参数格式cat [OPTION]... [FILE]... cat命令参数-A, --show-all 等价于 -vET -b, --number-nonblank 对非空输出行编号 -e 等价于 -vE -E, --show-ends 在每行结束处显示 -n, --number 对输出的所有行编号,由1开始对所有输出的行数编号 -s, --squeeze-blank 有连续两行以上的空白行,就代换为一行的空白行 -t 与 -vT 等价 -T, --show-tabs 将跳格字符显示为 ^I -u (被忽略) -v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外 cat常用参数示例$ cat test #展示文件内容
-A, --show-all 等价于 -vET
-b, --number-nonblank 对非空输出行编号
-e 等价于 -vE
$ cat -n test #展示文件内容并且展示行号
1 -A, --show-all 等价于 -vET
2 -b, --number-nonblank 对非空输出行编号
3 -e 等价于 -vE
复制代码tac命令与cat命令展示内容相反,不能带行号输出。$ tac test
-e 等价于 -vE
-b, --number-nonblank 对非空输出行编号
-A, --show-all 等价于 -vET
复制代码mkdir —— Make Directorymkdir介绍Create the DIRECTORY(ies), if they do not already exist. 如果目录不存在,则创建目录。 mkdir参数格式mkdir [OPTION]... DIRECTORY... mkdir命令参数-m, --mode=模式,设定权限<模式> (类似 chmod),而不是 rwxrwxrwx 减 umask -p, --parents 可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录; -v, --verbose 每次创建新目录都显示信息 --help 显示此帮助信息并退出 --version 输出版本信息并退出 mkdir常用参数示例#创建目录文件test
$ mkdir test
#连续创建
$ mkdir -p test1/tmp
$ ls
test test1
#创建时置顶目录权限
#tmp目录拥有可执行权限,Linux文件权限问题后期文章会详细讲到,记得关注我
$ mkdir -pm 777 test2/tmp
$ ls -lh
total 12K
drwxr-xr-x 2 localhost hero 4.0K Dec 21 21:39 test
drwxr-xr-x 3 localhost hero 4.0K Dec 21 21:40 test1
drwxr-xr-x 3 localhost hero 4.0K Dec 21 21:40 test2
$ ls
test test1 test2
#-v 参数可确定文件是否已经存在,如果不存在则会创建,并显示如下信息
$ mkdir -v test
mkdir: cannot create directory ‘test’: File exists
$ mkdir -v test7
mkdir: created directory ‘test7’
复制代码cd —— Change Directorycd介绍 切换当前目录至指定目录 常用参数示例#打印当前目录到标准输出
$ pwd
/Users/localhost
#切换到目录/
$cd /
复制代码 cd命令没什么参数就是切换目录到指定路径下,较为简单,但是使用评率极高。
|