您的位置:首页 > 运维架构 > Linux

文件查找find,设备的使用,虚拟机安装与快照,软硬链接

2017-02-15 18:43 453 查看
grep -r

################文件查找#################

1.locate filename       ##速度很快,在文件数据库中搜索filename信息,updatedb更新文件数据库

2.find

find    查找位置    -条件  
条件值  -exec 动作 {} \;

            -name

            -not    ##条件否

            -user   ##对所属用户进行查找

            -group  ##对所属组进行查找

            -size   ##文件大小进行查找  -num 小于num  +num大于num  num等于num'

            -perm   ##根据文件权限进行查找  后跟mode(最大为7  rwx)+mode  -mode

            -maxdepth   ##最大深度 num

            -mindepth   ##最小深度 后跟数字

            -a      ##且

            -o      ##或

            -type   f   ##普通文件

                d   ##目录

                c   ##字符设备

                s   ##套接字

                p   ##管道

                b   ##快设备

                l   ##连接

---------------对文件类型进行查找--------------

find -type 参数

[root@desktop12 ~]# cd /mnt

[root@desktop12 mnt]# touch file{1..5}

[root@desktop12 mnt]# ll

total 0

-rw-r--r--. 1 root root 0 2月  13 20:18 file1

-rw-r--r--. 1 root root 0 2月  13 20:18 file2

-rw-r--r--. 1 root root 0 2月  13 20:18 file3

-rw-r--r--. 1 root root 0 2月  13 20:18 file4

-rw-r--r--. 1 root root 0 2月  13 20:18 file5

[root@desktop12 mnt]# find -type f

./file1

./file2

./file3

./file4

./file5

[root@desktop12 mnt]# find -type d

.

./yyyy

-----------------对文件名字进行查找-------------------

find 查找地 -name 文件名

[root@desktop12 mnt]# find /etc  -name  passwd

/etc/passwd

/etc/pam.d/passwd

-----------------对所属组与用户进行查找-------------

find -group 组名

find -user  用户名

[root@desktop12 mnt]# ll

total 0

-rw-r--r--. 1 student student 0 2月  13 20:18 file1

-rw-r--r--. 1 root    student 0 2月  13 20:18 file2

-rw-r--r--. 1 student root    0 2月  13 20:18 file3

-rw-r--r--. 1 root    root    0 2月  13 20:18 file4

-rw-r--r--. 1 root    root    0 2月  13 20:18 file5

drwxr-xr-x. 2 root    root    6 2月  13 20:19 yyyy

[root@desktop12 mnt]# find -group student

./file1

./file2

[root@desktop12 mnt]# find -group student -a -user root ##同时满足组与用户

./file2

[root@desktop12 mnt]# find -group student -o -user root ##满足组或用户

.

./file1

./file2

./file4

./file5

./yyyy

--------------对文件权限进行查找--------------------

find -perm -mode

find -perm /mode

find -perm mode(+mode)

[root@desktop12 mnt]# ll

total 8

-r--------. 1 student student 12 2月  13 20:43 file1

-r--r-----. 1 root    student 28 2月  13 20:43 file2

-rw-r--r--. 1 student root     0 2月  13 20:18 file3

-rw-rw-rw-. 1 root    root     0 2月  13 20:18 file4

----------. 1 root    root     0 2月  13 20:18 file5

drwxr-xr-x. 2 root    root     6 2月  13 20:19 yyyy

[root@desktop12 mnt]# find -perm -400

.

./file1

./file2

./file3

./file4

./yyyy

[root@desktop12 mnt]# find -perm /400

.

./file1

./file2

./file3

./file4

./yyyy

[root@desktop12 mnt]# find -perm 400

./file1

[root@desktop12 mnt]# find -perm +400

./file1

[root@desktop12 mnt]# find -perm -440

.

./file2

./file3

./file4

./yyyy

[root@desktop12 mnt]# find -perm -442

./file4

[root@desktop12 mnt]# chmod 777 file5

[root@desktop12 mnt]# ll

total 8

-r--------. 1 student student 12 2月  13 20:43 file1

-r--r-----. 1 root    student 28 2月  13 20:43 file2

-rw-r--r--. 1 student root     0 2月  13 20:18 file3

-rw-rw-rw-. 1 root    root     0 2月  13 20:18 file4

-rwxrwxrwx. 1 root    root     0 2月  13 20:18 file5

drwxr-xr-x. 2 root    root     6 2月  13 20:19 yyyy

[root@desktop12 mnt]# find -perm -442

./file4

./file5

[root@desktop12 mnt]# find -perm 442

[root@desktop12 mnt]# find -perm +442

[root@desktop12 mnt]# find -perm /442

.

./file1

./file2

./file3

./file4

./file5

./yyyy

[root@desktop12 mnt]# find -perm /111

.

./file5

./yyyy

[root@desktop12 mnt]#
4000
find -perm /222

.

./file3

./file4

./file5

./yyyy

****************

find -perm -mode 

find -perm -440   #u>=4 且 g>=4 且 o>=0

find -perm /mode 

find -perm /440   #u有4的权限 或 g有4的权限

find -perm /640   #u有4或2的权限 或 g有4的权限

find -perm /777   #u有1或2或4的权限 或 g有1或2或4的权限
或 o有1或2或4的权限

find -perm mode  == find -perm+mode

find -perm 440    #u=4 且 g=4 且 o=0

#########################软链接与硬链接########################
软链接:是一类特殊的文件,里面包含了另一个文件的路径名,类似于windows下的快捷方式
硬链接:是一个文件的一个或多个文件名,即一个数据区被多个文件共享,对文件的更改是同步的,这些文件名会增加文件的count值,有几个文件名count值就会是几。只有在删掉最后一个文件名(在count值为1的时候),那个数据区才会被删除。

