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

【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# 

http://www.xdnf.cn/news/3434.html

相关文章:

  • 冯·诺依曼体系:现代计算机的底层逻辑与百年传承
  • C++ 与 Lua 联合编程
  • Python-pandas-操作Excel文件(读取数据/写入数据)及Excel表格列名操作详细分享
  • 单链表操作(single list)
  • Python高级爬虫之JS逆向+安卓逆向1.7节: 面向对象
  • NY204美光闪存MT29F8T08EQLCHL5-QA:C
  • python设置word字体的方法
  • GBDT 基本概述
  • JVM——JVM 是如何执行方法调用的?
  • 华为云Astro轻应用利用自定义连接器调用第三方接口实际操作
  • 【家政平台开发(98)】解锁家政平台新姿势:业务模式创新与多元化发展
  • C++11新特性_标准库_std::array
  • 软连接和硬连接【Linux操作系统】
  • Spring Boot中集成Guava Cache或者Caffeine
  • 接口测试实战指南:从入门到精通的质量保障之道
  • 【安装指南】Centos7 在 Docker 上安装 RabbitMQ4.0.x
  • 芯片中的pad、strap和probe
  • C++11新特性_委托构造函数
  • 《Android 应用开发基础教程》——第十一章:Android 中的图片加载与缓存(Glide 使用详解)
  • 铸铁划线平板:多行业的精密测量工具(北重铸铁平板厂家)
  • golang常用库之-标准库text/template
  • C++负载均衡远程调用学习之消息队列与线程池
  • 【前端知识】Vue3状态组件Pinia详细介绍
  • 同城跑腿小程序帮取帮送接单抢单预约取件智能派单同城配送全开源运营版源码优创
  • Python实例题:Python获取小说数据并分析
  • 计算方法实验四 解线性方程组的间接方法
  • 使用 n8n 创建一个定时获取“RSS新闻“的工作流
  • (35)VTK C++开发示例 ---将图片映射到平面2
  • 期刊、出版社、索引数据库
  • 从0搭建Transformer