Linux基本命令

find 命令

格式:find   【文件路径】【选项 选项的值】

文件路径:  ~ 表示$HOME目录

             . 表示当前目录

             / 表示根目录

选项有: -name:根据文件的名称搜索,支持通配符

-type f /d:  f ---代表普通文件

            d ----代表目录

1)

[root@localhost ~]# find / -name "a.txt" -type f

/root/a.txt

[root@localhost ~]#

2)在linux 系统中,如果要查找的文件的名称不清晰,可以使用部分文件名+*搜索

获取/etc/中以.conf结尾的文件

[root@localhost ~]# find /etc/ -name "*.conf" -type f

[root@localhost ~]# find /etc/ -name "http*" -type f

/etc/sysconfig/httpd

/etc/logrotate.d/httpd

/etc/httpd/conf/httpd.conf

2.创建并设置文件最后的修改的时间、

格式:语法 touch -m -d 日期时间格式 文件名称

[root@localhost ~]# touch /opt/def.txt

[root@localhost ~]# stat /opt/def.txt

  文件:"/opt/def.txt"

  大小:0          块:0          IO 块:4096   普通空文件

设备:fd00h/64768d Inode:33653822    硬链接:1

权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)

环境:unconfined_u:object_r:usr_t:s0

最近访问:2024-07-15 16:00:06.275277389 +0800

最近更改:2024-07-15 16:00:06.275277389 +0800

最近改动:2024-07-15 16:00:06.275277389 +0800

创建时间:-

[root@localhost ~]# touch -m -d "2023-01-02 12:00" /opt/def.txt

[root@localhost ~]# stat /opt/def.txt

  文件:"/opt/def.txt"

  大小:0          块:0          IO 块:4096   普通空文件

设备:fd00h/64768d Inode:33653822    硬链接:1

权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)

环境:unconfined_u:object_r:usr_t:s0

最近访问:2024-07-15 16:00:06.275277389 +0800

最近更改:2023-01-02 12:00:00.000000000 +0800

最近改动:2024-07-15 16:03:01.705345245 +0800

创建时间:-

[root@localhost ~]# ls -l /opt/*.txt

-rw-r--r--. 1 root root 0 7月  15 16:04 /opt/b.txt

-rw-r--r--. 1 root root 0 7月  15 16:04 /opt/c.txt

-rw-r--r--. 1 root root 0 1月   2 2023 /opt/def.txt

-rw-r--r--. 1 root root 0 7月  15 16:04 /opt/d.txt

3.通过文件的最后修改时间搜索文件

语法 :find 文件路径 -mtime +days/-days

[root@localhost opt]# rm -rf *

[root@localhost opt]# touch a.txt

[root@localhost opt]# touch b.txt -m -d "2024-7-10 00:00"

[root@localhost opt]# touch c.txt -m -d "2024-7-13 00:00"

[root@localhost opt]# touch d.txt -m -d "2024-7-14 00:00"

[root@localhost opt]#  ls -l /opt/*.txt

-rw-r--r--. 1 root root 0 7月  15 16:10 /opt/a.txt

-rw-r--r--. 1 root root 0 7月  10 00:00 /opt/b.txt

-rw-r--r--. 1 root root 0 7月  13 00:00 /opt/c.txt

-rw-r--r--. 1 root root 0 7月  14 00:00 /opt/d.txt

[root@localhost opt]# find /opt/ -name "*.txt" -mtime -3  //find 文件路径 -mtime +days/-days

/opt/a.txt

/opt/c.txt

/opt/d.txt

4.Find exec选型

#查看目录中的txt文件

[root@localhost opt]# ls -l *.txt

#没有e.txt文件,在前面被删除了

-rw-r--r--. 1 root root 0 7月 14 13:54 a.txt

-rw-r--r--. 1 root root 0 7月 13 00:00 b.txt

-rw-r--r--. 1 root root 0 7月 12 00:00 c.txt

-rw-r--r--. 1 root root 0 7月 11 00:00 d.txt

# 创建文件并且指定文件修改日期

[root@localhost opt]# touch -m -d "2024-7-10 00:00" e.txt

