首页 > 代码库 > linux常用命令
linux常用命令
1、find用法
[root@template tmp]# find / -type f -name "text.txt"/tmp/text.txt/root/text.txt
#找到文件后,交给管道删除
[root@template tmp]# find / -type f -name "text.txt" | xargs rm -f
面试题:删除一个目录下的所有文件,但保留一个指定文件(保留file10)
[root@template tmp]# touch file{1..10}[root@template tmp]# ll总用量 0-rw-r--r-- 1 root root 0 5月 20 10:15 file1-rw-r--r-- 1 root root 0 5月 20 10:11 file10-rw-r--r-- 1 root root 0 5月 20 10:15 file2-rw-r--r-- 1 root root 0 5月 20 10:15 file3-rw-r--r-- 1 root root 0 5月 20 10:15 file4-rw-r--r-- 1 root root 0 5月 20 10:15 file5-rw-r--r-- 1 root root 0 5月 20 10:15 file6-rw-r--r-- 1 root root 0 5月 20 10:15 file7-rw-r--r-- 1 root root 0 5月 20 10:15 file8-rw-r--r-- 1 root root 0 5月 20 10:15 file9#方法一:[root@template tmp]# find /tmp -type f ! -name "file10"|xargs rm -f方法二:[root@template tmp]# find /tmp -type f ! -name "file10" -exec rm -f {} \;
#find找到的内容,-exec是参数,{}:要查找的目标,一般可以不写 \ :反斜杠转义字符
!作用就是:取反
2、rm remove 删除文件或者目录
-f 强制
-r 删除目录
注意:生产场景尽量不要使用rm,如果非要用,一定要先cp备份
[root@template tmp]# ll总用量 0-rw-r--r-- 1 root root 0 5月 20 10:06 text.txt[root@template tmp]# rm -f text.txt [root@template tmp]# ll总用量 0
3、echo用法
打印输出内容,配合“>或>>” 可以为文件覆盖及追加内容
">" 意思是重定向,会清除文件里所有以前数据
">>" 为追加内容
4、cat 查看文件内容
特殊用法:增加多行内容
[root@template tmp]# cat >>/tmp/nulige.txt <<EOF> I am studying linux.> EOF[root@template tmp]# ll总用量 4-rw-r--r-- 1 root root 0 5月 20 10:11 file10-rw-r--r-- 1 root root 21 5月 20 10:33 nulige.txt[root@template tmp]# cat nulige.txt I am studying linux.
5、cp 命令
语法:
cp 源文件 目录文件(cp 的得要参数apr)
参数:
-a :相当于-pdr
-d :若源文件为链接文件(link file),则复制链接文件属性而非档案本身
-f :强制,右目录档案已经存在且无法开启,则移除后再尝试
-i: 若目标文件已经存在时,覆盖时会先询问
-p:连同档案的属性一起复制过去,而非使用默认属性
-r:递归,用于复制目录
-u:若目标文件存在,则目标文件比源文件旧时才复制
提示:如果源文件是多个,那么目标文件在最后,且是目录
6、
linux常用命令
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。