[步骤] Linux 加密压缩 (zip 版)

步骤一:创建测试目录和测试文件

# mkdir test
# touch test/test.txt

(补充:这里以创建 test 目录和里面的 test.txt 文件为例)

步骤二:加密压缩文件或目录

2.1 交互式加密压缩文件或目录

# zip -re test1.zip test
Enter password: 
Verify password: 
  adding: test/ (stored 0%)
  adding: test/test.txt (stored 0%)

(补充:这里以将 test 目录和里面的 test.txt 文件加密压缩成 test1.zip (压缩)包为例)

2.2 非交互式加密解压文件或目录

# zip -rP eternalcenter test2.zip test
  adding: test/ (stored 0%)
  adding: test/test.txt (stored 0%)

(补充:这里以将 test 目录和里面的 test.txt 文件加密压缩成 test2.zip (压缩)包并且将密码设置为 eternalcenter 为例)

步骤三:解压加密文件或目录

3.1 交互式解压加密文件或目录

3.1.1 删除原测试目录和里面的文件
# rm -rf test

(补充:这里以删除 test 目录和里面的文件为例)

3.1.2 交互式解压加密文件或目录
# unzip test2.zip
Archive:  test2.zip
   creating: test/
[test2.zip] test/test.txt password: 
 extracting: test/test.txt

(补充:这里以解压 test2.zip (压缩)包为例)

3.2 非交互式解压加密文件或目录

3.2.1 删除原测试目录和里面的文件
# rm -rf test

(补充:这里以删除 test 目录和里面的文件为例)

3.2.2 非交互式解压加密文件
# unzip -P eternalcenter test1.zip 
Archive:  test1.zip
   creating: test/
 extracting: test/test.txt  

(补充:这里以解压 test2.zip (压缩)包并且解压密码为 eternalcenter 为例)

[步骤] Linux rm 命令的监控

步骤一:将原来的 rm 命令进行备份

# cp /usr/bin/rm  /usr/bin/rm.original

步骤二:创建一个记录 rm 命令使用的脚本

# cat /usr/bin/rm
#!/bin/bash
log=/var/log/rm_command.log
echo "The $$ is calling rm command" >> $log
echo "The full command is $*" >> $log
echo
echo "now use this command to get more information: /bin/ps axwwo user,pid,ppid,%cpu,%mem,vsz,rss,stat,time,cmd" >>$log
/bin/ps axwwo user,pid,ppid,%cpu,%mem,vsz,rss,stat,time,cmd >>$log
/usr/bin/rm.original $*
echo "============================================================" >>$log

步骤三:给记录 rm 命令使用的脚本执行权限

# chmod 755 /usr/bin/rm.original

步骤四:下次使用 rm 命令后就可以监控 /var/log/rm_command.log 日志了

(步骤略)

[排错] 解决 Linux 执行命令时报错 “bash: $’\r’: command not found”

报错代码

bash: $'\r': command not found

分析

Windows 系统文件换行使用的换行符是 \r\n
Linux 系统文件换行使用的换行符是 \n
在 Windows 编辑的文本换行符可能就是 \r\n
要解决这个问题需要将换行符从 \r\n 换成 \n

解决方法

方法一:使用 dos2unix 转换文件换行符

1.1 安装 dos2unix 软件

# yum -y install dos2unix

1.2 使用 dos2unix 转换文件换行符

# dos2unix <file>

方法二:使用 vim 命令转换文件换行符

2.1 使用 vim 命令进入文件

# vim <file>

2.2 转换文件换行符

: set ff=unix

(注意:是在 vim 的末行模式下执行以上命令)

2.3 保存文件

: wq

(注意:是在 vim 的末行模式下执行以上命令)

[命令] Linux 命令 sort(对数字或字母进行排序)

内容一:sort 命令的选项

1) -b 排序时忽略每行前面的空格
2) -c 检查是否已排序
3) -f 排序时忽略大小写字母
4) -n 按照数值到大小进行排序
5) -o 将排序结果导入到指定文件
6) -r 以相反的顺序进行排序
7) -t 指定排序的分隔符
8) -k 以指定的列进行排序

内容二:sort 命令的案例

2.1 案例一:检查是否已经排序

# cat test.txt
3
5
4
2
1

# sort -c test.txt 
sort: test.txt:3: disorder: 4

(补充:这里以检查 test.txt 文件里的排列为例)

2.2 案例二:sort 排序 1 列数字

# cat test.txt
3
5
4
2
20
1

# sort -n test.txt 
1
2
3
4
5
20

(补充:这里以排列 test.txt 文件里的列为例)

2.3 案例三:sort 排序 1 列字母

# cat test.txt 
c
e
d
b
a

# sort test.txt 
a
b
c
d
e

(补充:这里以排列 test.txt 文件里的列为例)

2.4 案例四:sort 以相反的顺序进行排序

# cat test.txt 
c
e
d
b
a

# sort -r test.txt 
e
d
c
b
a

(补充:这里以排列 test.txt 文件里的列为例)

2.5 案例五:sort 以 2 列中的第 1 列进行排序

# cat test.txt 
3 d
5 c
4 a
2 e
1 b

# sort test.txt 
1 b
2 e
3 d
4 a
5 c

(补充:这里以排列 test.txt 文件里的列为例)

2.6 案例六:sort 以 2 列中的第 2 列进行排序

# cat test.txt 
3 d
5 c
4 a
2 e
1 b

# sort -k2 test.txt 
4 a
1 b
5 c
3 d
2 e

(补充:这里以排列 test.txt 文件里的列为例)

2.7 案例七:sort 对 IP 地址进行排序

# cat test.txt 
10.0.200.10
172.16.50.10
192.168.100.1
192.168.100.10
172.16.50.1
10.0.200.1

# sort test.txt
10.0.200.1
10.0.200.10
172.16.50.1
172.16.50.10
192.168.100.1
192.168.100.10

(补充:这里以排列 test.txt 文件里的列为例)

2.8 案例八:sort 以 IP 地址的第三组数字进行排序

# cat test.txt 
10.0.200.10
172.16.50.10
192.168.100.1
192.168.100.10
172.16.50.1
10.0.200.1

# sort -t'.' -k3n test.txt
172.16.50.1
172.16.50.10
192.168.100.1
192.168.100.10
10.0.200.1
10.0.200.10

(补充:这里以排列 test.txt 文件里的列为例)