• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

来学Linux命令(二)

武飞扬头像
程序员皮卡秋
帮助1

前言

目前正在出一个Linux命令系列教程, 篇幅会较多, 喜欢的话,给个关注❤️ ~

作为服务端开发,linux命令还是要掌握一下的,可以做做基础性的运维。好了, 废话不多说直接开整吧~

cat & 将FILE或标准输入连接到标准输出

语法: cat [OPTION]... [FILE]...

这个命令大家肯定不陌生,有时候我们在服务器上查看日志,大多都会使用这个命令

-A, --show-all      等价于 -vET

-b, --number-nonblank  对非空输出行编号

-e            等价于 -vE

-E, --show-ends     在每行结束处显示

-n, --number   对输出的所有行编号,由1开始对所有输出的行数编号

-s, --squeeze-blank 有连续两行以上的空白行,就代换为一行的空白行

-t            与 -vT 等价

-T, --show-tabs     将跳格字符显示为 ^I


-v, --show-nonprinting  使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

比如我们需要显示行号:

[root@iZ2ze5vrnucj8nu52fq932Z cat]# cat -n a.txt
     1	hello world
     2	\nhello world
     3	\nhello world
     4	\nhello world
     5	\nhello world
     6	\nhello world
     7	\nhello world
     8	\nhello world
     9	\nhello world
    10	\nhello world
    11	\nhello world
    12	\nhello world
    13	\nhello world
    14	\nhello world
    15	\nhello world
    16	\nhello world
[root@iZ2ze5vrnucj8nu52fq932Z cat]# 

mkdir & 如果目录不存在,则创建目录

-m, --mode=模式,设定权限<模式> (类似 chmod),而不是 rwxrwxrwx 减 umask

-p, --parents 可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录;

-v, --verbose 每次创建新目录都显示信息

--help  显示此帮助信息并退出

--version 输出版本信息并退出

例如:


[root@iZ2ze5vrnucj8nu52fq932Z mkd]# mkdir test
[root@iZ2ze5vrnucj8nu52fq932Z mkd]# mkdir -p test1/tmp
[root@iZ2ze5vrnucj8nu52fq932Z mkd]# ls
test  test1
[root@iZ2ze5vrnucj8nu52fq932Z mkd]# 




root@iZ2ze5vrnucj8nu52fq932Z mkd]# mkdir -pm 777 test2/tmp
[root@iZ2ze5vrnucj8nu52fq932Z mkd]# ls -lh
total 12K
drwxr-xr-x 2 root root 4.0K Aug  1 09:38 test
drwxr-xr-x 3 root root 4.0K Aug  1 09:38 test1
drwxr-xr-x 3 root root 4.0K Aug  1 09:39 test2
[root@iZ2ze5vrnucj8nu52fq932Z mkd]# cd test2
[root@iZ2ze5vrnucj8nu52fq932Z test2]#

-v 参数可确定文件是否已经存在,如果不存在则会创建,并显示如下信息

[root@iZ2ze5vrnucj8nu52fq932Z mkd]# mkdir -v test
mkdir: cannot create directory ‘test’: File exists
[root@iZ2ze5vrnucj8nu52fq932Z mkd]# 



cd &  切换当前目录至指定目录

[root@iZ2ze5vrnucj8nu52fq932Z mkd]# ls
test  test1  test2
[root@iZ2ze5vrnucj8nu52fq932Z mkd]# cd ../
[root@iZ2ze5vrnucj8nu52fq932Z linux_study]# pwd
/root/linux_study
[root@iZ2ze5vrnucj8nu52fq932Z linux_study]# 

rm

程序尝试删除命令行上指定的非目录类型文件。如果文件的权限不允许写入,并且标准输入设备是终端,则会提示用户(在标准错误输出上)进行确认。

-f, --force  忽略不存在的文件,从不给出提示。


-i, --interactive 进行交互式删除

-r, -R, --recursive  指示rm将参数中列出的全部目录和子目录均递归地删除。

-d, --dir 删除空目录

使用示例:

[root@iZ2ze5vrnucj8nu52fq932Z rmd]# rm -i a.txt
rm: remove regular empty file ‘a.txt’? y
[root@iZ2ze5vrnucj8nu52fq932Z rmd]# ls
linux
[root@iZ2ze5vrnucj8nu52fq932Z rmd]# 


[root@iZ2ze5vrnucj8nu52fq932Z rmd]# rm -f linux
rm: cannot remove ‘linux’: Is a directory
[root@iZ2ze5vrnucj8nu52fq932Z rmd]# rm -rf linux
[root@iZ2ze5vrnucj8nu52fq932Z rmd]# ls
[root@iZ2ze5vrnucj8nu52fq932Z rmd]# 


总之rm命令慎用,尤其是rm -rf 不到万不得已,不要使用

mv & 移动目录或者文件到置顶目录下,同时具有重命名的功能

-b :若需覆盖文件,则覆盖前先行备份。

-f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;

-i :若目标文件 (destination) 已经存在时,就会询问是否覆盖

