用心分享,共同成长
没有什么比每天进步一点点更重要了
本文已收录到我的github:https://github.com/midou-tech/articles/tree/master/docs/linux ,欢迎star和issues。
序言
本篇文章主要讲解了一些linux常用命令,主要讲解模式是,命令介绍、命令参数格式、命令参数、命令常用参数示例。由于linux命令较多,我还特意选了一些日常使用较为频繁的命令进行讲解,但还是免不了文章很长,建议大家收藏起来,用到的时候不会了再来阅读。当然学习linux命令最好的方法是学会使用linux自带的man手册,所有linux命令规范和使用细则都会在该手册中讲解的很清楚,我在书写的过程中也是参考该手册和日常使用情况。
如果还有哪些是大家希望再补充的命令,可以留言给我,我会持续更新该文章,同时也作为我学习和工作的手册。
Linux初级指令
ls ——List
ls 介绍
这是我学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 -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 Directory
pwd介绍
打印当前工作目录的完整路径名。(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
$ touch -t 201911110000 test1
$ stat test*
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介绍
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 Directory
mkdir介绍
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常用参数示例
$ mkdir test
$ mkdir -p test1/tmp
$ ls
test test1
$ 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
$ mkdir -v test
mkdir: cannot create directory ‘test’: File exists
$ mkdir -v test7
mkdir: created directory ‘test7’
cd —— Change Directory
cd介绍
切换当前目录至指定目录
常用参数示例
$ pwd
/Users/localhost
$cd /
cd命令没什么参数就是切换目录到指定路径下,较为简单,但是使用评率极高。
rm&rmdir —— Remove Directory
rm介绍
The rm utility attempts to remove the non-directory type files specified on the command line. If the permissions of the file do not permit writing, and the standard input device is a terminal, the user is prompted (on the standard error output) for confirmation.
rm实用程序尝试删除命令行上指定的非目录类型文件。 如果文件的权限不允许写入,并且标准输入设备是终端,则会提示用户(在标准错误输出上)进行确认。
rm命令使用时还是需要注意的,他的删除恢复比较麻烦,有些系统会自带-i参数,输入命令之后还有一个确认步骤,有些是直接删掉了,是真删掉,从内存抹掉那种(其实底层是让该文件指针不指向该文件的内存块,内存上的内容原则上是存在的,但是恢复会比较复杂,需要扫描整块内存块才能拿到内容)。不要轻易删掉你写的重要代码,hh。
rm参数格式
rm [-dfiPRrvW] file ...
rm命令参数
-f, --force 忽略不存在的文件,从不给出提示。
-i, --interactive 进行交互式删除
-r, -R, --recursive 指示rm将参数中列出的全部目录和子目录均递归地删除。
-d, --dir 删除空目录
rm常用参数示例
$ touch tmp.cc tmp.java tmp.py tmp.go
$ mkdir -p linux/test
$ ls
linux tmp.cc tmp.go tmp.java tmp.py
$ rm -i tmp.cc
remove tmp.cc? y
$ rm -f tmp.go
$ rm -f linux
rm: linux: is a directory
$ rm -rf linux
$ ls
tmp.java tmp.py
rmdir==rm -d 删除空目录
mv —— Move
mv介绍
In its first form, the mv utility renames the file named by the source operand to the destination path named by the target operand. This form is assumed when the last operand does not name an already existing directory.
In its second form, mv moves each file named by a source operand to a destination file in the existing directory named by the directory operand. The destination path for each operand is the pathname produced by the concatenation of the last operand, a slash, and the final pathname component of the named file.
总结下,就是移动目录或者文件到置顶目录下,同时具有重命名的功能。
mv参数格式
mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
mv命令参数
-b :若需覆盖文件,则覆盖前先行备份。
-f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;
-i :若目标文件 (destination) 已经存在时,就会询问是否覆盖
-n:不要覆盖现有文件。 (-n选项将覆盖以前的任何-f或-i选项。)
-u :若目标文件已经存在,且 source 比较新,才会更新(update)
mv常用参数示例
$ touch tmp.cc
$ ls
tmp.cc
$ mv tmp.cc tmp.java
$ ls
tmp.java
$ pwd
/Users/localhost/test
$ mv /Users/localhost/logs/tmp.txt ./tmp.log
$ ls /Users/localhost/logs/
discover-client metabase tesla
$ ls ./
tmp.java tmp.log
$ mv /Users/localhost/logs/tesla ./tesla.ba
$ ls
tesla.ba tmp.java tmp.log
这个命令在写makefile文件的时候用起来很舒服,可以把编译的结果移到指定目录并重命名。
cp —— Copy
cp介绍
In the first synopsis form, the cp utility copies the contents of the source_file to the target_file. In the second synopsis form, the contents of each named source_file is copied to the destination targ
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69957447/viewspace-2671546/
,如需转载,请注明出处,否则将追究法律责任。