Linux 常见的命令2
rm命令详解
删除/root/yyTest/目录下的文件yyTest.ini (系统会询问是否删除)
rm /root/yyTest/yyTest.ini
强行删除/root/yyTest/目录下的文件yyTest.ini(直接删除,系统不会提示)
rm -f /root/yyTest/yyTest.ini
删除/root/yyTest/目录下的所有.log文件
rm -f /root/yyTest/*.log
删除/root/yyTest/目录下的 polo/文件夹
rm -r /root/yyTest/polo/
强行删除/root/yyTest/目录下的 polo/文件夹
rm -rf /root/yyTest/polo/
删除/root/yyTest/目录下的所有内容
rm -rf /root/yyTest/*
创建文件命令详解
touch
创建一个文件
touch yyTest.ini
同时创建两个文件
touch test1.txt test2.txt
批量创建文件(如创建2000个文件)
touch test{0001..2000}.txt
更改文件 yyTest.ini时间为当前时间(yyTest.ini已存在)
touch yyTest.ini
使用>、>>
>
直接覆盖原文件,不会有任何提示
>>
追加在原文件末尾,不会覆盖原文件的内容
直接用>创建空文件
> test.ini
注:清除文件·:> test.ini
- 将history命令执行的结果保存到history.log文件中
[root@gxzs-solr1 ~]# history > history.log (history.log 文件 会自动生成)
[root@gxzs-solr1 ~]# cat history.log
- 执行命令 curl ‘xxx’ ,将其返回结果保存到 log.log 中
[root@gx-solr1 ~]# curl 'http://192.168.0.110:8983/solr/scan_detail/admin/file?_=1544066402749&contentType=text/plain;charset=utf-8&file=managed-schema&wt=json' > log.log
- 执行命令 cat /etc/hosts , 将其返回结果保存到 hosts.log 中
[root@slave1 ~]# cat /etc/hosts > hosts.log
[root@slave1 ~]# more hosts.log
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
注意:
使用 > ,执行命令时,每次都会新生成一个 > 后面的文件,将之前生成的文件替换掉(文件创建时间也会跟着改变)。
4. 使用 >> 向 hosts.log中追加 当前日期
[root@slave1 ~]# echo "当前日期是 `date`" >> hosts.log
[root@slave1 ~]# ls -l hosts.log
-rw-r--r--. 1 root root 11 Dec 13 16:04 hosts.log # 查看hosts.log 的日期
[root@slave1 ~]# cat hosts.log # 查看hosts.log 文件的内容
I am OK!
[root@slave1 ~]# echo "当前日期是 `date`" >> hosts.log # 向hosts.log中追加 当前日期
[root@slave1 ~]# cat hosts.log # 再次查看hosts.log 文件的内容
I am OK!
当前日期是 Thu Dec 13 16:05:18 CST 2018
[root@slave1 ~]# ls -l hosts.log # 再次查看hosts.log 的日期
-rw-r--r--. 1 root root 56 Dec 13 16:05 hosts.log
[root@slave1 ~]#
ls 创建文件(将结果写入文件)
ls > test.ini
ls >> test.ini
grep 创建文件(将结果写入文件)
ps -ef | grep java >test.ini
ps -ef | grep java >>test.ini
echo 创建文件(将结果写入文件)
echo $PATH > test.ini
echo $PATH >> test.ini
使用cp创建文件
只要目标文件是新文件则算创建文件
使用cat创建文件
简单使用>、>>
cat > test.ini
cat >> test.ini
其实用的也是 > 和 >> ,但是有一点不一样的是,敲完上述命令会进入 test.ini 的编辑模式,可以直接输入你想要写入的内容,最后按ctrl+z退出编辑模式自动保存
head命令详解
显示文件的前5行(两种方式)
head -n 5 test.txt
head -5 test.txt
显示文件的前100个字符
head -c 100 test.txt
显示文件的第10-20行
head -20 test.txt | tail -10
cat命令详解
获取test.txt文件所有内容
cat test.txt
无论是否为空行,都显示行号
cat -n test.txt
显示行号,除了空行
cat -b test.txt
连续读取两个文件,按顺序输出
cat test1.txt test2.txt
倒序输出
其实就是cat倒过来写即可
tac test.txt
nl命令详解
nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等等的功能。
显示行号,除了空行
默认就是这个
nl test.txt
nl -b t test.txt
无论是否为空行,都显示行号
nl -b a test.txt
行号靠最左显示
nl -n ln test.txt
行号靠最右显示
nl -n rn test.txt
行号靠最右显示,不足位数左边补0
nl -n rz test.txt
more命令详解
more 命令类似 cat ,不过会以一页一页的形式显示,更方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会往回(back)一页显示
每次显示5行
more -5 test.txt
从第5行开始显示
more +5 test.txt
每次翻页时,先清空屏幕内容
more -5 -p test.txt
若遇到连续两行以上的空白行,合并为一行空白行
more -s test.txt
执行more命令后,常用的操作
向下滚动一屏
z
空格键
ctrl+f
向上滚动一屏
b
ctrl+b
输出文件名和当前行的行号
:f
进入vim编辑器
v
退出more模式
q
less命令详解
查看文件
less test.txt
ps查看进程并通过less分页显示
ps -ef | less
显示当前行数的百分比
less -m test.txt
显示当前行数/总行数和百分比
less -M test.txt
显示连续空行为一行
less -s test.txt
进入less模式之后的操作
g:移动到第一行 G:移动到最后一行 u:向前移动半屏 d:向后移动半屏 f:向后移动指定行数或一屏
b:向前移动指定行数或一屏 j:向下移动一行 k:向上移动一行 q:结束less模式
find命令基础使用
find命令相对于locate这种非实时查找的搜索命令,大大增加了我们搜索的便捷度以及准确性;并且能够方便的帮助我们对大文件、特定类型的文件查找与删除,特别是有超多小碎文件的时候,更是方便至极…
根据属主 属组查找
-user username:查找属主是xx的文件
-group group:查找属组的xx文件
-uid useruid:查找uid号的文件
-gid groupid:查找gid号的文件
-nouser:查找没有属主的文件,即文件存在但是 user已被删除
-nogroup:查找没有属组的文件
根据文件类型查找
-type f:普通文件
-type d:目录文件
-type l:符号链接文件
-type s:套接字文件
-type b:块设备文件
-type c:字符设备文件
-type p:管道文件
根据大小查找
-size +10M :大于10m的文件
-size +10k:大于10k的文件
-size +1G:大于1G的文件
-size -1G:小于文件的文件
根据时间查找
一天为单位
-atime :访问时间
-mtime :修改时间
-ctime :改变时间
以分钟为单位:
-amin: 访问时间 -mmin:修改时间 -cmin:改变时间
根据权限查找
-perm +mode:
-perm +600:属主属组其他权限 只要有一个匹配就当成功;600代表三个对象,6属主 CentOS7上 使用 /600
-perm -600:每个对象都必须同时拥有其指定的权限,三个对象同时成立 如:-003表示其他用户必须有写与执行权限
组合条件查找
-a :与
-o :或
-not:非
! :非
处理动作
-print:打印到屏幕
-ls:查找到的文件 进行 ls
-delete:删除查找到的文件
-ok command {}\; 对查找的文件执行由command指定的命令,交互式
-exec command {}\;同上,非交互式
{}:代表前面find找到的 文件名称本身
例如:
find ./ -type f -exec cp {} {}.bak \; 将查找到的文件都复制出一个.bak文件
find查找后的动作传递模式
默认:查找到指定类型的文件时进行一次性传递
xargs:xargs命令即让find查找的传递模式为 查找一个传递一个到动作上,删除较多碎文件很好用,例如:find -type f | xargs command;
查找/home/test目录下的符号*.txt的文件
find /home/test -name "*.txt" -print
查找权限是755的
find /home/test -perm 755 -print
查找属主是test的
find /home/test -user test -print
查找数组是test的
find /home/test -group test -print
查找更改时间小于5天的
find /home/test -mtime -5 -print
查找更改时间大于3天的
find /home/test -mtime +3 -print
查找所有目录
find /home/test -type d -print
查找除了目录的所有文件
find /home/test ! -type d -print
查找文件
find /home/test -type f -print
查找符号链接文件
find /home/test -type l -pint
不包括/home/test/test/目录下的test.sh
find /home/test -name "test.sh" -prune /home/test/test -print
删除test.sh文件
find /home/test -name "test.sh" -type f -exec rm {} \;
显示以test开头的文件
find /home/test -name "*test*" -type f -exec more {} \;