linux常用命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux常用命令相关的知识,希望对你有一定的参考价值。

1、find用法

[[email protected] tmp]# find / -type f -name "text.txt"
/tmp/text.txt
/root/text.txt

#找到文件后,交给管道删除
[[email protected] tmp]# find / -type f -name "text.txt" | xargs rm -f

面试题:删除一个目录下的所有文件,但保留一个指定文件(保留file10)

[[email protected] tmp]# touch file{1..10}
[[email protected] 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


#方法一:
[[email protected] tmp]# find /tmp -type f ! -name "file10"|xargs rm -f

方法二:
[[email protected] tmp]# find /tmp -type f ! -name "file10" -exec rm -f {} \;

#find找到的内容,-exec是参数,{}:要查找的目标,一般可以不写 \ :反斜杠转义字符
!作用就是:取反

 2、rm  remove  删除文件或者目录 

    -f 强制

   -r 删除目录

 注意:生产场景尽量不要使用rm,如果非要用,一定要先cp备份

[[email protected] tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 5月  20 10:06 text.txt
[[email protected] tmp]# rm -f text.txt 
[[email protected] tmp]# ll
总用量 0

 3、echo用法

   打印输出内容,配合“>或>>” 可以为文件覆盖及追加内容

   ">" 意思是重定向,会清除文件里所有以前数据

   ">>" 为追加内容

4、cat 查看文件内容

  特殊用法:增加多行内容

[[email protected] tmp]# cat >>/tmp/nulige.txt <<EOF
> I am studying linux.
> EOF
[[email protected] 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
[[email protected] 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常用命令的主要内容,如果未能解决你的问题,请参考以下文章

linux中怎么查看mysql数据库版本

Linux常用文件管理命令

markdown [Docker] Docker片段列表和命令#linux #docker #snippets

Android 逆向Linux 文件权限 ( Linux 权限简介 | 系统权限 | 用户权限 | 匿名用户权限 | 读 | 写 | 执行 | 更改组 | 更改用户 | 粘滞 )(代码片段

C#常用代码片段备忘

常用python日期日志获取内容循环的代码片段