【Linux】基础指令(2)

man

linux中有很多指令,我们不可能全部记住,man是linux/unix系统中的手册页指令,当我们遇到不熟悉的命令可以用man来查看命令,函数,配置文件的详细使用说明。

man手册分为多个章节,详情如下:

cp

功能:复制文件或目录

cp普通文件

bash 复制代码
root@hcss-ecs-887f:~/lession1# echo "hello world!">file1.txt
root@hcss-ecs-887f:~/lession1# cat file1.txt
hello world!
root@hcss-ecs-887f:~/lession1# ll'
> ^C
root@hcss-ecs-887f:~/lession1# ll
total 12
drwxr-xr-x  2 root root 4096 Apr 29 20:11 ./
drwx------ 11 root root 4096 Apr 29 20:10 ../
-rw-r--r--  1 root root   13 Apr 29 20:11 file1.txt
root@hcss-ecs-887f:~/lession1# pwd
/root/lession1
root@hcss-ecs-887f:~/lession1# tree
.
└── file1.txt

0 directories, 1 file
root@hcss-ecs-887f:~/lession1# cp file1.txt filecopy1.txt
root@hcss-ecs-887f:~/lession1# ll
total 16
drwxr-xr-x  2 root root 4096 Apr 29 20:14 ./
drwx------ 11 root root 4096 Apr 29 20:10 ../
-rw-r--r--  1 root root   13 Apr 29 20:11 file1.txt
-rw-r--r--  1 root root   13 Apr 29 20:14 filecopy1.txt
root@hcss-ecs-887f:~/lession1# cat filecopy1.txt
hello world!
root@hcss-ecs-887f:~/lession1# 

将多个文件拷贝到指定路径下

bash 复制代码
root@hcss-ecs-887f:~/lession1# ll
total 16
drwxr-xr-x  2 root root 4096 Apr 29 20:14 ./
drwx------ 11 root root 4096 Apr 29 20:10 ../
-rw-r--r--  1 root root   13 Apr 29 20:11 file1.txt
-rw-r--r--  1 root root   13 Apr 29 20:14 filecopy1.txt
root@hcss-ecs-887f:~/lession1# mkdir d1
root@hcss-ecs-887f:~/lession1# ll
total 20
drwxr-xr-x  3 root root 4096 Apr 29 20:20 ./
drwx------ 11 root root 4096 Apr 29 20:10 ../
drwxr-xr-x  2 root root 4096 Apr 29 20:20 d1/
-rw-r--r--  1 root root   13 Apr 29 20:11 file1.txt
-rw-r--r--  1 root root   13 Apr 29 20:14 filecopy1.txt
root@hcss-ecs-887f:~/lession1# cp *.txt d1  
root@hcss-ecs-887f:~/lession1# ll
total 20
drwxr-xr-x  3 root root 4096 Apr 29 20:20 ./
drwx------ 11 root root 4096 Apr 29 20:10 ../
drwxr-xr-x  2 root root 4096 Apr 29 20:21 d1/
-rw-r--r--  1 root root   13 Apr 29 20:11 file1.txt
-rw-r--r--  1 root root   13 Apr 29 20:14 filecopy1.txt
root@hcss-ecs-887f:~/lession1# tree d1
d1
├── file1.txt
└── filecopy1.txt

0 directories, 2 files
root@hcss-ecs-887f:~/lession1# 

cp如果目标文件存在,会覆盖目标文件的内容

bash 复制代码
root@hcss-ecs-887f:~/lession1# ll
total 20
drwxr-xr-x  3 root root 4096 Apr 29 20:20 ./
drwx------ 11 root root 4096 Apr 29 20:10 ../
drwxr-xr-x  2 root root 4096 Apr 29 20:21 d1/
-rw-r--r--  1 root root   13 Apr 29 20:11 file1.txt
-rw-r--r--  1 root root   13 Apr 29 20:14 filecopy1.txt
root@hcss-ecs-887f:~/lession1# echo "who are you?">file1.txt
root@hcss-ecs-887f:~/lession1# cat file1.txt
who are you?
root@hcss-ecs-887f:~/lession1# cp file1.txt filecopy1.txt
root@hcss-ecs-887f:~/lession1# cat filecopy1.txt
who are you?