[root@localhost opt]# ls -l

总用量 0

-rw-r--r--. 1 root root 0 7月 14 13:54 a.txt

-rw-r--r--. 1 root root 0 7月 13 00:00 b.txt

-rw-r--r--. 1 root root 0 7月 12 00:00 c.txt

-rw-r--r--. 1 root root 0 7月 11 00:00 d.txt-rw-r--r--. 1 root root 0 7月 10 00:00 e.txt

# 查找三天以前的文件

[root@localhost opt]# find /opt/ -name "*.txt" -type f -mtime +3

/opt/e.txt

# 使用-exec 文件调用rm函数 {}表示前面find查到的内容 \;表示标识符

# 这里在{}后面没有打空格报错了,在{}后应该打空格

[root@localhost opt]# find /opt/ -name "*.txt" -type f -mtime +3 -exec rm -

rf {}\;

find: 遗漏“-exec”的参数

[root@localhost opt]# find /opt/ -name "*.txt" -type f -mtime +3 -exec rm -

rf {} \;

总用量 0

-rw-r--r--. 1 root root 0 7月 14 13:54 a.txt

-rw-r--r--. 1 root root 0 7月 13 00:00 b.txt

-rw-r--r--. 1 root root 0 7月 12 00:00 c.txt

-rw-r--r--. 1 root root 0 7月 11 00:00 d.txt

5.快速生成指定大小的文件

find 路径 -size 文件大小 [常用单位 k M G]

size值 搜索等于size的文件

-size值 【0,size值)

+size值 (size值,正无穷)

扩展命令 dd

使用dd创建扩展命令

生成指定大小的测试文件

语法  dd if=/dev/zero of=文件名称 bs=1M count=1

if表示输入文件

of表示输出文件

bs代表字节为单位的块大小

count代表被复制的块

其中/dev/zore是一个字符设备,会不断地返回0字节的文件

【案例1】

#查看文件

[root@localhost opt]# ls

a.txt b.txt c.txt d.txt

# 删除文件

[root@localhost opt]# rm -rf *

# 创建名称为a.txt,大小为1m的文件

[root@localhost opt]# dd if=/dev/zero of=a.txt bs=1M count=1

记录了1+0 的读入

记录了1+0 的写出

1048576字节(1.0 MB)已复制,0.0027841 秒,377 MB/秒

# 查看文件信息,使用单位字节

[root@localhost opt]# ls -l

总用量 1024-rw-r--r--. 1 root root 1048576 7月 14 14:37 a.txt

# 查看文件信息,使用文件大小单位 默认m

[root@localhost opt]# ls -lh

总用量 1.0M

-rw-r--r--. 1 root root 1.0M 7月 14 14:37 a.txt

生成其他大小的文件

[root@localhost opt]# dd if=/dev/zero of=b.txt bs=5M count=1

记录了1+0 的读入

记录了1+0 的写出

5242880字节(5.2 MB)已复制,0.0111468 秒,470 MB/秒

[root@localhost opt]# dd if=/dev/zero of=c.txt bs=10M count=1

记录了1+0 的读入

记录了1+0 的写出

10485760字节(10 MB)已复制,0.0476839 秒,220 MB/秒

[root@localhost opt]# ls -l

总用量 16384

-rw-r--r--. 1 root root 1048576 7月 14 14:37 a.txt

-rw-r--r--. 1 root root 5242880 7月 14 14:42 b.txt

-rw-r--r--. 1 root root 10485760 7月 14 14:42 c.txt

[root@localhost opt]# ls -lh

总用量 16M

-rw-r--r--. 1 root root 1.0M 7月 14 14:37 a.txt

-rw-r--r--. 1 root root 5.0M 7月 14 14:42 b.txt

-rw-r--r--. 1 root root 10M 7月 14 14:42 c.txt

6.tree

1、tree命令作用
以树状结构查看目录下的内容

2.tree命令优点
使用tree命令可以很直接看到目录下的内容,不用进入每个目录然后ls看一下了

3、tree命令安装
yum -y install tree 

