Day 6 文件管理 file management
一 输出与重定向 Input/Output redirection
Output redirection
符号>代表以覆盖的方式将内容定向到文件中,意思是替换原文件内容
符号>>以追加的方式将内容定向到文件中,意思在原文末尾添加内容
If a command has its output redirected to a file and the file already contains some data, that data will be lost.
You can use >> operator to append the output in an existing file
Error Redirection
ls >> 1.txt 2>&1 #将ls的执行结果重定向到1.txt文件中
ls 2&>> 1.txt #同上,两种书写格式意思相同
ls >> 1.txt 2>>2.txt #正确输出和错误输出分别输入到1.txt和2.txt文件中
Input redirection
[root@localhost ~]# ip a > 1.txt 2>&1 #将查看到的ip地址无论正确错误输出重定向到1.txt文档中
[root@localhost ~]# tr 'e' 'E' < 1.txt #将1.txt文件中小写字母e替换成大写字母E进行显示
二 字符处理命令 Character Manipulation Commands
Sort 排序
-n 依照数值的大小排序
To sort a file numerically used –n option. -n option is also predefined in unix as the above options are. This option is used to sort the file with numeric data present inside.
-r 以相反的顺序来排序
Sorting In Reverse Order : You can perform a reverse-order sort using the -r flag. the -r flag is an option of the sort command which sorts the input file in reverse order i.e. descending order by default.
-k 以某列进行排序
Unix provides the feature of sorting a table on the basis of any column number by using -k option. Use the -k option to sort on a certain column. For example, use “-k 2” to sort on the second column.
-c 检查列表是否已经排序
This option is used to check if the file given is already sorted or not & checks if a file is already sorted pass the -c option to sort. This will write to standard output if there are lines that are out of order.The sort tool can be used to understand if this file is sorted and which lines are out of order
-u 仅显示出现一次的行
To sort and remove duplicates pass the -u option to sort. This will write a sorted list to standard output and remove duplicates. This option is helpful as the duplicates being removed gives us an redundant file.
-t 指定分割符,默认是以空格为分隔符
Define the delimiter
Uniq 去重
-c 在每列旁边显示该行重复出现的次数。
It tells how many times a line was repeated by displaying a number as a prefix with the line
-d 仅显示重复出现的行列。
It only prints the repeated lines and not the lines which aren’t repeated
-D 显示全部所有重复的行
It also prints only duplicate lines but not one per group.
-u 仅显示不重复出现的行
It prints only the unique lines.
-i 忽略大小写
It is used to make the comparison case-insensitive.
wc 统计
-c 统计文件的Bytes数;
This option displays count of bytes present in a file. With this option it display two-columnar output, 1st column shows number of bytes present in a file and 2nd is the file name.
-l 统计文件的行数;
This option prints the number of lines present in a file. With this option wc command displays two-columnar output, 1st column shows number of lines present in a file and 2nd itself represent the file name.
-w 统计文件中单词的个数,默认以空白字符做为分隔符
This option prints the number of words present in a file. With this option wc command displays two-columnar output, 1st column shows number of words present in a file and 2nd is the file name.
-m 统计文件中字母的个数
Using -m option ‘wc’ command displays count of characters from a file.
-L 统计文件中字母最多的一行的字母个数
The ‘wc’ command allow an argument -L, it can be used to print out the length of longest (number of characters) line in a file.
cut 删除指定字段
-d 指定字段的分隔符,默认的字段分隔符为"TAB";
-f 显示指定字段的内容;
By default the output delimiter is same as input delimiter that we specify in the cut with -d option. To change the output delimiter use the option –output-delimiter=”delimiter”.
Here cut command changes delimiter(%) in the standard output between the fields which is specified by using -f option .
tr 替换或删除
-d 删除
[root@localhost ~]# head -1 /etc/passwd |tr "root" "ROOT"
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
[root@localhost ~]#
[root@localhost ~]# head -1 /etc/passwd |tr -d "root"
:x:0:0::/:/bin/bash
[root@localhost ~]# echo "hello egon qq:378533872" > a.txt
[root@localhost ~]# tr "egon" "EGON" < a.txt
hEllO EGON qq:378533872
三 打包压缩 Package and Compress
打包压缩
[root@localhost test]# tar czvf etc1_bak.tar.gz /etc/ # 选项z代表gzip压缩算法
c创建 v详细 f打包后文件路径
[root@localhost test]# tar cjvf etc1_bak.tar.bz2 /etc/ # 选项j代表bzip2压缩算法
解压缩
#1、针对xxx.tar.gz 或者 xxx.tar.bz2,统一使用
[root@localhost test]# tar xvf 压缩包 -C 解压到的目录 # 无需指定解压算法,tar会自动判断
•
#2、针对xxx.zip,用unzip
选项:
-l #显示压缩包的列表信息
-q #静默输出
-d #解压到指定的目录
[root@localhost test]# unzip -q xxx.zip -d /opt