linux基本指令 Part.II

这篇博客详细介绍了Linux系统中的一些基本指令,包括rm、man手册、copy、mv(剪切和重命名)、cat、more和less、head和tail、时间相关指令如date、find及其相关选项、grep、zip和unzip等。内容涵盖文件操作、信息查看、文本处理及系统管理等多个方面。
摘要由CSDN通过智能技术生成


在这里插入图片描述

1 rm

-f 即使文件属性为只读(即写保护),亦直接删除
-i 删除前逐一询问确认
-r 删除目录及其下所有文件

rm慎用,没有回收站!

2 man 手册 manual

-k 根据关键字搜索联机帮助
num 只在第num章节找
-a 将所有章节的都显示出来,比如 man printf 它缺省从第一章开始搜索,知道就停止,用a选项,当按下q退出,他会继续往后面搜索,直到所有章节都搜索完毕
  • 安装
yum install -y man-pages
man fork

Linux的命令有很多参数,我们不可能全记住,我们可以通过查看联机手册获取帮助。

[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# man ls
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# man fork
No manual entry for fork
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# man ls
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# man printf
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# printf "%d:%s\n" 10 "hello world"
10:hello world
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# man 3 printf
No manual entry for printf in section 3
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# man man
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# man 1 printf
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# man fork
No manual entry for fork

3 copy

  • 拷贝文件下级目录 cp
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# tree .
.
├── dir
├── file.txt
├── mydir
└── myfile.c

2 directories, 2 files
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cp file.txt dir
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# tree .
.
├── dir
│   └── file.txt
├── file.txt
├── mydir
└── myfile.c

2 directories, 3 files

  • 拷贝目录上级目录 cp -r
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# tree ..
..
├── lesson2
│   ├── a.out
│   ├── d1
│   │   └── d2
│   ├── dir
│   │   └── dir
│   ├── mycode
│   ├── myfile.c
│   └── test.c
└── lesson3
    ├── dir
    │   └── file.txt
    ├── file.txt
    ├── mydir
    └── myfile.c

9 directories, 6 files
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cp -r dir ..
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# tree ..
..
├── dir
│   └── file.txt
├── lesson2
│   ├── a.out
│   ├── d1
│   │   └── d2
│   ├── dir
│   │   └── dir
│   ├── mycode
│   ├── myfile.c
│   └── test.c
└── lesson3
    ├── dir
    │   └── file.txt
    ├── file.txt
    ├── mydir
    └── myfile.c

10 directories, 7 files

  • 删除,选择放前面
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# rm -rf ../dir

4 mv

-f--force 强行复制文件或目录, 不论目的文件或目录是否已经存在
-i--interactive 覆盖文件之前先询问用户
-r 递归处理,将指定目录下的文件与子目录一并处理。若源文件或目录的形态,不属于目录或符号链
接,则一律视为普通文件处理
-R 或 --recursive递归处理,将指定目录下的文件及子目录一并处理

4.1剪切 cut

  • mv文件
[root@iZ2vc35hhygf9q48p23vbnZ dir]# mv file.txt ..
mv: overwrite ‘../file.txt’? y
[root@iZ2vc35hhygf9q48p23vbnZ dir]# ls
[root@iZ2vc35hhygf9q48p23vbnZ dir]# tree ..
..
├── dir
├── file.txt
├── mydir
└── myfile.c

2 directories, 2 files
[root@iZ2vc35hhygf9q48p23vbnZ dir]# mv ../file.txt .
[root@iZ2vc35hhygf9q48p23vbnZ dir]# tree ..
..
├── dir
│   └── file.txt
├── mydir
└── myfile.c

2 directories, 2 files

  • mv目录
[root@iZ2vc35hhygf9q48p23vbnZ dir]# mv dir2 ..
[root@iZ2vc35hhygf9q48p23vbnZ dir]# tree ..
..
├── dir
│   └── file.txt
├── dir2
├── mydir
└── myfile.c

3 directories, 2 files
[root@iZ2vc35hhygf9q48p23vbnZ dir]# mv ../dir2 .
[root@iZ2vc35hhygf9q48p23vbnZ dir]# tree ..
..
├── dir
│   ├── dir2
│   └── file.txt
├── mydir
└── myfile.c

3 directories, 2 files

  • mv src dst, dst一定是目录

4.2 重命名 rename

[root@iZ2vc35hhygf9q48p23vbnZ dir]# ls
dir2  file.txt
[root@iZ2vc35hhygf9q48p23vbnZ dir]# mv file.txt name.txt
[root@iZ2vc35hhygf9q48p23vbnZ dir]# ls
dir2  name.txt

[root@iZ2vc35hhygf9q48p23vbnZ dir]# ls
dir2  name.txt
[root@iZ2vc35hhygf9q48p23vbnZ dir]# mv name.txt ../newname.txt
[root@iZ2vc35hhygf9q48p23vbnZ dir]# ls
dir2
[root@iZ2vc35hhygf9q48p23vbnZ dir]# ls ..
dir  mydir  myfile.c  newname.txt

5 cat (concatenate 连接)

-b 对非空输出行编号
-n 对输出的所有行编号
-s 不输出多行空行
  • cat 打印
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# nano newname.txt 
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cat newname.txt
aaaaaaaaaaaa
bbbbbbbbbbbb
cccccccccccc
dddddddddddd
  • echo打印到显示器和与>输出重定向配合输出到显示屏
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# echo "hello world"
hello world
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# echo "hello" > mylog.txt
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# ls
dir  mydir  myfile.c  mylog.txt  newname.txt
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# nano mylog.txt 
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cat mylog.txt
hello

注意:echo "hello world" 可以理解为向显示器输入,显示器可以看作一种"文件".

  • Linux下一切皆文件
    在这里插入图片描述
  • echo > 覆盖写入
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cat mylog.txt 
1
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# echo "111" > mylog.txt 
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cat mylog.txt 
111

  • echo >> 追加重定向
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cat mylog.txt 
222
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# echo "333" >> mylog.txt
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cat mylog.txt
222
333
  • cat <打印
  • cat 打印
  • cat >覆盖输出
  • cat >>追加输出

6 more 和 less

cnt=0; while [ $cnt -le 1000 ]; do echo "hello $cnt"; let cnt++; done > mylog.txt

moreless都是显示,但more不如less好,无法上翻./查找,n下一个,q退出.

7 head 和 tail

分别打印前面10行后面10行

[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# head mylog.txt 
1
2
3
4
5
6
7
8
9

[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# tail mylog.txt 
9

10
11
12
13
14
15
16
17
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# wc -l mylog.txt 
18 mylog.txt
  • 命令行管道
  • 计算机最重要的资源是数据,管道|可以连接两条命令
head -520 mylog.txt > tmp.txt
tail -20 tmp.txt 

拿500-520行数据,等价于

cat mylog.txt | head -520 | tail -20

8 时间相关指令

8.1 date

  • date
date
Thu Nov 17 19:37:53 CST 2022
  • 年月日时分秒
date +%Y-%m-%d/%H:%M%S
2022-11-17/19:4148
  • 时间戳
    在这里插入图片描述
    -显示3个月日历(calendar)
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cal -3
    October 2022          November 2022         December 2022   
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
                   1         1  2  3  4  5               1  2  3
 2  3  4  5  6  7  8   6  7  8  9 10 11 12   4  5  6  7  8  9 10
 9 10 11 12 13 14 15  13 14 15 16 17 18 19  11 12 13 14 15 16 17
16 17 18 19 20 21 22  20 21 22 23 24 25 26  18 19 20 21 22 23 24
23 24 25 26 27 28 29  27 28 29 30           25 26 27 28 29 30 31
30 31   

9 补充

  • seed
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cat test.c 
#include <stdio.h>

int main()
{
  printf("time:%u\n", (unsigned int)time(NULL));
  return 0;
}
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# gcc test.c 
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# ls
a.out  dir  mydir  myfile.c  mylog.txt  newname.txt  test.c  tmp.txt
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# ./a.out 
time:1668686902
  • sortsort -r升降排序(按照每行ASCII码)
  • | uqiq去重

10 find (很重要,选项也多)

10.1 find

  • 搜索文件
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# find /root -name a.out
/root/lesson3/a.out
/root/lesson2/a.out

10.2 which

  • 搜索指令在哪(一般在/usr/bin)
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# which pwd
/usr/bin/pwd
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# which man
/usr/bin/man

ls /usr/bin | head -10

10.3 whereis

[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=ceaf496f3aec08afced234f4f36330d3d13a657b, stripped

-注意executable,指令本质是可执行程序.

10.4 指令起别名alias

[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# alias myls='ll -a -i -n'
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# myls
total 56
1055429 drwxr-xr-x  4 0 0 4096 Nov 17 20:08 .
 917505 dr-xr-x---. 7 0 0 4096 Nov 16 22:06 ..
1055502 -rwxr-xr-x  1 0 0 8496 Nov 17 20:08 a.out
1055496 drwxr-xr-x  3 0 0 4096 Nov 16 22:24 dir
1055450 drwxr-xr-x  2 0 0 4096 Nov 16 00:53 mydir
1055448 -rw-r--r--  1 0 0    0 Nov 16 00:44 myfile.c
1055090 -rw-r--r--  1 0 0 9901 Nov 17 19:20 mylog.txt
1055497 -rw-r--r--  1 0 0   52 Nov 17 18:02 newname.txt
1055097 -rw-r--r--  1 0 0   96 Nov 17 20:08 test.c
1055094 -rw-r--r--  1 0 0 5090 Nov 17 19:26 tmp.txt
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# which myls
alias myls='ll -a -i -n'

11 grep

[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cat > my.txt
aaaa
bbbb
cccc
dddd
AAAA
BBBB
CCCC
DDDD^C
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# grep 'bb' my.txt 
bbbb
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# grep -ivn 'b' my.txt 
1:aaaa
3:cccc
4:dddd
5:AAAA
7:CCCC

-ivn忽略大小写,反向,行数

12 zip/unzip

zip - package and compress (archive) files

  • 任务管理器
    top top - display Linux processes
    yum install -y zip
    在这里插入图片描述
[root@iZ2vc35hhygf9q48p23vbnZ ~]# zip -r lesson3.zip lesson3
  adding: lesson3/ (stored 0%)
  adding: lesson3/myfile.c (stored 0%)
  adding: lesson3/my.txt (deflated 20%)
  adding: lesson3/mydir/ (stored 0%)
  adding: lesson3/a.out (deflated 72%)
  adding: lesson3/dir/ (stored 0%)
  adding: lesson3/dir/dir2/ (stored 0%)
  adding: lesson3/test.c (deflated 4%)
  adding: lesson3/mylog.txt (deflated 80%)
  adding: lesson3/tmp.txt (deflated 80%)
  adding: lesson3/newname.txt (deflated 67%)
[root@iZ2vc35hhygf9q48p23vbnZ ~]# mv lesson3.zip lesson3
[root@iZ2vc35hhygf9q48p23vbnZ ~]# ls
lesson2  lesson3
[root@iZ2vc35hhygf9q48p23vbnZ ~]# cd lesson3
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# ls
a.out  dir  lesson3.zip  mydir  myfile.c  mylog.txt  my.txt  newname.txt  test.c  tmp.txt

yum install -y unzip

[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# unzip lesson3.zip 
Archive:  lesson3.zip
   creating: lesson3/
 extracting: lesson3/myfile.c        
  inflating: lesson3/my.txt          
   creating: lesson3/mydir/
  inflating: lesson3/a.out           
   creating: lesson3/dir/
   creating: lesson3/dir/dir2/
  inflating: lesson3/test.c          
  inflating: lesson3/mylog.txt       
  inflating: lesson3/tmp.txt         
  inflating: lesson3/newname.txt     
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# ls
a.out  lesson3      mydir     mylog.txt  newname.txt  tmp.txt
dir    lesson3.zip  myfile.c  my.txt     test.c
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# cd lesson3
[root@iZ2vc35hhygf9q48p23vbnZ lesson3]# ls
a.out  dir  mydir  myfile.c  mylog.txt  my.txt  newname.txt  test.c  tmp.txt

总结

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值