-n:不要覆盖现有文件。(-n选项将覆盖以前的任何-f或-i选项。)

-u :若目标文件已经存在,且 source 比较新,才会更新(update)

使用示例:

[root@iZ2ze5vrnucj8nu52fq932Z mvd]# ls
a.txt  b.txt
[root@iZ2ze5vrnucj8nu52fq932Z mvd]# mv a.txt c.txt   
[root@iZ2ze5vrnucj8nu52fq932Z mvd]# ls
b.txt  c.txt
[root@iZ2ze5vrnucj8nu52fq932Z mvd]# 


# 这里的文件也可以是指定目录下的文件

[root@iZ2ze5vrnucj8nu52fq932Z mvd]# pwd
/root/linux_study/mvd
[root@iZ2ze5vrnucj8nu52fq932Z mvd]# mv /root/linux_study/mvd/c.txt /root/linux_study/mvd/d.txt
[root@iZ2ze5vrnucj8nu52fq932Z mvd]# ls
b.txt  d.txt
[root@iZ2ze5vrnucj8nu52fq932Z mvd]# 


# 覆盖前先备份

[root@iZ2ze5vrnucj8nu52fq932Z mvd]# ls
d.txt  e.txt
[root@iZ2ze5vrnucj8nu52fq932Z mvd]# mv -b d.txt e.txt
mv: overwrite ‘e.txt’? y
[root@iZ2ze5vrnucj8nu52fq932Z mvd]# ls
e.txt  e.txt~
[root@iZ2ze5vrnucj8nu52fq932Z mvd]# 


cp & 拷贝

source_file的内容复制到target_file。在第二个大纲格式中,每个命名的source_file的内容都复制到目标target_directory。文件本身的名称不会更改。如果cp检测到尝试将文件复制到自身的尝试,则复制将失败。

-a, --archive  等于-dR --preserve=all

--backup[=CONTROL  为每个已存在的目标文件创建备份

-b        类似--backup 但不接受参数

--copy-contents    在递归处理是复制特殊文件内容

-d        等于--no-dereference --preserve=links

-f, --force    如果目标文件无法打开则将其移除并重试(当 -n 选项

存在时则不需再选此项)

-i, --interactive    覆盖前询问(使前面的 -n 选项失效)

-H        跟随源文件中的命令行符号链接

-l, --link      链接文件而不复制

-L, --dereference  总是跟随符号链接

-n, --no-clobber  不要覆盖已存在的文件(使前面的 -i 选项失效)

-P, --no-dereference  不跟随源文件中的符号链接

-p        等于--preserve=模式,所有权,时间戳

--preserve[=属性列表  保持指定的属性(默认:模式,所有权,时间戳),如果

可能保持附加属性:环境、链接、xattr 等

-R, -r, --recursive 复制目录及目录内的所有项目

使用示例:


# 复制自身会报错
[root@iZ2ze5vrnucj8nu52fq932Z cp]# ls
a.txt  b.txt
[root@iZ2ze5vrnucj8nu52fq932Z cp]# cp a.txt a.txt
cp: ‘a.txt’ and ‘a.txt’ are the same file
[root@iZ2ze5vrnucj8nu52fq932Z cp]# 


[root@iZ2ze5vrnucj8nu52fq932Z cp]# cp a.txt c.txt
[root@iZ2ze5vrnucj8nu52fq932Z cp]# ls
a.txt  b.txt  c.txt
[root@iZ2ze5vrnucj8nu52fq932Z cp]# 


echo & 标准输出

[root@iZ2ze5vrnucj8nu52fq932Z echo]# echo "hello world"
hello world
[root@iZ2ze5vrnucj8nu52fq932Z echo]# 

将内容输出到指定文件


[root@iZ2ze5vrnucj8nu52fq932Z echo]# ls
[root@iZ2ze5vrnucj8nu52fq932Z echo]# echo "hello world" >> a.txt
[root@iZ2ze5vrnucj8nu52fq932Z echo]# ls
a.txt
[root@iZ2ze5vrnucj8nu52fq932Z echo]# cat a.txt
hello world
[root@iZ2ze5vrnucj8nu52fq932Z echo]# 


结束语

命令很多,大家不用去背,可以放到便签之类的工具中,用到的时候翻一下就好~

本着把自己知道的都告诉大家,如果本文对您有所帮助,点赞 关注鼓励一下呗~

Linux相关文章

往期面试题相关文章

项目源码(源码已更新 欢迎star⭐️)

往期设计模式相关文章

设计模式项目源码(源码已更新 欢迎star⭐️)

Kafka 专题学习

项目源码(源码已更新 欢迎star⭐️)

ElasticSearch 专题学习

项目源码(源码已更新 欢迎star⭐️)

往期并发编程内容推荐

推荐 SpringBoot & SpringCloud (源码已更新 欢迎star⭐️)

博客(阅读体验较佳)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkbkca
系列文章
更多 icon
同类精品
更多 icon
继续加载