linux:linux常用命令
差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
| linux:linux常用命令 [2026/10/13 22:06] – 外部编辑 127.0.0.1 | linux:linux常用命令 [2026/11/05 16:31] (当前版本) – [13. sort] user01 | ||
|---|---|---|---|
| 行 2: | 行 2: | ||
| ## - 删除N天前最老的目录或文件 | ## - 删除N天前最老的目录或文件 | ||
| 删除3天前最老的目录或文件,find时仅限搜索一层。 -- 感觉这玩意有时候不灵,不太好用。 | 删除3天前最老的目录或文件,find时仅限搜索一层。 -- 感觉这玩意有时候不灵,不太好用。 | ||
| - | < | + | |
| + | ```bash | ||
| + | find /your_dir/ -maxdepth 1 -mtime +3 | head -1 | xargs rm -rf | ||
| + | ``` | ||
| ## - 解压 | ## - 解压 | ||
| - | < | + | |
| + | ``` | ||
| Main operation mode: | Main operation mode: | ||
| -c, --create | -c, --create | ||
| 行 28: | 行 33: | ||
| -C, --directory=DIR | -C, --directory=DIR | ||
| change to directory DIR | change to directory DIR | ||
| - | </ | + | ``` |
| - | | + | |
| - | * 解压tar.gz到指定目录 | + | |
| - | * 解压bz2到指定目录 | + | ``` |
| + | tar -xvf xxx.tar.gz -C /tmp/ | ||
| + | ``` | ||
| + | |||
| + | * 解压tar.gz到指定目录 | ||
| + | ``` | ||
| + | tar -zxvf xxx.tar.gz -C /tmp/ | ||
| + | ``` | ||
| + | |||
| + | * 解压bz2到指定目录 | ||
| + | ``` | ||
| + | tar -jxvf xxx.tar.bz2 -C /tmp/ | ||
| + | ``` | ||
| 行 41: | 行 59: | ||
| - | < | + | |
| + | ``` | ||
| cksum [FILE]... | cksum [FILE]... | ||
| - | </ | + | ``` |
| 一般网上下载软件的时候都需要check crc值,来确保下载数据有没有问题,有时候即使文件大小,crc也是check不过的,下载下来的数据包有问题。 | 一般网上下载软件的时候都需要check crc值,来确保下载数据有没有问题,有时候即使文件大小,crc也是check不过的,下载下来的数据包有问题。 | ||
| 行 51: | 行 71: | ||
| ## - mount | ## - mount | ||
| ### - NFS | ### - NFS | ||
| - | < | + | |
| + | ``` | ||
| + | mount -t nfs 192.168.2.22:/ | ||
| + | ``` | ||
| ### - cdrom | ### - cdrom | ||
| - | < | + | |
| + | ``` | ||
| mount -t iso9660 /dev/scd0 /media/dvd1 | mount -t iso9660 /dev/scd0 /media/dvd1 | ||
| mount -t iso9660 /dev/scd1 /media/dvd2 | mount -t iso9660 /dev/scd1 /media/dvd2 | ||
| - | </ | + | ``` |
| 行 68: | 行 94: | ||
| **bash** | **bash** | ||
| - | < | + | |
| + | ```bash | ||
| alias cd=' | alias cd=' | ||
| - | </ | + | ``` |
| **csh/ | **csh/ | ||
| - | < | + | |
| + | ```bash | ||
| alias cd 'cd \!*; ls' | alias cd 'cd \!*; ls' | ||
| 行 90: | 行 119: | ||
| !:x* Like x-$( $ is the last argument) above. | !:x* Like x-$( $ is the last argument) above. | ||
| !:x- Like x* except that word$ is omitted. | !:x- Like x* except that word$ is omitted. | ||
| + | ``` | ||
| - | </ | ||
| ## - diff | ## - diff | ||
| - | < | + | |
| + | ```bash | ||
| diff -r -q dir1 dir2 | grep differ | awk ' | diff -r -q dir1 dir2 | grep differ | awk ' | ||
| alias tt 'diff -r -q \!* | grep differ | awk ' "'" | alias tt 'diff -r -q \!* | grep differ | awk ' "'" | ||
| - | </ | + | ``` |
| ## - 管道 | ## - 管道 | ||
| **格式** | **格式** | ||
| - | < | + | |
| + | ``` | ||
| command1 | command2 | command1 | command2 | ||
| command1 | command2 [ | commandN... ] | command1 | command2 [ | commandN... ] | ||
| - | </ | + | ``` |
| 当在两个命令之间设置管道时,管道符|左边命令的输出就变成了右边命令的输入。只要第一个命令向`标准输出`写入,而第二个命令是从`标准输入`读取,那么这两个命令就可以形成一个管道。 | 当在两个命令之间设置管道时,管道符|左边命令的输出就变成了右边命令的输入。只要第一个命令向`标准输出`写入,而第二个命令是从`标准输入`读取,那么这两个命令就可以形成一个管道。 | ||
| 行 113: | 行 146: | ||
| ## - bshell prompt | ## - bshell prompt | ||
| - | < | + | |
| + | ```bash | ||
| Sequence Value Displayed | Sequence Value Displayed | ||
| \a ASCII bell. This makes the computer beep when it is encountered. | \a ASCII bell. This makes the computer beep when it is encountered. | ||
| 行 145: | 行 179: | ||
| cursor or changing text colors. | cursor or changing text colors. | ||
| \] Signals the end of a non-printing character sequence. | \] Signals the end of a non-printing character sequence. | ||
| + | ``` | ||
| - | </ | ||
| ## - cshell prompt | ## - cshell prompt | ||
| - | < | + | |
| + | ```bash | ||
| prompt | prompt | ||
| The string which is printed before reading each command from the terminal. prompt may include any of the following formatting sequences (+), which are replaced by the given informa- tion: | The string which is printed before reading each command from the terminal. prompt may include any of the following formatting sequences (+), which are replaced by the given informa- tion: | ||
| 行 228: | 行 263: | ||
| Set by default to ‘%# ’ in interactive shells. | Set by default to ‘%# ’ in interactive shells. | ||
| - | </ | + | ``` |
| 行 234: | 行 270: | ||
| 参考文件:http:// | 参考文件:http:// | ||
| - | 使用csh时,可以在.cshrc文件中添加如下设置: set prompt = “< | + | 使用csh时,可以在.cshrc文件中添加如下设置: set prompt = “<span style=" |
| 其中红色部分是设置title名字为%C2,绿色是命令行提示符为%/ | 其中红色部分是设置title名字为%C2,绿色是命令行提示符为%/ | ||
| ## - cshrc | ## - cshrc | ||
| - | < | + | |
| + | ```bash | ||
| set title = " | set title = " | ||
| 行 258: | 行 295: | ||
| alias | alias | ||
| alias | alias | ||
| + | ``` | ||
| - | </ | ||
| ## - 不同机器之间手动同步时间 | ## - 不同机器之间手动同步时间 | ||
| - | < | + | |
| + | ```bash | ||
| / | / | ||
| - | </ | + | ``` |
| ## - sort | ## - sort | ||
| - | < | + | |
| + | ```bash [enable_line_numbers=" | ||
| sort | sort | ||
| 行 291: | 行 331: | ||
| + ===> < | + ===> < | ||
| -o ===> < | -o ===> < | ||
| - | ———————————————— | + | ``` |
| - | 版权声明:本文为CSDN博主「IT蓝月」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 | + | |
| - | 原文链接:https:// | + | 用 **`sort -V`(版本排序 / 自然排序)**,专门处理 `row0 row1 row10 row2` 这种字母 + 数字混合,让里面数字按数值走: |
| - | </ | + | |
| + | ``` | ||
| + | # data.txt | ||
| + | row0 | ||
| + | row1 | ||
| + | row10 | ||
| + | row2 | ||
| + | ``` | ||
| + | |||
| + | ``` | ||
| + | sort -V data.txt | ||
| + | ``` | ||
| + | |||
| + | 输出: | ||
| + | |||
| + | ``` | ||
| + | row0 | ||
| + | row1 | ||
| + | row2 | ||
| + | row10 | ||
| + | ``` | ||
| + | |||
| + | 降序: | ||
| + | |||
| + | ``` | ||
| + | sort -Vr data.txt | ||
| + | ``` | ||
| + | |||
| + | 区别: | ||
| + | |||
| + | - `sort -n`:是把**整行当成数字**,你的行是 `rowxx` 带前缀字母,`-n` 无效 | ||
| + | - `sort` 默认:ASCII 字典序 → `row0, | ||
| + | - `sort -V`:自然排序,字符串分段比较,遇到数字段自动按数值比较,完美匹配你这个场景 | ||
| 行 302: | 行 375: | ||
| tail - output the last part of files | tail - output the last part of files | ||
| - | < | + | |
| + | ``` | ||
| tail -n 20 file; # 输出文件的最后20行 | tail -n 20 file; # 输出文件的最后20行 | ||
| tail -n +20 file; # 从文件的第20行开始输出 | tail -n +20 file; # 从文件的第20行开始输出 | ||
| + | ``` | ||
| - | </ | ||
| ## - head | ## - head | ||
| head - output the first part of files | head - output the first part of files | ||
| - | < | + | |
| + | ``` | ||
| head -n 20 file; # 输出文件的前20行内容 | head -n 20 file; # 输出文件的前20行内容 | ||
| - | </ | + | ``` |
| 行 318: | 行 394: | ||
| ### - 基本用法 | ### - 基本用法 | ||
| sed 常用命令 | sed 常用命令 | ||
| - | < | + | |
| + | ```bash | ||
| sed -i ' | sed -i ' | ||
| sed -n ' | sed -n ' | ||
| 行 324: | 行 401: | ||
| sed -n ' | sed -n ' | ||
| sed -n ' | sed -n ' | ||
| - | </ | + | ``` |
| + | |||
| - | < | + | ```bash |
| sed ' | sed ' | ||
| 行 339: | 行 418: | ||
| # -i, 将替换后的结果写回原文件中 | # -i, 将替换后的结果写回原文件中 | ||
| sed -i ' | sed -i ' | ||
| - | </ | + | ``` |
| 行 345: | 行 425: | ||
| 对某个字符串进行替换 | 对某个字符串进行替换 | ||
| - | < | + | |
| + | ```bash | ||
| [me@linuxbox ~]$ echo " | [me@linuxbox ~]$ echo " | ||
| back | back | ||
| - | </ | + | ``` |
| 行 358: | 行 440: | ||
| * P -- 打印多行中的一行 | * P -- 打印多行中的一行 | ||
| - | < | + | |
| + | ```bash | ||
| sed '/ | sed '/ | ||
| - | </ | + | ``` |
| ## - basename | ## - basename | ||
| - | < | + | |
| + | ```tcsh | ||
| # csh | # csh | ||
| set a = '/ | set a = '/ | ||
| 行 369: | 行 454: | ||
| echo $name | echo $name | ||
| > xxx | > xxx | ||
| - | </ | + | ``` |
| ## - cut | ## - cut | ||
| cut是一个简单功能的字段截取工具,推荐使用更强功能的awk [awk语法速查](语法: | cut是一个简单功能的字段截取工具,推荐使用更强功能的awk [awk语法速查](语法: | ||
| - | < | + | |
| + | ``` | ||
| cut OPTION... [FILE]... | cut OPTION... [FILE]... | ||
| -c:仅显示行中指定范围的字符; | -c:仅显示行中指定范围的字符; | ||
| -d:指定字段的分隔符,默认的字段分隔符为“TAB”; | -d:指定字段的分隔符,默认的字段分隔符为“TAB”; | ||
| -f:显示指定字段的内容; | -f:显示指定字段的内容; | ||
| + | ``` | ||
| + | |||
| - | </ | + | ``` |
| - | < | + | |
| # cat test.txt | # cat test.txt | ||
| No Name Mark Percent | No Name Mark Percent | ||
| 行 422: | 行 510: | ||
| jack | jack | ||
| alex | alex | ||
| + | ``` | ||
| - | </ | ||
| ## - wget批量下载文件 | ## - wget批量下载文件 | ||
| 行 449: | 行 537: | ||
| ## - iconv 文件编码转换 | ## - iconv 文件编码转换 | ||
| - | < | + | |
| + | ``` | ||
| iconv - convert text from one character encoding to another | iconv - convert text from one character encoding to another | ||
| SYNOPSIS | SYNOPSIS | ||
| iconv [options] [-f from-encoding] [-t to-encoding] | iconv [options] [-f from-encoding] [-t to-encoding] | ||
| | | ||
| + | ``` | ||
| - | </ | ||
linux/linux常用命令.1791900372.txt.gz · 最后更改: 由 127.0.0.1