4、tree命令的使用
Linux tree命令用于以树状图列出目录的内容。

执行tree指令,它会列出指定目录下的所有文件,包括子目录里的文件。

语法:

tree [-aACdDfFgilnNpqstux][-I <范本样式>][-P <范本样式>][目录...]
-a 显示所有文件和目录。
-A 使用ASNI绘图字符显示树状图而非以ASCII字符组合。
-C 在文件和目录清单加上色彩,便于区分各种类型。
-d 显示目录名称而非内容。
-D 列出文件或目录的更改时间。
-f 在每个文件或目录之前,显示完整的相对路径名称。

以树状图列出当前目录结构。可直接使用如下命令:tree

 
[root@localhost opt]#  tree /var/log/
/var/log/
├── anaconda
│   ├── anaconda.log
│   ├── ifcfg.log
│   ├── journal.log
│   ├── ks-script-foxDg3.log
│   ├── ks-script-xoNdUE.log
│   ├── packaging.log
│   ├── program.log
│   ├── storage.log
│   ├── syslog
│   └── X.log
├── audit
│   └── audit.log
├── boot.log
├── boot.log-20240617
├── boot.log-20240711
├── boot.log-20240714
├── btmp
├── btmp-20240711
├── chrony
├── cron
├── cron-20240711
├── cron-20240714
├── dmesg
├── dmesg.old
├── firewalld
├── grubby
├── grubby_prune_debug
├── httpd
├── lastlog
├── maillog
├── maillog-20240711
├── maillog-20240714
├── messages
├── messages-20240711
├── messages-20240714
├── rhsm
 
 
├── secure
├── secure-20240711
├── secure-20240714
├── spooler
├── spooler-20240711
├── spooler-20240714
├── tallylog
├── tuned
│   └── tuned.log
├── vmware-network.1.log
├── vmware-network.2.log
├── vmware-network.3.log
├── vmware-network.4.log
├── vmware-network.5.log
├── vmware-network.6.log
├── vmware-network.log
├── vmware-vgauthsvc.log.0
├── vmware-vmsvc.log
├── vmware-vmsvc-root.log
├── vmware-vmtoolsd-root.log
├── wtmp
└── yum.log	

7.scp

使用scp下载文件和目录

语法

scp [选项] 用户名@linux主机地址:/资源路径 linux本地文件路径

复制文件

1.查看克隆机的ip地址,并且清空opt目录中的文件

2.查看原主机的ip地址,并且查看opt目录中的数据

3.从原主机上下载/opt/a.txt到克隆机上的/opt目录,注意如果有询问,输入yes

再输入密码即可

复制目录1.源主机opt目录下创建目录,并且将a.txt b.txt c.txt复制一份在新目录中

2.在克隆机上使用scp指令复制目录到本地opt目录,需要添加-r选项,无法执行

第二次连接主机,不需要再次输入yes

-r 代表递归,主要作用文件夹

scp上传文件

语法 scp [选项] 本地主机资源路径 {远程主机}用户名@主机ip:放置路

上传文件,将克隆机中的a.txt文件上传到源主机中

# 以下操作都是在原主机192.168.135.129中执行的

# 清空opt目录中的文件

[root@localhost ~]# rm -rf /opt/*

[root@localhost ~]# ls /opt/

#ssh管理克隆机

[root@localhost ~]# ssh -lroot -p22 192.168.135.132

The authenticity of host '192.168.135.132 (192.168.135.132)' can't be

established.

ECDSA key fingerprint is SHA256:CkKRXsYIVPxBU2aCwVy42OZPQpcOnsPp4lK0qesv0is.

ECDSA key fingerprint is

MD5:cb:e1:2c:97:ca:f1:54:7a:e6:c2:d1:22:32:41:04:c8.

# 第一次连接需要确认输入yes

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.135.132' (ECDSA) to the list of known

hosts.

#输入密码

root@192.168.135.132's password:

Last login: Sun Jul 14 15:58:47 2024

上传目录,把克隆机中的folder目录上传到源主机的opt目录

要求必须启用ssh服务

systemctl start sshd

systemctl stop sshd

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值