linux常用命令(持续更新)

本文介绍了Linux命令行中grep(文本搜索)、find(文件查找)、sed(文本编辑)、wc(字数统计)和tar(文件打包/解压)等工具的使用方法和选项,帮助读者掌握基本的系统管理技巧。
摘要由CSDN通过智能技术生成

1. grep

grep <find_content> <target_file>

grep -i <find_content> <target_file>                        # 不区分大小写
grep -w <find_content> <target_file>                        # 精确匹配
grep -e <find_content1> -e <find_content2> <target_file>    # 联合查找
grep -n <find_content> <target_file>                        # 显示查找到的内容行号
grep -v <find_content> <target_file>                        # 反向查找(不包含find_content)
grep -n <find_content> ./                                   # 当前路径下递归查找
grep -lr <find_content> ./                                  # 当前路径下递归查找所在文件的文件名
grep -E <regex> ./                                          # 用正则表达式查找

2. find

find <target_dir> -name "find_content"

find <target_dir> -type f    # 按类型查找:查找文件
find <target_dir> -type d   # 按类型查找:查找路径
find <target_dir> -type l   # 按类型查找:查找符号链接文件

find <target_dir> -a   # 按时间查找:访问时间(access)
find <target_dir> -c   # 按时间查找:文件权限变更时间(change)
find <target_dir> -m   # 按时间查找:修改时间(modify)
eg:
    find ./ -mmin  -1   # 1分钟内修改过的文件
    find ./ -mtime -1   # 1天内修改过的文件

find <target_dir> -user <user_name>     # 按用户名查找
find <target_dir> -group <group_name>   # 按用户组查找

3. sed

sed # stream-editor

sed -e '' <target_file>             # 不改变target_file原文件,会显示出缓冲区的内容
sed -n '' <target_file>             # 不改变target_file原文件,不显示出缓冲区的内容,只显示修改还的内容
sed -i '' <target_file>             # 修改target_file原文件
sed -ie '' <target_file>            # 修改target_file原文件, 并将源文件备份为target_filee
sed -f <shell_file> <target_file>   # 用脚本shell_file配合sed修改target_file
选项多次使用:sed -e '' -e '' -e '' <target_file>

6个脚本参数
  i insert    # 指定行前插入
    eg:
        sed -i '1ithe \inserted line' test.md   # 文件test.md的第1行前插入"the inserted line"

  a append    # 指定行后追加
    eg:
        sed -i '1athe \appened line' test.md    # 文件test.md的第1行后追加"the appened line"

  d delete    # 删除
    eg:
        sed -i '1d' test.md     # 删除文件test.md的第1行

  c copy  # 指定行覆盖
    eg:
        sed -i '1coverrider' test.md     # 文件test.md的第1行内容替换为"overrider"

  s substitude    # 替换指定的内容
    eg:
        sed -e '1s/old/new/' test.md    # 局部替换:用"new"替换文件test.md第1行中的第一个"old"
        sed -e '1s/old/new/g' test.md   # 全局替换:用"new"替换文件test.md第1行中的所有"old"

  p print         # 打印
    eg:
        sed -n '1p' test.md    # 将test.md第1行中的内容打印出来

4. wc 

wc # word count

wc <file.md>
2 3 15 file.md  # 2:  文件file.md有2 lines
                # 3:  文件file.md有3 words
                # 15: 文件file.md有15 Bytes
                # file.md: 文件名

wc -l <file.md> # line
wc -w <file.md> # word
wc -c <file.md> # Bytes
wc -m <file.md> # 字符数

5. tar

tar -cf <file.tar> <src_file>   # 将src_file压缩成file.tar
tar -xf <file.tar>              # 将file.tar解压

tar -zcf <file.tar.gz> <src_file>   # 用gz算法压缩
tar -zxf <file.tar.gz>              # 用gz算法解压

tar -xzf <file.tar.gz> -C ./tmp     # 用gz算法解压到指定路径./tmp下

tar -v # 解压detail显示
注意: -f参数指定文件的,需要放在其他参数的最后

6. tail & head 

TODO

Reference

My 博客园主页  今天学点啥? - 博客园 (cnblogs.com)

感谢Bilibili UP主 Ternurafl Ternurafl的个人空间-Ternurafl个人主页-哔哩哔哩视频

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值