Linux常用命令总结-----不断更新(-)
一、mkdir 在Linux中创建目录(默认情况下不能创建多级目录) make directories
命令的重要参数:
-p --parents no error if existing, make parent directories as needed (创建目录的时候如果需要创建父目录不会报错,用来多层级创建目录)
-v --verbose print a message for each created directory (显示目录创建的过程)
例子:
1.使用默认的mkdir在/tmp目录下创建一个data目录
[root@moban ~]# ls /tmp
test.txt
[root@moban ~]# mkdir /tmp/data
[root@moban ~]# ls -ld /tmp/data/
drwxr-xr-x 2 root root 4096 Jan 21 09:28 /tmp/data/
2.使用mkdir在/tmp/data目录下创建a/b/c/d/e目录
[root@moban data]# mkdir /tmp/data/a/b/c/d/e
mkdir: cannot create directory `/tmp/data/a/b/c/d/e': No such file or directory (注意这里系统提示你无法创建'/tmp/data/a/b/c/d/e'这个目录)
[root@moban data]# mkdir -pv /tmp/data/a/b/c/d/e (这里我们采用-p参数进行多层级创建,同时使用-v参数让系统显示整个目录的创建过程)
mkdir: created directory `/tmp/data/a'
mkdir: created directory `/tmp/data/a/b'
mkdir: created directory `/tmp/data/a/b/c'
mkdir: created directory `/tmp/data/a/b/c/d'
mkdir: created directory `/tmp/data/a/b/c/d/e'
二、 ls 用列表的方式显示目录的内容(默认情况下不显示隐藏文件) list directory contents
命令的重要参数:
-a -all do not ignore entries starting with . (显示所有的文件包括用"."开头的隐藏文件)
-l use a long listing format (用长格式显示内容的具体信息)
-d --directory list directory entries instead of contents, and do not dereference symbolic links(只显示文件自身的相关信息,但是不包括符号链接)
-h --human-readable with -l, print sizes in human readable format (e.g., 1K 234M 2G)(和-l参数一起使用,可以将内容的大小实现为方便人阅读的格式)
-i --inode print the index number of each file(显示目录下每个文件的iNode的索引号)
-t sort by modification time(按照修改时间进行排序)
-r --reverse reverse order while sorting(倒序排列)
例子:
1.显示目录/tmp下包括的内容
[root@moban data]# ls /tmp
data test.txt
2.显示目录/tmp下内容的相关信息
[root@moban data]# ls -l /tmp/
total 8
drwxr-xr-x 3 root root 4096 Jan 21 09:36 data
-rw-r--r-- 1 root root 292 Jan 20 15:57 test.txt
3.显示/tmp目录下的所有内容包含隐藏文件
[root@moban data]# ls -a /tmp
. .. data .ICE-unix test.txt
[root@moban data]# ls -al /tmp/
total 20
drwxrwxrwt 4 root root 4096 Jan 21 09:44 .
dr-xr-xr-x. 22 root root 4096 Jan 21 09:22 ..
drwxr-xr-x 3 root root 4096 Jan 21 09:36 data
drwxrwxrwt 2 root root 4096 Jan 21 09:22 .ICE-unix
-rw-r--r-- 1 root root 292 Jan 20 15:57 test.txt
4.显示/tmp目录自身的属性信息
[root@moban data]# ls -ld /tmp
drwxrwxrwt 4 root root 4096 Jan 21 09:44 /tmp
5.显示/tmp目录下所有文件的大小,使用K,M,G的格式显示
[root@moban data]# ls -lh /tmp
total 8.0K
drwxr-xr-x 3 root root 4.0K Jan 21 09:36 data
-rw-r--r-- 1 root root 292 Jan 20 15:57 test.txt
6.显示/tmp目录的inode文件的索引号
[root@moban data]# ls -il /tmp
total 8
915788 drwxr-xr-x 3 root root 4096 Jan 21 09:36 data
915785 -rw-r--r-- 1 root root 292 Jan 20 15:57 test.txt
7.按照修改时间倒序排列目录内容列表(当一个目录内用很多文件的时候,我们希望在最后一屏显示我们最近修改过的文件)
[root@moban tmp]# ls -lrt
total 28
-rw-r--r-- 1 root root 0 Jan 1 2016 testlab.txt
drwxr-xr-x 2 root root 4096 Jan 21 20:44 data
-rw-r--r-- 1 root root 0 Jan 21 21:58 1
-rw-r--r-- 1 root root 0 Jan 21 21:59 9.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 8.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 7.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 6.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 5.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 4.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 3.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 2.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 20.txt
-rw-r--r-- 1 root root 0 Jan 21 22:02 test1.txt
-rw-r--r-- 1 root root 1213 Jan 21 23:42 test.txt
drwxr-xr-x 7 root root 4096 Jan 23 14:10 test
drwxr-xr-x 9 root root 4096 Jan 23 14:10 old
-rw-r--r-- 1 root root 16 Jan 25 15:10 data.txt
drwxr-xr-x 4 root root 4096 Jan 26 20:32 testPython
-rw-r--r-- 1 root root 472 Jan 27 15:30 glances-root.log
三、cd 切换工作目录 Change the shell working directory.
例子:
1.从当前的工作目录切换到当前用的家目录
[root@moban data]# pwd
/tmp/data
[root@moban data]# cd ~
[root@moban ~]# pwd
/root
四、pwd 显示用户当前的工作路径 print name of current/working directory
例子:
1.查看用户当前的所在的目录
[root@moban ~]# pwd
/root
[root@moban ~]# cd /tmp/data/a
[root@moban a]# pwd
/tmp/data/a
五、stat 查看文件或者文件系统的状态信息 display file or file system status
命令的重要参数:
-f 显示文件系统的信息
-t 使用简要形式显示相关的信息
例子:
1.显示/tmp/test.txt文件的相关状态信息
[root@moban ~]# stat /tmp/test.txt
File: `/tmp/test.txt'
Size: 292 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 915785 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-01-20 17:31:32.015989451 +0800
Modify: 2018-01-20 15:57:29.967992079 +0800
Change: 2018-01-20 16:19:00.874988532 +0800
2.显示/tmp/test.txt文件相关的文件系统信息
[root@moban ~]# stat -f /tmp/test.txt
File: "/tmp/test.txt"
ID: 2a3b8b3dea084067 Namelen: 255 Type: ext2/ext3
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 4689996 Free: 4187985 Available: 3948075
Inodes: Total: 1201872 Free: 1122007
3.显示/tmp/test.txt文件的简要信息
[root@moban ~]# stat -t /tmp/test.txt
/tmp/test.txt 292 8 81a4 0 0 803 915785 1 0 0 1516440692 1516435049 1516436340 4096
六、echo 向屏幕上打印需要输出的内容 display a line of text
命令重要参数:
-n 打印结束以后不要自动换行 do not output the trailing newline
-e 开启转义字符功能 enable interpretation of backslash escapes
-E 关闭转义字符功能(默认使用这个参数) disable interpretation of backslash escapes (default)
例子:
1.在屏幕上打印“Welcome to the world of Linux.”
[root@moban ~]# echo "Welcome to the world of Linux."
Welcome to the world of Linux.
2.在屏幕上打印如下内容:
How are you?
Fine,thank you .and you?
I'm fine too.
[root@moban ~]# echo -ne "How are you?\nFine,thank you.and you?\nI'm fine too.\n"
How are you?
Fine,thank you.and you?
I'm fine too.
七、cat 用于查看文件的内容,也可以进行多行输入到文件。 concatenate files and print on the standard output(合并文件并打印到标准输出)
命令重要参数:
-A --show-all 相当于参数vET
-b --number-nonblank 显示行号的时候,将空行不作为一行
-e 相当于参数vE
-E --show-ends 在行的结尾打印出"$"符号
-n --number 打印出行号(默认空行算一行)
-s --squeeze-blank 在文件打印的过程中连续的空行作为一行
-T --show-tabs 在打印文件的时候用^I代替Tab
-v --show-nonprinting 显示文件中的非打印可见的字符
例子:
1.在屏幕上显示文件/tmp/test.txt的内容
[root@moban ~]# cat /tmp/test.txt
10 17 11
12
13
14
15
16
18
19
20
2.显示文件/tmp/test.txt中的Tab字符
[root@moban ~]# cat -T /tmp/test.txt
10^I17^I11
12
13
14
15
16
18
19
20
3.显示文件/tmp/test.txt的行号及其内容
[root@moban ~]# cat -n /tmp/test.txt
1 10 17 11
2
3
4
5 12
6
7 13
8
9 14
10 15
11 16
12
13 18
14 19
15 20
4.显示文件/tmp/test.txt的行号及其内容,空行不算在内
[root@moban ~]# cat -b /tmp/test.txt
1 10 17 11
2 12
3 13
4 14
5 15
6 16
7 18
8 19
9 20
5.显示文件/tmp/test.txt的内容,连续的空行算作一行
[root@moban ~]# cat -s /tmp/test.txt
10 17 11
12
13
14
15
16
18
19
20
6.在文件/tmp/test.txt每行的结尾加上"$"符号
[root@moban ~]# cat -E /tmp/test.txt
10 17 11$
$
$
$
12$
$
13$
$
14$
15$
16$
$
18$
19$
20$
7.使用cat进行多行输入到文件/tmp/test1.txt,然后查看文件的内容
[root@moban ~]# cat >> /tmp/test1.txt << EOF
> How old are you?
> Thirty.
> Are you Chinese?
> Yes.
> EOF
[root@moban ~]# cat /tmp/test1.txt
How old are you?
Thirty.
Are you Chinese?
Yes.
8.使用cat进行多行输入到文件/tmp/test1.txt,然后查看文件内容(结束符需要用tab缩进)
[root@moban ~]# cat /tmp/file1.txt
1234
[root@moban ~]# cat > /tmp/file1.txt <<-EOF
> welcome to my lab.
> thank you.
> EOF
[root@moban ~]# cat /tmp/file1.txt
welcome to my lab.
thank you.
八、cp 拷贝文件或者目录 copy files and directories
命令重要参数:
-a --archive 相当于dRp参数
-d 相当于no-dereference --preserve=links
-R 相当于参数-r
-r --recursive 对目录的内容进行递归(默认情况下cp无法拷贝目录)
-p 相当于--preserve=mode,ownership,timestamps,保持文件的属性信息
例子:
1.将文件/tmp/test1.txt拷贝到/tmp/data目录
[root@moban ~]# cp /tmp/test1.txt /tmp/data/
[root@moban ~]# ls /tmp/data/
a test1.txt
2.将/etc目录拷贝到/tmp目录
[root@moban ~]# cp -r /etc /tmp/
[root@moban ~]# ls /tmp/
data etc test1.txt test.txt
3.将文件/tmp/test1.txt拷贝到/tmp/data/a目录并保持文件属性
[root@moban ~]# stat /tmp/test1.txt
File: `/tmp/test1.txt'
Size: 47 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 915789 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-01-21 15:47:59.439372101 +0800
Modify: 2018-01-21 15:47:52.889347184 +0800
Change: 2018-01-21 15:47:52.889347184 +0800
[root@moban ~]# cp -a /tmp/test1.txt /tmp/data/a/
[root@moban ~]# stat /tmp/data/a/test1.txt
File: `/tmp/data/a/test1.txt'
Size: 47 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 917330 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-01-21 15:47:59.439372101 +0800
Modify: 2018-01-21 15:47:52.889347184 +0800
Change: 2018-01-21 16:01:15.764350467 +0800
九、man 用于查看命令的帮助手册 format and display the on-line manual pages
帮助手册的基本形式如下:
| man帮助信息中的标题 | 功能说明(带*的是重点) |
| NAME | 命令说明及介绍 * |
| SYNOPSIS | 命令的基本使用语法 * |
| DESCRIPTION | 命令使用详细描述,以及相关参数选项说明 * |
| OPTIONS | 命令相关参数选项说明(有的命令帮助没有此项) |
| COMMANDS | 在执行这个程序(或软件)的时候,可以在此程序(或软件)中执行的命令 |
| FILES | 程序涉及(或使用、或关联)的相关文件 |
| EXAMPLES | 命令的一下例子 * |
| SEE ALSO | 和命令相关的信息说明 |
| BUGS(REPORTING BUGS) | 命令对应缺陷问题的描述 |
| COPYRIGHT | 版权相关信息声明 |
| AUTHOR | 作者介绍 |
十、mv 用于移动文件或者对文件进行重命名(默认情况下只能移动空的目录) move (rename) files
命令重要参数:
-f --force 如果移动的目的地存在相同的文件直接覆盖,不给出提示
1.将/tmp/test1.txt重命名为/tmp/test1.avi
[root@moban ~]# mv /tmp/test1.txt /tmp/test1.avi
[root@moban ~]# ll /tmp/
total 16
drwxr-xr-x 3 root root 4096 Jan 21 15:58 data
drwxr-xr-x 84 root root 4096 Jan 21 15:59 etc
-rw-r--r-- 1 root root 47 Jan 21 15:47 test1.avi
-rw-r--r-- 1 root root 39 Jan 21 15:35 test.txt
2.将/root/data目录移动到/tmp/data目录下
[root@moban ~]# mv /root/data/* /tmp/data/
[root@moban ~]# ls /tmp/data/
a alex.avi test1.txt
十一、rm 删除文件或者目录(默认情况下不能删除非空目录) remove files or directories
命令重要参数:
-f --force 强制删除文件或者目录,不需要再次确认
-r, -R --recursive 递归删除目录及其目录内的文件和子目录
1.删除/tmp/test1.avi文件
[root@moban data]# ll /tmp/
total 16
drwxr-xr-x 3 root root 4096 Jan 21 16:18 data
drwxr-xr-x 84 root root 4096 Jan 21 15:59 etc
-rw-r--r-- 1 root root 47 Jan 21 15:47 test1.avi
-rw-r--r-- 1 root root 39 Jan 21 15:35 test.txt
[root@moban data]# rm /tmp/test1.avi
rm: remove regular file `/tmp/test1.avi'? y
[root@moban data]# ll /tmp/
total 12
drwxr-xr-x 3 root root 4096 Jan 21 16:18 data
drwxr-xr-x 84 root root 4096 Jan 21 15:59 etc
-rw-r--r-- 1 root root 39 Jan 21 15:35 test.txt
2.删除/tmp/data/a目录的所有内容
[root@moban data]# tree a/
a/
├── b
│ └── c
│ └── d
│ └── e
└── test1.txt
4 directories, 1 file
[root@moban data]# rm -rf a/
[root@moban data]# ll
total 8
-rw-r--r-- 1 root root 10 Sep 22 05:35 alex.avi
-rw-r--r-- 1 root root 47 Jan 21 15:58 test1.txt
十二、head 默认显示一个文件的开始10行 output the first part of files
命令主要参数:
-c --bytes 打印文件开始的N字节
-n --lines 打印文件开始的N行
十三、tail 默认显示一个文件的末尾10行 output the last part of files
-c --bytes 打印文件末尾的N字节
-n --lines 打印文件末尾的N行
-f --follow 打印文件的末尾10行,然后随着文件的刷新而刷新
-F 和-f的区别在于如果监控的文件被打断,可以保持重试状态
十四、alias 设置别名或者查看系统现有的别名 Define or display aliases.
注意:
在设置系统别名的时候,最好的办法是设置在用户家目录下的.bashrc。如果设置在/etc/profile,用户再次登录的时候有可能不生效。
如果系统已经设置了别名,现在需要暂时屏蔽别名功能,可以使用"\"+命令或者直接使用命令的全路径
例子:
1.显示系统现在使用的别名
[root@moban ~]# alias
alias cp='cp -i'
alias grep='grep --color'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
十五、unalias 取消别名 remove all alias definitions.
命令重要参数:
-a 一次性取消所有已经定义的别名
十六、which 显示命令的全路径 shows the full path of (shell) commands.
例子:
1.查询cat命令在系统中的全路径
[root@moban ~]# which cat
/bin/cat
十七、find 在指定的目录下按照要求搜索文件(可以实现变量目录下的所有目录) search for files in a directory hierarchy
命令重要参数:
-type 按照文件类型搜索
-name 按照文件的名称搜索(只能支持*,?和[]三个通配符)
-mtime 按照文件的修改的时间搜索
-size 按照文件的大小搜索,可以使用K\M\G
-maxdepth 设置在搜索目录时候的最大深度(使用这个参数要放在-type参数之前,不然系统会给出提示)
-regex 在find命令中使用正则表达式
-exec 将find命令的结果当做参数传递给其他的命令执行
例子:
1.查找/tmp目录下所有以'.txt'结尾的文件
[root@moban ~]# find /tmp -type f -name "*.txt"
/tmp/18.txt
/tmp/20.txt
/tmp/3.txt
/tmp/8.txt
/tmp/10.txt
/tmp/2.txt
/tmp/data/test1.txt
/tmp/9.txt
/tmp/14.txt
/tmp/19.txt
/tmp/4.txt
/tmp/17.txt
/tmp/1.txt
/tmp/5.txt
/tmp/15.txt
/tmp/16.txt
/tmp/etc/pki/nssdb/pkcs11.txt
/tmp/7.txt
/tmp/12.txt
/tmp/13.txt
/tmp/6.txt
/tmp/test.txt
/tmp/11.txt
2.查找/tmp目录下名字为'test1.txt'的文件
[root@moban ~]# find /tmp -type f -name "test1.txt"
/tmp/data/test1.txt
/tmp/test1.txt
3.查找/tmp目录下所有以".txt"结尾的文件,查找目录的深度为2
[root@moban ~]# find /tmp -maxdepth 2 -type f -name "*.txt"
/tmp/18.txt
/tmp/20.txt
/tmp/3.txt
/tmp/8.txt
/tmp/10.txt
/tmp/2.txt
/tmp/data/test1.txt
/tmp/9.txt
/tmp/test1.txt
/tmp/14.txt
/tmp/19.txt
/tmp/4.txt
/tmp/17.txt
/tmp/1.txt
/tmp/5.txt
/tmp/15.txt
/tmp/16.txt
/tmp/7.txt
/tmp/12.txt
/tmp/13.txt
/tmp/6.txt
/tmp/test.txt
/tmp/11.txt
4.通过find查找/tmp目录下的所有.txt文件,并查看其相关的文件权限
[root@moban tmp]# find /tmp -type f -name "*.txt" -exec ls -l {} \; ====>相当于find /tmp -type f -name "*.txt" | xargs ls -l
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/20.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/3.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/8.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/2.txt
-rw-r--r-- 1 root root 47 Jan 21 15:58 /tmp/data/test1.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/9.txt
-rw-r--r-- 1 root root 0 Jan 21 22:02 /tmp/test1.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/4.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/5.txt
-rw-r--r-- 1 root root 16 Jan 25 15:10 /tmp/data.txt
-rw-r--r-- 1 root root 0 Jan 1 2016 /tmp/testlab.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/7.txt
-rw-r--r-- 1 root root 0 Jan 26 15:58 /tmp/testPython/dir1/subdir1/sa.txt
-rw-r--r-- 1 root root 12 Jan 26 15:59 /tmp/testPython/dir1/c.txt
-rw-r--r-- 1 root root 12 Jan 26 16:00 /tmp/testPython/dir1/a_copy.txt
-rw-r--r-- 1 root root 4 Jan 26 16:13 /tmp/testPython/dir1/b.txt
-rw-r--r-- 1 root root 12 Jan 26 15:59 /tmp/testPython/dir1/a.txt
-rw-r--r-- 1 root root 0 Jan 26 15:58 /tmp/testPython/dir2/subdir1/sb.txt
-rw-r--r-- 1 root root 12 Jan 26 15:59 /tmp/testPython/dir2/c.txt
-rwxrwxrwx 1 root root 12 Jan 26 16:02 /tmp/testPython/dir2/d.txt
-rw-r--r-- 1 root root 0 Jan 26 15:57 /tmp/testPython/dir2/b.txt
-rw-r--r-- 1 root root 12 Jan 26 15:59 /tmp/testPython/dir2/a.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/6.txt
-rw-r--r-- 1 root root 1213 Jan 21 23:42 /tmp/test.txt
十八、tree 树形显示目录结构 list contents of directories in a tree-like format.
命令重要参数:
-a 显示目录下所有的文件,包括隐藏文件
-d 只显示目录
-f 显示文件的全路径
-p 显示文件的类型及其权限
例子:
1.显示目录内的所有文件
[root@moban ~]# tree /tmp/
/tmp/
├── 1
├── 20.txt
├── 2.txt
├── 3.txt
├── 4.txt
├── 5.txt
├── 6.txt
├── 7.txt
├── 8.txt
├── 9.txt
├── data
│ ├── alex.avi
│ └── test1.txt
├── test1.txt
└── test.txt
2.只显示目录下的子目录
[root@moban ~]# tree -d /tmp/
/tmp/
└── data
1 directory
3.显示文件的全路径
[root@moban ~]# tree -f /tmp/
/tmp
├── /tmp/1
├── /tmp/20.txt
├── /tmp/2.txt
├── /tmp/3.txt
├── /tmp/4.txt
├── /tmp/5.txt
├── /tmp/6.txt
├── /tmp/7.txt
├── /tmp/8.txt
├── /tmp/9.txt
├── /tmp/data
│ ├── /tmp/data/alex.avi
│ └── /tmp/data/test1.txt
├── /tmp/test1.txt
└── /tmp/test.txt
4.显示文件的类型及其权限
[root@moban ~]# tree -p /tmp/
/tmp/
├── [-rw-r--r--] 1
├── [-rw-r--r--] 20.txt
├── [-rw-r--r--] 2.txt
├── [-rw-r--r--] 3.txt
├── [-rw-r--r--] 4.txt
├── [-rw-r--r--] 5.txt
├── [-rw-r--r--] 6.txt
├── [-rw-r--r--] 7.txt
├── [-rw-r--r--] 8.txt
├── [-rw-r--r--] 9.txt
├── [drwxr-xr-x] data
│ ├── [-rw-r--r--] alex.avi
│ └── [-rw-r--r--] test1.txt
├── [-rw-r--r--] test1.txt
└── [-rw-r--r--] test.txt
1 directory, 14 files
十九、seq 在屏幕上打印一个序列 print a sequence of numbers
命令重要参数:
-s --separator 设置打印序列的分割符号(默认使用换行符做为分割符号)
-f --format 设置打印的格式
-w --equal-width 打印的时候在列前添加零使其宽度保持一致
例子:
1.在屏幕上打印1*3*5*7*9
[root@moban ~]# seq -s "*" 1 2 10
1*3*5*7*9
2.在屏幕上打印str001到str010
[root@moban ~]# seq -f"str%03g" 1 10
str001
str002
str003
str004
str005
str006
str007
str008
str009
str010
3.在屏幕上打印0001到0010
[root@moban ~]# seq -f"%04g" 1 10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
4.打印20以内的所有奇数,并保持宽度一致
[root@moban ~]# seq -w 1 2 20
01
03
05
07
09
11
13
15
17
19
二十、xargs 从标准输入中建立并执行命令 build and execute command lines from standard input
命令重要参数:
-n 设置每行中参数的最大个数
例子:
1.将/tmp/test.txt文件的内容按照每行3列进行输出
[root@moban ~]# cat /tmp/test.txt
1
2
3
4
5
6
7
8
9
10
[root@moban ~]# xargs -n 3 < /tmp/test.txt
1 2 3
4 5 6
7 8 9
10
2.显示/tmp目录下所有".txt"文件的权限
[root@moban ~]# find /tmp -type f -name "*.txt" | xargs ls -l
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/20.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/2.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/3.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/4.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/5.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/6.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/7.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/8.txt
-rw-r--r-- 1 root root 0 Jan 21 21:59 /tmp/9.txt
-rw-r--r-- 1 root root 47 Jan 21 15:58 /tmp/data/test1.txt
-rw-r--r-- 1 root root 0 Jan 21 22:02 /tmp/test1.txt
-rw-r--r-- 1 root root 21 Jan 21 22:59 /tmp/test.txt
二十一、touch 在Linux中修改文件的时间戳(默认情况下文件不存在就创建一个空文件) change file timestamps
命令主要参数:
-c --no-create 如果文件不存在,就不创建文件;如果文件存在就修改时间戳
-a 只修改文件的访问时间(Only Access Time)
-d --date 使用自己设置的时间来修改文件的时间戳,而不采用默认的系统当前时间
例子:
1.在/tmp目录下创建一个名称为testlab.txt的文件
[root@moban ~]# stat /tmp/testlab.txt
File: `/tmp/testlab.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 915796 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-01-22 02:24:31.173336991 +0800
Modify: 2018-01-22 02:24:31.173336991 +0800
Change: 2018-01-22 02:24:31.173336991 +0800
2.将/tmp/testlab.txt文件的时间戳修改为2016-01-01
[root@moban ~]# touch -d 2016-01-01 /tmp/testlab.txt
[root@moban ~]# stat /tmp/testlab.txt
File: `/tmp/testlab.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 915796 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-01-01 00:00:00.000000000 +0800
Modify: 2016-01-01 00:00:00.000000000 +0800
Change: 2018-01-22 02:25:26.927354609 +0800
3.将/tmp/testlab.txt.文件的访问时间修改为系统当前时间
[root@moban ~]# touch -a $(echo date) /tmp/testlab.txt
[root@moban ~]# stat /tmp/testlab.txt
File: `/tmp/testlab.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 915796 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-01-22 02:26:33.654355913 +0800
Modify: 2016-01-01 00:00:00.000000000 +0800
Change: 2018-01-22 02:26:33.654355913 +0800
二十二、history 显示当前用户之前的操作记录 Display or manipulate the history list.
命令重要参数:
-c 清空用户的所有操作记录
二十三、useradd 在Linux下添加用户或者更新用户详细信息 create a new user or update default new user information
命令主要参数:
-d --home-dir 设置新建用户的家目录(这个目录不需要存在)
-g --gid 设置用户的组ID
-G --groups 设置用户从属于的其他组ID
-M 创建用户的时候不创建用户的家目录
-s --shell 指定用户登录时候使用的shell(/sbin/nologin相当于不让用户登录系统,参见系统虚拟账号的配置)
-u --uid 设置用户的ID
例子:
1.在系统中使用默认参数创建一个lab用户
[root@moban ~]# useradd lab
[root@moban ~]# id lab
uid=502(lab) gid=502(lab) groups=502(lab)
2.创建一个lab1用户,并指定用户的家目录为/lab1
[root@moban ~]# useradd -d /lab1 lab1
[root@moban ~]# ls -ld /lab1
drwx------ 2 lab1 lab1 4096 Jan 22 04:06 /lab1
[root@moban ~]# id lab1
uid=503(lab1) gid=503(lab1) groups=503(lab1)
二十三、userdel 删除用户 delete a user account and related files
命令重要参数:
-r --remove 删除用户的时候连带用户的家目录也删除(这个参数在工作中要慎用,因为我们并不知道用户家目录的文件是不是还有人要用)
二十四、groupadd 添加用户组 create a new group
命令重要参数:
-g --gid 创建用户组的时候自行设置组ID
二十五、groupdel 删除特点的用户组 delete a group
二十六、passwd 修改指定用户的密码 update user’s authentication tokens
命令重要参数:
--stdin 使用标准输入来更新用户的密码,是shell无需进行交互式的设置
例子:
1.将lab1用户的密码设置成为lab1@test.com
[root@moban ~]# echo "lab1@test.com" | passwd --stdin lab1
Changing password for user lab1.
passwd: all authentication tokens updated successfully.
二十七、whoami 显示当前正在使用的系统账号 print effective userid
例子:
1.打印系统当前正在使用的账号
[root@moban ~]# whoami
root
二十八、su 切换用户身份 run a shell with substitute user and group IDs
命令重要参数:
-, -l --login 切换到一个用户身份的时候连带环境变量一起切换(这个参数非常重要,切换用户的时候一定要加上)
二十九、chkconfig 查询或者更新每个运行级别下的系统服务 updates and queries runlevel information for system services
命令重要参数:
--level 需要设置那个级别开启或者关闭的服务
--add 需要添加自启动的服务
--del 删除由chkconfig管理的服务
--list 将对应运行级别的系统服务列表打印出来
例子:
1.查看chkconfig正在管理的系统的服务列表
[root@moban ~]# chkconfig --list
abrt-ccpp 0:off 1:off 2:off 3:on 4:off 5:on 6:off
abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off
cpuspeed 0:off 1:on 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
haldaemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off
ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
irqbalance 0:off 1:off 2:off 3:on 4:on 5:on 6:off
kdump 0:off 1:off 2:off 3:on 4:on 5:on 6:off
lvm2-monitor 0:off 1:on 2:on 3:on 4:on 5:on 6:off
mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
messagebus 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
ntpdate 0:off 1:off 2:off 3:off 4:off 5:off 6:off
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off
psacct 0:off 1:off 2:off 3:off 4:off 5:off 6:off
quota_nld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rngd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
smartd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
snmpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
snmptrapd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
svnserve 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
winbind 0:off 1:off 2:off 3:off 4:off 5:off 6:off
2.关闭postfix在level3上的自启动
[root@moban ~]# chkconfig --level 3 postfix off
[root@moban ~]# chkconfig --list | grep postfix
postfix 0:off 1:off 2:on 3:off 4:on 5:on 6:off
3.设置snmpd服务为开机自启动
[root@moban ~]# chkconfig --list | grep snmpd
snmpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@moban ~]# chkconfig snmpd on
[root@moban ~]# chkconfig --list | grep snmpd
snmpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
4.让chkconfig不再管理snmpd服务
[root@moban ~]# chkconfig --list | grep snmpd
snmpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@moban ~]# chkconfig --del snmpd
[root@moban ~]# chkconfig --list | grep snmpd
[root@moban ~]#
三十、df 显示系统磁盘的利用率 report file system disk space usage
命令重要参数:
-a --all 显示所有(包括虚拟文件系统)
-h --human-readable 用人类好识别的方式来显示文件的大小
-i --inodes 显示系统中inode的使用率
-T --print-type 显示文件系统的分区类型
例子:
1.查看系统当前的磁盘利用率
[root@moban ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 18759984 2007448 15792896 12% /
tmpfs 502068 0 502068 0% /dev/shm
/dev/sda1 194241 33777 150224 19% /boot
2.查看系统当前的磁盘利用率包括虚拟文件系统
[root@moban ~]# df -a
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 18759984 2007448 15792896 12% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
tmpfs 502068 0 502068 0% /dev/shm
/dev/sda1 194241 33777 150224 19% /boot
none 0 0 0 - /proc/sys/fs/binfmt_misc
3.查看系统inode的利用率
[root@moban ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda3 1201872 79903 1121969 7% /
tmpfs 125517 1 125516 1% /dev/shm
/dev/sda1 51200 38 51162 1% /boot
4.查看系统磁盘的利用率及其分区类型
[root@moban ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda3 ext4 18759984 2007448 15792896 12% /
tmpfs tmpfs 502068 0 502068 0% /dev/shm
/dev/sda1 ext4 194241 33777 150224 19% /boot
浙公网安备 33010602011771号