当前位置: 首页 > news >正文

【Linux】基础指令(2)

man

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

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

 cp

 功能:复制文件或目录

cp普通文件

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.txt0 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# 

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

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.txt0 directories, 2 files
root@hcss-ecs-887f:~/lession1# 

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

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.txt1 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.txt1 directory, 4 files
root@hcss-ecs-887f:~/lession2# 

mv

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

移动文件或目录

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
└── txt12 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.txt0 directories, 1 file
root@hcss-ecs-887f:~/lession1/txt1# 

重命名文件或目录

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

root@hcss-ecs-887f:~# cd lession1
root@hcss-ecs-887f:~/lession1# tree
.
├── d1
│   ├── file1.txt
│   └── filecopy1.txt
├── filecopy1.txt
└── txt1└── file1.txt2 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.txt2 directories, 4 files
root@hcss-ecs-887f:~/lession1# 

常用选项

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

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

cat

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

查看文件内容

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# 

带行号输出:

root@hcss-ecs-887f:~/lession1# cat -b temp.txt1	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功能类似,用于逐页显示文件内容(适合查看大文件)。

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
基础功能:
 
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.txt1 hello2 03 hello4 15 hello6 27 hello8 39 hello10 411 hello12 513 hello14 615 hello16 717 hello18 819 hello20 921 hello22 1023 hello24 1125 hello26 1227 hello28 1329 hello30 1431 hello32 15
......

 head指令

功能:

打印文件开头10行

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⾥最尾部的内容显⽰在屏幕上,并且不断刷新,使你看到最新的⽂件内容.
root@hcss-ecs-887f:~/lession1# tail temp.txt
hello
1996
hello
1997
hello
1998
hello
1999
hello
2000
root@hcss-ecs-887f:~/lession1# 

date

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

显示当前日期和时间

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
root@hcss-ecs-887f:~/lession1# date "+%Y-%m-%d %H:%M:%S"
2025-05-02 15:09:15
root@hcss-ecs-887f:~/lession1# 

显示相对时间

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 纪元) 到当前时间的秒数(或毫秒/微秒/纳秒)

生成时间戳

date +%s       # 当前时间戳(秒级)
date +%s%N     # 纳秒级时间戳

将时间戳转化为可读格式

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

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

 cal

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

find

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

相关文章:

  • 红鸟3D互动系统棋类源码一键部署教程(含多个打包版本与功能解构)
  • PowerBI实现点击空白处隐藏弹窗(详细教程)
  • NVIDIA NPP 库入门
  • MySQL初阶:数据库基础,数据库和表操作,数据库中的数据类型
  • STM32MP157开发板设置静态IP地址
  • Windows配置grpc
  • mescroll.js 是在 H5端 运行的下拉刷新和上拉加载插件
  • Vue 3 ref() 深度解析:从响应式原理到实战技巧
  • 设计模式简述(十六)门面模式
  • 第 8 篇:B/B+ 树:为海量磁盘数据而生
  • 《操作系统真象还原》调试总结篇
  • B站Michale_ee——ESP32_IDF SDK——FreeRTOS_8 消息缓冲区
  • javascript交换值最好三种
  • 计算机网络——客户端/服务端,URI与URL的区别,以及TCP/IP核心机制全解析
  • (36)VTK C++开发示例 ---纹理贴图四边形
  • 【大模型实战篇】对Qwen3提到的thinking和no thinking混合思考模式的讨论
  • Manus AI多语言手写识别技术解析
  • PostgreSQL 的 VACUUM 与 VACUUM FULL 详解
  • 【git】获取特定分支和所有分支
  • 【Linux深入浅出】之全连接队列及抓包介绍
  • 最火“五一”预订!小长假前两日多地接待游客量两位数增长,出境游订单井喷
  • 全红婵/陈芋汐夺得跳水世界杯总决赛女子双人10米台冠军
  • 中国科学院院士张泽民已任重庆医科大学校长
  • 何立峰出席驻沪中央金融机构支持上海建设国际金融中心座谈会并讲话
  • 辽宁辽阳火灾事故饭店经营者已被控制,善后处置全面展开
  • 安徽省公安厅原副厅长刘海石主动投案,正接受审查调查