拷贝前询问

用于在复制文件时提示用户确认,防止意外覆盖已有文件。当目标位置存在同名文件时,会提示是否覆盖

根据回应y还是n决定是否覆盖。

递归强制拷贝整个目录

复制代码
root@hcss-ecs-887f:~/lession1# pwd
/root/lession1
root@hcss-ecs-887f:~/lession1# tree
.
├── d1
│?? ├── file1.txt
│?? └── filecopy1.txt
├── file1.txt
└── filecopy1.txt

1 directory, 4 files
root@hcss-ecs-887f:~/lession1# cd ..
root@hcss-ecs-887f:~# cp -rf lession1 lession2
root@hcss-ecs-887f:~# cd lession2
root@hcss-ecs-887f:~/lession2# tree
.
├── d1
│?? ├── file1.txt
│?? └── filecopy1.txt
├── file1.txt
└── filecopy1.txt

1 directory, 4 files
root@hcss-ecs-887f:~/lession2# 

mv

mv是move的缩写,用来移动或重命名,经常用来备份文件或目录。

移动文件或目录

bash 复制代码
root@hcss-ecs-887f:~/lession1# mkdir txt1
root@hcss-ecs-887f:~/lession1# cd
root@hcss-ecs-887f:~# cd lession1
root@hcss-ecs-887f:~/lession1# tree
.
├── d1
│   ├── file1.txt
│   └── filecopy1.txt
├── file1.txt
├── filecopy1.txt
└── txt1

2 directories, 4 files
root@hcss-ecs-887f:~/lession1# mv file1.txt txt1
root@hcss-ecs-887f:~/lession1# cd txt1
root@hcss-ecs-887f:~/lession1/txt1# tree
.
└── file1.txt

0 directories, 1 file
root@hcss-ecs-887f:~/lession1/txt1# 

重命名文件或目录

如果目标路径与要操作的源文件在同一目录下,mv会执行重命名操作

cpp 复制代码
root@hcss-ecs-887f:~# cd lession1
root@hcss-ecs-887f:~/lession1# tree
.
├── d1
│   ├── file1.txt
│   └── filecopy1.txt
├── filecopy1.txt
└── txt1
    └── file1.txt

2 directories, 4 files
root@hcss-ecs-887f:~/lession1# mv txt1 txt2
root@hcss-ecs-887f:~/lession1# tree
.
├── d1
│   ├── file1.txt
│   └── filecopy1.txt
├── filecopy1.txt
└── txt2
    └── file1.txt

2 directories, 4 files
root@hcss-ecs-887f:~/lession1# 

常用选项

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

-i:若目标文件存在会询问是否覆盖。

cat

cat是一个非常常用的指令,用来查看文件内容,合并文件和创建文件

查看文件内容

cpp 复制代码
root@hcss-ecs-887f:~/lession1# cnt=0; while [ $cnt -le 20 ]; do echo "hell0 world!";
let cnt++; done > temp.txt
root@hcss-ecs-887f:~/lession1# cat temp.txt
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
hell0 world!
root@hcss-ecs-887f:~/lession1# 

带行号输出:

cpp 复制代码
root@hcss-ecs-887f:~/lession1# cat -b temp.txt
     1	hell0 world!
     2	hell0 world!
     3	hell0 world!
     4	hell0 world!
     5	hell0 world!
     6	hell0 world!
     7	hell0 world!
     8	hell0 world!
     9	hell0 world!
    10	hell0 world!
    11	hell0 world!
    12	hell0 world!
    13	hell0 world!
    14	hell0 world!
    15	hell0 world!
    16	hell0 world!
    17	hell0 world!
    18	hell0 world!
    19	hell0 world!
    20	hell0 world!
    21	hell0 world!
root@hcss-ecs-887f:~/lession1# 

more