软链接:(ln -s 被链接的文件  链接到哪块)

[root@desktop12 mnt]# ln -s /mnt/westos /boot

[root@desktop12 mnt]# ls -li /boot/westos

1566794 lrwxrwxrwx. 1 root root 11 2月  13 22:00 /boot/westos -> /mnt/westos

[root@desktop12 mnt]# rm -fr ./*

[root@desktop12 mnt]# ls -li /boot/westos

1566794 lrwxrwxrwx. 1 root root 11 2月  13 22:00 /boot/westos -> /mnt/westos

[root@desktop12 mnt]# rm -fr /boot/westos

硬链接:(ln  被链接的文件  链接到哪块)

[root@desktop12 mnt]# touch westos

[root@desktop12 mnt]# ln /mnt/westos  /boot

[root@desktop12 mnt]# ls -li /boot/westos

8846766 -rw-r--r--. 2 root root 0 2月  13 22:01 /boot/westos

[root@desktop12 mnt]# rm -fr /boot/westos

[root@desktop12 mnt]# ls -li westos

8846766 -rw-r--r--. 1 root root 0 2月  13 22:01 westos

################设备的使用#####################

-------设备的挂载------------

fdisk -l        ##查看真实存在的设备

cat /proc/partitions    ##系统能够识别的设备

blkid           ##系统能够挂载使用的设备id

df -h           ##查看设备被系统使用的情况

df命令查看情况:



blkid命令查看情况:



[root@foundation12 mnt]# umount /dev/sdb4

umount: /mnt: target is busy.

        (In some cases useful info aboutprocesses that use

         the device is found by lsof(8)or fuser(1))  ##设备正在被使用,不能卸载
解决:fuser -kvm /dev/sdb4    ##停止正在使用的/dev/sdb4设备

fuser -kvm 设备|挂载点 #-k kill
,-v显示详细信息,-m扫描设备

-----------------------------------------------------------------------------------------------------

##############虚拟机的安装与管理#######################

virt-manager            ##开启图形管理工具

virt-viewer vmname      ##显示虚拟机,vmname表示虚拟机名称

virsh list          ##列出正在运行的vm

virsh list --all        ##列出所有vm

virsh start vmname      ##运行指定vm

virsh shutdown vmname       ##正常关闭指定vm

virsh destroy vmname        ##强行结束指定vm

virsh create vmname.xml     ##临时恢复指定vm,vmname表示前端管理文件

virsh define vmname.xml     ##永久恢复vm

virsh undefine  vmname      ##删除vm的前端管理,不会删除存储

[root@foundation12 ~]# cd /etc/libvirt/qemu/

[root@foundation12 qemu]# ls

networks  server.xml

[root@foundation12 qemu]# cp yang.xml /mnt

[root@foundation12 qemu]# rm -fr yang.xml

[root@foundation12 qemu]# virsh create /mnt/yang.xml 

Domain yang created from /mnt/yang.xml

[root@foundation12 qemu]# virsh define /mnt/yang.xml

Domain yang defined from /mnt/yang.xml

[root@foundation12 qemu]# ls

desktop.xml  networks  server.xml yang.xml

[root@foundation12 ~]# cd /var/lib/libvirt/images/

[root@foundation12 images]# ls

yang.qcow2

[root@foundation12 images]# rm -fr yang.qcow2

####脚本安装虚拟机###

[root@foundation12 ~]# vim virt-creat.sh

  1 #!/bin/bash

  2 virt-install \

  3 --name $1 \

  4 --ram 1024 \

  5 --file/var/lib/libvirt/images/$1.qcow2 \

  6 --file-size 8 \

  7 --cdrom/mnt/rhel-server-7.2-x86_64-dvd.iso & &> /dev/null

[root@foundation12 ~]# sh virt-creat.sh yang

--------虚拟机快照---------

因为经常会在虚拟机上进行一些危险的操作,导致虚拟机崩溃,为了避免经常重新安装虚拟机,有了虚拟机快照的功能,给虚拟机建立快照,平常用快照,快照崩溃,删除快照,重新建立快照。快照功能与虚拟机功能一致。

从虚拟机yang上建立快照:

[root@foundation12 ~]# vim vm-creat.sh

  1 #!/bin/bash

  2 qemu-img create -f qcow2 -b/var/lib/libvirt/images/yang.qcow2 /var/lib/libv    irt/images/$1.qcow2 &>/dev/null

  3 virt-install \

  4 --name $1 \

  5 --ram 1024 \

  6 --file/var/lib/libvirt/images/$1.qcow2 \

  7 --import &> /dev/null&

快照崩溃后重置快照:

[root@foundation12 ~]# vim vm-reset.sh

  1 #!/bin/bash

  2 virsh destroy $1

  3 rm -fr/var/lib/libvirt/image/$1.qcow2

  4 qemu-img create -f qcow2 -b/var/lib/libvirt/images/yang.qcow2 /var/lib/libv    irt/images/$1.qcow2 &>/dev/null

  5 virsh start $1

  6virt-viewer $1 &>/dev/null &

一般为了节省资源,都不装图形功能,但是有些操作又需要图形,我们可以在执行脚本后添加以下代码,开启一个vnc端口,前提是有vnc服务,然后在另一台有图形功能的主机上就能用图形显示没有图形的主机的图形。

开启vnc端口,在脚本后加这几行代码:(此处将端口地址设为7000,这台机器提示设置在5900以后)

--vnc \

--vncport=7000 \

--vnclisten=172.25.254.12 &> /dev/null &
在有图形的主机上进行的操作:

vncviewer ip地址:端口地址
比如:  vncviewer172.25.254.12:7000

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux red hat 运维