1.pwd Print the current working directory. With the -P option, pwd prints the physical directory, without any symbolic links; the -L option makes pwd follow symbolic links. 通常进入系统后,往往想知道自己当前位置,因此pwd就派上用场了,使用时注意一下-L,-P的区别,-P选项会显示物理路径。 ![]() ![]() 1 mengxialeideMBP:~ mengxianglei$ ls -ld /etc
2 lrwxr-xr-x@ 1 root wheel 11 12 2 2017 /etc -> private/etc
3 mengxialeideMBP:~ mengxianglei$ cd /etc
4 mengxialeideMBP:etc mengxianglei$ pwd
5 /etc
6 mengxialeideMBP:etc mengxianglei$ pwd -L
7 /etc
8 mengxialeideMBP:etc mengxianglei$ pwd -P
9 /private/etc
2.paste The paste utility concatenates the corresponding lines of the given input files, replacing all but the last file's newline characters with a single tab character, and writes the resulting lines to standard output. If end-of-file is reached on an input file while other input files still contain data, the file is treated as if it were an endless source of empty lines. 英文的意思粘贴,可以将两个文件粘合为一个,粘贴的方式时按照行进行粘合。 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat int.txt 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 mengxialeideMBP:mxl mengxianglei$ cat char.txt 13 a 14 b 15 c 16 d 17 e 18 f 19 g 20 h 21 i 22 j 23 mengxialeideMBP:mxl mengxianglei$ paste int.txt char.txt 24 1 a 25 2 b 26 3 c 27 4 d 28 5 e 29 6 f 30 7 g 31 8 h 32 9 i 33 10 j -d 选项可以指定粘合的分隔符 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ paste -d '*' char.txt int.txt
2 a*1
3 b*2
4 c*3
5 d*4
6 e*5
7 f*6
8 g*7
9 h*8
10 i*9
11 j*10
-s 选项使文本内容按照一行显示 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ seq 1000 > test.txt 2 mengxialeideMBP:mxl mengxianglei$ wc -l test.txt 3 1000 test.txt 4 mengxialeideMBP:mxl mengxianglei$ paste -s test.txt |wc -l 5 1 -sd配合使用格式单个文件,这个时候的分隔符是依次使用的 paste 还可以将列转行的时候,指定几行的内容归为一行,- 代表一行的内容,下面是把三行归为一行进行处理 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ paste - - - <char.txt 2 a b c 3 d e f 4 g h i 5 j 6 7 mengxialeideMBP:mxl mengxianglei$ paste -d '+-*' - - - -<char.txt 8 9 a+b-c*d 10 11 e+f-g*h 12 13 i+j-* 3.ps The ps utility displays a header line, followed by lines containing information about all of your processes that have controlling terminals. ps命令工作中常用,排查进程是否启动,获取进程号等等,通常可以结合grep获取到想要的进程的运行情况 -e选项显示所有进程,-f选项显示uip,pid等 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ ps -ef|grep server
2 55 51 1 0 251018 ?? 0:01.46 /System/Library/CoreServices/appleeventsd --server
3 0 364 1 0 251018 ?? 0:00.04 /System/Library/CoreServices/CrashReporterSupportHelper server-init
4 0 408 1 0 251018 ?? 0:00.19 /usr/sbin/systemsoundserverd
5 0 567 1 0 261018 ?? 0:00.23 /System/Library/CoreServices/SubmitDiagInfo server-init
6 501 3621 3488 0 11:40上午 ttys000 0:00.00 grep server
-u可以获取到指定用户下的相关进程 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ ps -u mengxianglei 2 UID PID TTY TIME CMD 3 501 265 ?? 0:06.43 /usr/sbin/cfprefsd agent 4 501 266 ?? 0:12.98 /usr/libexec/UserEventAgent (Aqua) 5 501 268 ?? 0:19.82 /usr/sbin/distnoted agent 4.cd Change the current directory to DIR 更改当前的目录到指定的路径 用法比较简单,cd 后面直接跟需要跳转的路径即可。 特殊符号说明 ~代表用户家目录 -代表用户上一次的目录 . 代表用户当前目录 ..代表上一级目录 以/开头的是绝对路径,否则是以当前路径为基础的相对路径 5.mkdir The mkdir utility creates the directories named as operands, in the order specified, using mode rwxrwxrwx (0777) as modified by the current umask(2). 创建目录命令,类似window里面的信件文件夹 -p 如果创建多级目录,需要使用-p选项 -v 显示创建详细过程 创建目录时可以技巧性创建多个目录 ![]() ![]() 1 mkdir -p ./mulu/{1,2}
2 mengxialeideMBP:mxl mengxianglei$ cd mulu/
3 mengxialeideMBP:mulu mengxianglei$ ls
4 1 2
5 mengxialeideMBP:mulu mengxianglei$
![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ mkdir -p ./mulu/{1..3} 2 mengxialeideMBP:mxl mengxianglei$ cd mulu 3 mengxialeideMBP:mulu mengxianglei$ ls 4 1 2 3 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat mkmulu.txt
2 ./mulu/1
3 ./mulu/2
4 ./mulu/3
5 ./mulu/4
6 mengxialeideMBP:mxl mengxianglei$ mkdir -p `cat mkmulu.txt`
7 mengxialeideMBP:mxl mengxianglei$ cd mulu
8 mengxialeideMBP:mulu mengxianglei$ ls
9 1 2 3 4
6.touch The touch utility sets the modification and access times of files. If any file does not exist, it is created with default permissions. 创建空文件,如果文件存在修改文件时间 linux文件时间介绍 atime:文件的最后访问时间 mtime:最后的文件修改时间(修改文件内容) ctime:最后的文件状态改变时间(访问、修改文件内容、文件移动、权限改变) ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ touch touchfile 2 mengxialeideMBP:mxl mengxianglei$ ls -l 3 total 0 4 -rw-r--r-- 1 mengxianglei staff 0 11 4 13:06 touchfile 5 mengxialeideMBP:mxl mengxianglei$ stat touchfile 6 16777220 3212171 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:06:48 2018' 'Nov 4 13:06:48 2018' 'Nov 4 13:06:48 2018' 'Nov 4 13:06:48 2018' 4194304 0 0 touchfile 7 mengxialeideMBP:mxl mengxianglei$ touch -a touchfile 8 mengxialeideMBP:mxl mengxianglei$ stat touchfile 9 16777220 3212171 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:07:41 2018' 'Nov 4 13:06:48 2018' 'Nov 4 13:07:41 2018' 'Nov 4 13:06:48 2018' 4194304 0 0 touchfile 10 mengxialeideMBP:mxl mengxianglei$ touch -m touchfile 11 mengxialeideMBP:mxl mengxianglei$ stat touchfile 12 16777220 3212171 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:07:41 2018' 'Nov 4 13:08:55 2018' 'Nov 4 13:08:55 2018' 'Nov 4 13:06:48 2018' 4194304 0 0 touchfile -r 选项,可以修改目标文件时间属性与源文件一样 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ touch 1.txt
2 mengxialeideMBP:mxl mengxianglei$ stat 1.txt
3 16777220 3212193 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:12:18 2018' 'Nov 4 13:12:18 2018' 'Nov 4 13:12:18 2018' 'Nov 4 13:12:18 2018' 4194304 0 0 1.txt
4 mengxialeideMBP:mxl mengxianglei$ touch 2.txt
5 mengxialeideMBP:mxl mengxianglei$ stat 2.txt
6 16777220 3212194 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:13:00 2018' 'Nov 4 13:13:00 2018' 'Nov 4 13:13:00 2018' 'Nov 4 13:13:00 2018' 4194304 0 0 2.txt
7 mengxialeideMBP:mxl mengxianglei$ touch -r 1.txt 2.txt
8 mengxialeideMBP:mxl mengxianglei$ stat 2.txt
9 16777220 3212194 -rw-r--r-- 1 mengxianglei staff 0 0 'Nov 4 13:12:18 2018' 'Nov 4 13:12:18 2018' 'Nov 4 13:13:14 2018' 'Nov 4 13:12:18 2018' 4194304 0 0 2.txt
7.ls list directory contents 显示目录内容 常用参数介绍: l 长格式显示 a 显示所有文件包扩隐藏文件 A 显示所有文件包括隐藏文件,但不显示.、.. t 按照最后修改时间(mtime)排序 r 相反次序 F 目录后面加/ i 显示inode节点信息 d 显示目录本身信息 h 人类可读 --full-time 显示完整时间 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ ls -AlrtFih 2 total 0 3 3212194 -rw-r--r-- 1 mengxianglei staff 0B 11 4 13:12 2.txt 4 3212193 -rw-r--r-- 1 mengxianglei staff 0B 11 4 13:17 1.txt 5 3212415 drwxr-xr-x 2 mengxianglei staff 64B 11 4 13:38 mulu/ 8.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 target_directory. The names of the files themselves are not changed. If cp detects an attempt to copy a file to itself, the copy will fail. 复制文件或目录,通常对一些文件的修改,我们要先备份一下,因此cp命令比较常用 -r 复制目录时递归复制,必须加上-r选项 -p 保留文件所有者、权限信息、时间属性 -d 如果文件为链接文件,仅复制链接文件本身 -a 相当于drp ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ ls -ld script/
2 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:36 script/
3 mengxialeideMBP:mxl mengxianglei$ cp -r script 'script.$(date +%F)'
4 mengxialeideMBP:mxl mengxianglei$ ls -l
5 total 0
6 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:36 script
7 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:37 script.2018-11-04
-i 如果存在相同文件,则提示用户进行确认 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ touch getdate.sh getcurrentdate.sh 2 mengxialeideMBP:script mengxianglei$ cp -i getdate.sh getcurrentdate.sh 3 overwrite getcurrentdate.sh? (y/n [n]) y 9.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. 移动文件或目录,如果目标不存在,则重命名 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ mkdir dir{1..5}
2 mengxialeideMBP:script mengxianglei$ ls
3 dir1 dir2 dir3 dir4 dir5
4 mengxialeideMBP:script mengxianglei$ mv dir1 dir6
5 mengxialeideMBP:script mengxianglei$ ls -ld
6 drwxr-xr-x 7 mengxianglei staff 224 11 4 14:52 .
7 mengxialeideMBP:script mengxianglei$ ls -l ./
8 total 0
9 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir2
10 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir3
11 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir4
12 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir5
13 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir6
14 mengxialeideMBP:script mengxianglei$ mv dir2 dir6
15 mengxialeideMBP:script mengxianglei$ ls -l ./
16 total 0
17 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir3
18 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir4
19 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir5
20 drwxr-xr-x 3 mengxianglei staff 96 11 4 14:52 dir6
21 mengxialeideMBP:script mengxianglei$ ls -l dir6
22 total 0
23 drwxr-xr-x 2 mengxianglei staff 64 11 4 14:51 dir2
10.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. 删除文件或者目录,系统运行会产生一些垃圾文件,因此为了节省磁盘,会经常将不需要的备份或者垃圾文件进行删除操作 -r 可以递归删除目录及目录里面的内容 -f 强制删除,无需提示 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ rm -rf dir6 2 mengxialeideMBP:script mengxianglei$ ls 3 dir3 dir4 dir5 通常可以结合find命令进行文件删除操作 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ ls
2 dir3 dir4 dir5
3 mengxialeideMBP:script mengxianglei$ find ./ -type d -name 'dir3'|xargs rm -rf
4 mengxialeideMBP:script mengxianglei$ ls
5 dir4 dir5
11.ln The ln utility creates a new directory entry (linked file) which has the same modes as the original file. It is useful for maintaining multiple copies of a file in many places at once without using up storage for the ``copies''; instead, a link ``points'' to the original copy. There are two types of links; hard links and symbolic links. How a link ``points'' to a file is one of the differences between a hard and symbolic link. linux下创建链接分为硬链接合软链接,两种链接方式时由区别的,默认创建硬链接,-s参数创建软链接,即快捷方式。硬链接相当于文件的另一个入口,与源文件是同一个inode编号,软链接则由自己的inode,是一个链接文件。 平时的应用创建软链接的情况较多,另外,目录是不可以创建硬链接的。 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ touch file1 2 mengxialeideMBP:script mengxianglei$ ln file1 hard_link 3 mengxialeideMBP:script mengxianglei$ ln -s file1 soft_link 4 mengxialeideMBP:script mengxianglei$ ls -l 5 total 0 6 -rw-r--r-- 2 mengxianglei staff 0 11 4 15:15 file1 7 -rw-r--r-- 2 mengxianglei staff 0 11 4 15:15 hard_link 8 lrwxr-xr-x 1 mengxianglei staff 5 11 4 15:16 soft_link -> file1 9 mengxialeideMBP:script mengxianglei$ ls -lhi 10 total 0 11 3213173 -rw-r--r-- 2 mengxianglei staff 0B 11 4 15:15 file1 12 3213173 -rw-r--r-- 2 mengxianglei staff 0B 11 4 15:15 hard_link 13 3213177 lrwxr-xr-x 1 mengxianglei staff 5B 11 4 15:16 soft_link -> file1 14 mengxialeideMBP:script mengxianglei$ 12.find The find utility recursively descends the directory tree for each path listed, evaluating an expression (composed of the ``primaries'' and ``operands'' listed below) in terms of each file in the tree. 通常为了查找一些内容,进行find命令,比较常用。 -type 查找类型 -name 文件名称 -mtime 修改时间 +、- ! 取反 -user 属于某个用户的文件 -nouser 不属于任何用户的文件 -maxdepth 1 最深一级目录 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ touch {1..5}.txt
2 mengxialeideMBP:script mengxianglei$ ls
3 1.txt 2.txt 3.txt 4.txt 5.txt
4 mengxialeideMBP:script mengxianglei$ find ./ -name '1.txt' |xargs rm
5 mengxialeideMBP:script mengxianglei$ ls
6 2.txt 3.txt 4.txt 5.txt
7 mengxialeideMBP:script mengxianglei$ find ./ -name '2.txt' -exec rm {} \;
8 mengxialeideMBP:script mengxianglei$ ls
9 3.txt 4.txt 5.txt
通过-exec命令,给找到的文件批量改名字,逐条处理 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ touch {1..9}.txt 2 mengxialeideMBP:script mengxianglei$ find ./ -name '*.txt' -exec mv {} {}.bak \; 3 mengxialeideMBP:script mengxianglei$ ls 4 1.txt.bak 2.txt.bak 3.txt.bak 4.txt.bak 5.txt.bak 6.txt.bak 7.txt.bak 8.txt.bak 9.txt.bak 通过xargs,将查找的结果集一起处理 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ find ./ -name '*.txt'|xargs
2 .//9.txt .//8.txt .//5.txt .//4.txt .//6.txt .//7.txt .//3.txt .//2.txt .//1.txt
3 mengxialeideMBP:script mengxianglei$ find ./ -name '*.txt'|xargs rm -r
13.xargs The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments 标准输入处理为入参 -n 每行显示个数 -d 指定分隔符号 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ cat int.txt 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 mengxialeideMBP:script mengxianglei$ xargs < int.txt 13 1 2 3 4 5 6 7 8 9 10 14 mengxialeideMBP:script mengxianglei$ xargs -n 2 < int.txt 15 1 2 16 3 4 17 5 6 18 7 8 19 9 10 14.rename 替换文件名 rename from to file 15.basename 显示文件名,不显示路径 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ ls
2 int.txt
3 mengxialeideMBP:script mengxianglei$ basename int.txt
4 int.txt
5 mengxialeideMBP:script mengxianglei$ basename int.txt .txt
6 int
16.dirname 显示路径,参数如果是相对路径,则返回内容也同样相对,参数如果是绝对路径,返回内容也是绝对。 ![]() ![]() 1 mengxialeideMBP:script mengxianglei$ dirname /Users/mengxianglei/mxl/script/int.txt 2 /Users/mengxianglei/mxl/script 3 mengxialeideMBP:script mengxianglei$ dirname int.txt 4 . 17.chattr 修改文件扩展属性 a 只能想文件追加数据。+a 、 -a i. 文件不能被删除、改名、写入或者新增 +i、-i 18.lsattr 查看文件扩展属性 19.chown The chown utility changes the user ID and/or the group ID of the specified files. Symbolic links named by arguments are silently left unchanged unless -h is used. 更改文件所属用户即属主 -R 递归更改目录下文件 ![]() ![]() 1 chown mengxianglei ./dir1
2 chown -R mengxianglei.staff ./dir
3 chown -R .staff ./dir
4 chown -R :staff ./dir
20.chmod 改变文件或者目录权限 -R 递归处理 r 读权限 w 写权限 x 执行权限 4,2,1 通过4,2,1来计算,可以理解4,2,1为读写执行的权重,计算的总和即文件的执行权限。 ls -l 查看文件时候,第一位为文件的类型,后面9位分别代表属主、属组、其他用户的读、写、执行 21.cat cat -- concatenate and print files 查看或者聚合文件,与paste不同的是,是按照文件内容依次结合,而非逐行结合 通过cat将两个文件内容简单合并 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat int.txt 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 mengxialeideMBP:mxl mengxianglei$ cat char.txt 13 a 14 b 15 c 16 d 17 e 18 f 19 g 20 h 21 i 22 j 23 k 24 l 25 m 26 n 27 o 28 p 29 q 30 r 31 s 32 t 33 u 34 v 35 w 36 x 37 y 38 z 39 mengxialeideMBP:mxl mengxianglei$ cat int.txt char.txt 40 1 41 2 42 3 43 4 44 5 45 6 46 7 47 8 48 9 49 10 50 a 51 b 52 c 53 d 54 e 55 f 56 g 57 h 58 i 59 j 60 k 61 l 62 m 63 n 64 o 65 p 66 q 67 r 68 s 69 t 70 u 71 v 72 w 73 x 74 y 75 z -n 显示行号 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat -n char.txt
2 1 a
3 2 b
4 3 c
5 4 d
6 5 e
7 6 f
8 7 g
9 8 h
10 9 i
11 10 j
12 11 k
13 12 l
14 13 m
15 14 n
16 15 o
17 16 p
18 17 q
19 18 r
20 19 s
21 20 t
22 21 u
23 22 v
24 23 w
25 24 x
26 25 y
27 26 z
通过cat命令清空文件内容 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat /dev/null>test.txt 2 mengxialeideMBP:mxl mengxianglei$ cat test.txt 3 mengxialeideMBP:mxl mengxianglei$ 通过cat 非交互式写入文件内容,如果结尾<<-EOF,最后的EOF可以不顶格 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat >>test.txt<<EOF
2 > aaa
3 > ddd
4 > ddd
5 > EOF
6 mengxialeideMBP:mxl mengxianglei$ cat test.txt
7 aaa
8 ddd
9 ddd
22.less 根据需要加载文件,支持分页查看 -N 显示每页行号 输入空格,跳转到下一页,输入b,返回到上一页,上下键逐行移动 23.head head -- display first lines of a file 显示文件开头几行 -n 指定显示的行数 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ head -n 11 int.txt 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 24.tail tail -- display the last part of a file 显示文件的后面行数,通常查看日志时常用该命令监控日志文件信息 -n 指定显示多少行 -f 实时监控文件变化 -F 实时监控文件变化,文件不存在是不报错,一直等待 25.cut The cut utility cuts out selected portions of each line (as specified by list) from each file and writes them to the standard output. If no file arguments are specified, or a file argument is a single dash (`-'), cut reads from the standard input. The items specified by list can be in terms of column position or in terms of fields delimited by a special character. Column numbering starts from 1. 从文本中提取文字 -b 以字节单位进行分隔 -c 以字符为单位进行分隔 -d 指定分隔符 -f 指定显示区域 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat char.txt
2 a b c d e f g h i j k l m n o p q r s t u v w x y z
3 mengxialeideMBP:mxl mengxianglei$ cut -c 1-5 char.txt
4 a b c
5 mengxialeideMBP:mxl mengxianglei$ cut -b 1-5 char.txt
6 a b c
7 mengxialeideMBP:mxl mengxianglei$ cut -b 1-5,7 char.txt
8 a b cd
9 mengxialeideMBP:mxl mengxianglei$ cut -d ' ' -f 2 char.txt
10 b
![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cut -d : -f 1 /etc/passwd 2 ## 3 # User Database 4 # 5 # Note that this file is consulted directly only when the system is running 6 # in single-user mode. At other times this information is provided by 7 # Open Directory. 8 # 9 # See the opendirectoryd(8) man page for additional information about 10 # Open Directory. 11 ## 12 nobody 13 root 14 daemon 15 _uucp 16 _taskgated 17 _networkd 18 _installassistant 19 _lp 20 _postfix 21 _scsd 22 _ces 23 _appstore 24 _mcxalr 25 _appleevents 26 _geod 27 _serialnumberd 28 _devdocs 29 _sandbox 30 _mdnsresponder 26.split split -- split a file into pieces 将大文件分割为小软件 -l 指定分隔的行数 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ wc -l int.txt
2 100 int.txt
3 mengxialeideMBP:mxl mengxianglei$ split -l 30 int.txt int_s_
4 mengxialeideMBP:mxl mengxianglei$ ls -l
5 total 40
6 -rw-r--r-- 1 mengxianglei staff 292 11 5 00:02 int.txt
7 -rw-r--r-- 1 mengxianglei staff 81 11 5 00:12 int_s_aa
8 -rw-r--r-- 1 mengxianglei staff 90 11 5 00:12 int_s_ab
9 -rw-r--r-- 1 mengxianglei staff 90 11 5 00:12 int_s_ac
10 -rw-r--r-- 1 mengxianglei staff 31 11 5 00:12 int_s_ad
-a 指定文件扩展名长度 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ split -l 10 -a 5 int.txt 2 mengxialeideMBP:mxl mengxianglei$ ls -l 3 total 88 4 -rw-r--r-- 1 mengxianglei staff 292 11 5 00:02 int.txt 5 -rw-r--r-- 1 mengxianglei staff 21 11 5 00:16 xaaaaa 6 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaab 7 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaac 8 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaad 9 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaae 10 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaaf 11 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaag 12 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaah 13 -rw-r--r-- 1 mengxianglei staff 30 11 5 00:16 xaaaai 14 -rw-r--r-- 1 mengxianglei staff 31 11 5 00:16 xaaaaj 27.sort sort -- sort or merge records (lines) of text and binary files -n 按照数值大小排序 -r 倒叙 -t 指定分隔 -k 指定排序 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat test.txt
2 1 z
3 2 y
4 3 x
5 4 w
6 5 v
7 6 u
8 7 t
9 8 s
10 9 r
11 10 q
12 p
13 o
14 n
15 m
16 l
17 k
18 j
19 i
20 h
21 g
22 f
23 e
24 d
25 c
26 b
27 a
28 mengxialeideMBP:mxl mengxianglei$ sort -t ' ' -k 2 test.txt
29 a
30 b
31 c
32 d
33 e
34 f
35 g
36 h
37 i
38 j
39 k
40 l
41 m
42 n
43 o
44 p
45 1 z
46 10 q
47 2 y
48 3 x
49 4 w
50 5 v
51 6 u
52 7 t
53 8 s
54 9 r
28.uniq The uniq utility reads the specified input_file comparing adjacent lines, and writes a copy of each unique input line to the output_file. If input_file is a single dash (`-') or absent, the standard input is read. If output_file is absent, standard output is used for output. The second and succeeding copies of identical adjacent input lines are not written. Repeated lines in the input will not be detected if they are not adjacent, so it may be necessary to sort the files first. 去除重复行,只能去除相邻的重复行,如果想去除所有重复行,需要先排序 -c 去除重复行,统计出重复行的个数 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ sort -n int.txt |uniq -c 2 5 1 3 5 2 4 5 3 5 5 4 6 5 5 7 5 6 8 5 7 9 5 8 10 5 9 11 5 10 29.wc wc -- word, line, character, and byte count -l 统计行数 -L 统计最长行长度 -m 统计字符数 30.diff diff - compare files line by line 逐行比较文件 31.tr The tr utility copies the standard input to the standard output with substitution or deletion of selected characters. -d 删除字符 -s 保留连续字符的第一个字符 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat test.txt 2 aafdfdsfsfaaaaafsdfsdffn1122111fsf343 3 mengxialeideMBP:mxl mengxianglei$ tr 'abc' '123' <test.txt 4 11fdfdsfsf11111fsdfsdffn1122111fsf343 5 mengxialeideMBP:mxl mengxianglei$ tr -d 'a' <test.txt 6 fdfdsfsffsdfsdffn1122111fsf343 7 mengxialeideMBP:mxl mengxianglei$ tr -s 'a' <test.txt 8 afdfdsfsfafsdfsdffn1122111fsf343 32.tee tee -- pipe fitting 多重定向 -a 追加 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat a.txt|tee b.txt
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 10
12 mengxialeideMBP:mxl mengxianglei$ cat a.txt|tee -a b.txt
13 1
14 2
15 3
16 4
17 5
18 6
19 7
20 8
21 9
22 10
23 mengxialeideMBP:mxl mengxianglei$ cat b.txt
24 1
25 2
26 3
27 4
28 5
29 6
30 7
31 8
32 9
33 10
34 1
35 2
36 3
37 4
38 5
39 6
40 7
41 8
42 9
43 10
34.grep the grep utility searches any given input files, selecting lines thatmatch one or more patterns. -v 排除,显示不匹配的行 ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ cat a.txt 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 mengxialeideMBP:mxl mengxianglei$ grep -v 2 a.txt 13 1 14 3 15 4 16 5 17 6 18 7 19 8 20 9 21 10 -n 显示匹配行或者行号 -i 不区分大小写 -E 扩展的grep ![]() ![]() 1 mengxialeideMBP:mxl mengxianglei$ grep '1|2' a.txt
2 mengxialeideMBP:mxl mengxianglei$ grep -E '1|2' a.txt
3 1
4 2
5 10
--color=auto -w 只匹配过滤的单词 去除空行和日志 35.sed the sed utility reads the specified files, or the standard input if no files are specified, modifying the input as specified by a list of commands. 添加内容指定行前增加内容 sed '2i 2' int.txt 36.awk awk scans each input file for lines that match any of a set of patterns specified literally in prog or in one or more files specified as -f progfile. -F 指定分隔符 NR 行号 NF 尾 $0 整行 输出指定的行信息 输出指定行范围信息 gsub函数使用 gsub('替换对象',“替换成什么内容”,哪一列) 获取以太网eth0的ip地址 37.uname uname -- Print operating system name -r 显示内核版本 -m 显示硬件架构 -a 显示所有信息 -n 显示主机名 38.hostname hostname -- set or print name of current host system /etc/sysconfig/network /etc/hostname -a显示主机别名 -i 主机IP -I 主机IP,不依赖DNS解析,速度快 39.stat 40.du 41.date 42.echo |
|