和cat功能类似,用于逐页显示文件内容(适合查看大文件)。

bash 复制代码
root@hcss-ecs-887f:~/lession1# cnt=0; while [ $cnt -le 2000 ]; do echo "hello
world"; let cnt++; done > temp.txt
root@hcss-ecs-887f:~/lession1# more -10 temp.txt
hello
world
hello
world
hello
world
hello
world
hello
world
--More--(0%)

less

less ⼯具也是对⽂件或其它输出进⾏分⻚显⽰的⼯具,应该说是linux正统查看⽂件内容的⼯具,
功能极其强⼤
less 的⽤法⽐起 more 更加的有弹性,在 more 的时候,我们并没有办法向前⾯翻, 只能往后⾯

但若使⽤了 less 时,就可以使⽤ [pageup] [pagedown] 等按键的功能来往前往后翻看⽂件,更
容易⽤来查看⼀个⽂件的内容
除此之外,在 less ⾥头可以拥有更多的搜索功能,不⽌可以向下搜,也可以向上搜。
选项:
-i 忽略搜索时的⼤⼩写
-N 显⽰每⾏的⾏号
/字符串:向下搜索"字符串"的功能
?字符串:向上搜索"字符串"的功能
n:重复前⼀个搜索(与 / 或 ? 有关)
N:反向重复前⼀个搜索(与 / 或 ? 有关)
q:quit
基础功能:

bash 复制代码
root@hcss-ecs-887f:~/lession1# cnt=0; while [ $cnt -le 2000 ]; do echo "hello
$cnt"; let cnt++; done > temp.txt
root@hcss-ecs-887f:~/lession1# less -N temp.txt

1 hello
      2 0
      3 hello
      4 1
      5 hello
      6 2
      7 hello
      8 3
      9 hello
     10 4
     11 hello
     12 5
     13 hello
     14 6
     15 hello
     16 7
     17 hello
     18 8
     19 hello
     20 9
     21 hello
     22 10
     23 hello
     24 11
     25 hello
     26 12
     27 hello
     28 13
     29 hello
     30 14
     31 hello
     32 15
......

head指令

功能:

打印文件开头10行

cpp 复制代码
root@hcss-ecs-887f:~/lession1# head temp.txt
hello
0
hello
1
hello
2
hello
3
hello
4
root@hcss-ecs-887f:~/lession1# 


#可以手动指定打印几行

root@hcss-ecs-887f:~/lession1# head -5 temp.txt
hello
0
hello
1
hello
root@hcss-ecs-887f:~/lession1# 

tail

tail 命令从指定点开始将⽂件写到标准输出.使⽤tail命令的-f选项可以⽅便的查阅正在改变的⽇志⽂
件,tail -f filename会把filename⾥最尾部的内容显⽰在屏幕上,并且不断刷新,使你看到最新的⽂件内容.

bash 复制代码
root@hcss-ecs-887f:~/lession1# tail temp.txt
hello
1996
hello
1997
hello
1998
hello
1999
hello
2000
root@hcss-ecs-887f:~/lession1# 

date

用于显示或设置系统时间和日期的命令,它支持多种格式输出,还可用于计算日期和时间差

显示当前日期和时间

bash 复制代码
root@hcss-ecs-887f:~/lession1# date
Fri May  2 03:08:03 PM CST 2025
root@hcss-ecs-887f:~/lession1# 

自定义格式输出

%H : ⼩时(00..23)
%M : 分钟(00..59)
%S : 秒(00..61)
%X : 相当于 %H:%M:%S
%d : ⽇ (01..31)
%m : ⽉份 (01..12)
%Y : 完整年份 (0000..9999)
%F : 相当于 %Y-%m-%d

cpp 复制代码
root@hcss-ecs-887f:~/lession1# date "+%Y-%m-%d %H:%M:%S"
2025-05-02 15:09:15
root@hcss-ecs-887f:~/lession1# 

显示相对时间

cpp 复制代码
root@hcss-ecs-887f:~/lession1# date -d "1 day"                     # 1 天后
date -d "2 hours ago"               # 2 小时前
date -d "next monday"               # 下周一
date -d "2024-05-20 + 3 days"       # 2024-05-20 的 3 天后
Sat May  3 03:12:34 PM CST 2025
Fri May  2 01:12:34 PM CST 2025
Mon May  5 12:00:00 AM CST 2025
Thu May 23 12:00:00 AM CST 2024
root@hcss-ecs-887f:~/lession1# 

时间戳

在 Linux 中,时间戳(Timestamp) 表示从 1970年1月1日 00:00:00 UTC(Unix 纪元) 到当前时间的秒数(或毫秒/微秒/纳秒)

生成时间戳

cpp 复制代码
date +%s       # 当前时间戳(秒级)
date +%s%N     # 纳秒级时间戳

将时间戳转化为可读格式

设置系统时间(需root权限)

bash 复制代码
sudo date -s "2025-05-1 11:00:00"  # 设置时间为 2025年5月1日 11:00

cal

cal命令可以⽤来显⽰公历(阳历)⽇历。公历是现在国际通⽤的历法,⼜称格列历,通称阳历。"阳历"⼜名"太阳历",系以地球绕⾏太阳⼀周为⼀年,为西⽅各国所通⽤,故⼜名"西历"。
常用选项:
-3 显⽰系统前⼀个⽉,当前⽉,下⼀个⽉的⽉历
-j 显⽰在当年中的第⼏天(⼀年⽇期按天算,从1⽉1号算起,默认显⽰当前⽉在⼀年中的天数)
-y 显⽰当前年份的⽇历

但是部分Linux系统中没有预装cal系统
可以通过以下命令安装:

bash 复制代码
sudo apt install bsdmainutils

find

Linux下find命令在⽬录结构中搜索⽂件,并执⾏指定的操作。
Linux下find命令提供了相当多的查找条件,功能很强⼤。由于find具有强⼤的功能,所以它的选
项也很多,其中⼤部分选项都值得我们花时间来了解⼀下。
即使系统中含有⽹络⽂件系统( NFS),find命令在该⽂件系统中同样有效,只你具有相应的权
限。
在运⾏⼀个⾮常消耗资源的find命令时,很多⼈都倾向于把它放在后台执⾏,因为遍历⼀个⼤的
⽂件系统可能会花费很⻓的时间(30G字节以上的文件系统)

bash 复制代码
root@hcss-ecs-887f:~/lession1# pwd
/root/lession1
root@hcss-ecs-887f:~/lession1# tree
.
├── d1
│   ├── file1.txt
│   └── filecopy1.txt
├── filecopy1.txt
├── temp.txt
└── txt2
    └── file1.txt

2 directories, 5 files
root@hcss-ecs-887f:~/lession1# find txt2
txt2
txt2/file1.txt
root@hcss-ecs-887f:~/lession1# 
相关推荐
花酒锄作田19 分钟前
个人对Debian桌面系统的简单使用分享
linux
我是唐青枫21 分钟前
Linux diff 命令使用详解
linux·运维·服务器
獭.獭.33 分钟前
Linux -- 操作系统
linux·操作系统·冯诺依曼体系
Antonio91534 分钟前
【音视频】RTMP流媒体服务器搭建、推流拉流
服务器·ffmpeg·音视频
文牧之1 小时前
PostgreSQL 中 VACUUM FULL 对索引的影响
运维·数据库·postgresql
裁二尺秋风1 小时前
Nginx — http、server、location模块下配置相同策略优先级问题
运维·nginx·http
小镇敲码人1 小时前
【Linux深入浅出】之全连接队列及抓包介绍
linux·服务器·网络
我是苏苏1 小时前
在C# WebApi 中使用 Nacos02: 配置管理、服务管理实战
运维·服务器
wacpguo1 小时前
VS Code + Linux 远程开发 go
linux·运维·golang
独隅2 小时前
VMware Workstation 创建虚拟机并安装 Ubuntu 系统 的详细步骤指南
linux·运维·ubuntu