属性:
人的属性:性别,身高,体重,年龄。
文件的属性:大小,用户,组,权限,创建时间。
[root@localhost ~]# stat /etc/hosts
File: ‘/etc/hosts’
Size: 158 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 67109833 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2023-05-23 07:23:45.977483911 +0800
Modify: 2013-06-07 22:31:32.000000000 +0800
Change: 2023-05-07 20:28:56.812001998 +0800
Birth: -
最近访问: 2021-05-12 12:13:30 #access访问,浏览,针对内容
最近更改: 2021-05-10 10:31:31 #modify最后更改时间,针对内容
最近改动: 2021-05-10 10:31:31 #change 状态改变,属性改变
查看文件属性:
[root@localhost ~]# ls -li
total 32
135116374 -rw-r--r-- 1 root root 74 May 24 05:04 ab.txt
135116371 -rw-r--r-- 1 root root 8 May 24 04:43 a.txt
135116375 -rw-r--r-- 1 root root 87 May 24 05:05 b.txt
67118104 drwxr-xr-x 2 root root 6 May 23 22:51 dir
539794 drwxr-xr-x 2 root root 6 May 23 22:46 dir1
67109271 drwxr-xr-x 2 root root 6 May 23 22:46 dir2
135116367 drwxr-xr-x 2 root root 6 May 23 22:46 dir3
202163968 drwxr-xr-x 2 root root 6 May 23 22:46 dir4
539797 drwxr-xr-x 2 root root 6 May 23 22:46 dir5
135116365 -rw-r--r-- 1 root root 0 May 23 22:38 oldboy
134320002 -rw-r--r-- 1 root root 104 May 24 04:48 oldboy.txt
135116360 -rw-r--r-- 1 root root 30 May 24 04:13 oldgirl.txt
135116370 -rw-r--r-- 1 root root 0 May 23 22:50 seq
135116368 -rw-r--r-- 1 root root 21 May 24 04:22 seq.txt
135116369 -rw-r--r-- 1 root root 74 May 24 04:58 test.txt
135116373 -rw-r--r-- 1 root root 74 May 24 05:01 u.txt
135116373 -rw-r--r-- 1 root root 74 May 24 05:01 u.txt
1 2 3 4 5 6 7 8 9 10
第1列,索引节点,形态是数字。
第2列,首字符代表文件类型
中间9个字符,文件权限
.和selinux(高级安全组件) 相关符号。
第3列,硬链接数。
第4列,用户。
第5列,用户组。
第6列,文件大小(目录不是)。
第7-9列,日期时间。
第10列,文件名。
windows文件类型:根据扩展名来的。
.doc word文档
.avi .mp4 视频
-ppt .md .txt
Linux文件类型:
Linux下面不根据扩展名确定类型,但是依然会有扩展名,为了给人看。一眼看清楚。系统不识别扩展名,那么系统怎么确定文件类型呢?
ls -li 第2列,首字符代表文件类型切皆文件。
- 普通文件
(白色命令)
d 目录 directory
(浅蓝色命令)
l 软链接文件 link
(快捷方式)
[root@localhost ~]# ls -l /bin
lrwxrwxrwx. 1 root root 7 May 7 20:28 /bin -> usr/bin
[root@localhost ~]# ln -s /etc/hosts /tmp/a.hosts
[root@localhost ~]# cat /tmp/a.hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
c 字符设备
[root@localhost ~]# ls /dev/ -l
total 0
crw-rw---- 1 root video 10, 175 May 23 07:23 agpgart
crw------- 1 root root 10, 235 May 23 07:23 autofs
drwxr-xr-x 2 root root 120 May 23 07:23 block
drwxr-xr-x 2 root root 80 May 23 07:23 bsg
crw------- 1 root root 10, 234 May 23 07:23 btrfs-control
drwxr-xr-x 3 root root 60 May 23 07:23 bus
lrwxrwxrwx 1 root root 3 May 23 07:23 cdrom -> sr0
drwxr-xr-x 2 root root 2960 May 23 07:23 char
crw------- 1 root root 5, 1 May 23 07:23 console
lrwxrwxrwx 1 root root 11 May 23 07:23 core -> /proc/kcore
drwxr-xr-x 3 root root 60 May 23 07:23 cpu
crw------- 1 root root 10, 61 May 23 07:23 cpu_dma_latency
crw------- 1 root root 10, 62 May 23 07:23 crash
drwxr-xr-x 5 root root 100 May 23 07:23 disk
crw-rw---- 1 root audio 14, 9 May 23 07:23 dmmidi
drwxr-xr-x 2 root root 80 May 23 07:23 dri
crw-rw---- 1 root video 29, 0 May 23 07:23 fb0
lrwxrwxrwx 1 root root 13 May 23 07:23 fd -> /proc/self/fd
crw-rw-rw- 1 root root 1, 7 May 23 07:23 full
crw-rw-rw- 1 root root 10, 229 May 23 07:23 fuse
crw------- 1 root root 246, 0 May 23 07:23 hidraw0
crw------- 1 root root 10, 228 May 23 07:23 hpet
drwxr-xr-x 2 root root 0 May 23 07:23 hugepages
crw------- 1 root root 10, 183 May 23 07:23 hwrng
b 块设备
[root@localhost ~]# ls -l /dev/sd*
brw-rw---- 1 root disk 8, 0 May 23 07:23 /dev/sda
brw-rw---- 1 root disk 8, 1 May 23 07:23 /dev/sda1
brw-rw---- 1 root disk 8, 2 May 23 07:23 /dev/sda2
s socket文件 (进程通信时才会用到)
[root@localhost ~]# ls -l /dev/log
srw-rw-rw- 1 root root 0 May 23 07:23 /dev/log
查看文件类型:file
[root@localhost ~]# file /etc/hosts
/etc/hosts: ASCII text
[root@localhost ~]# file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=aaf05615b6c91d3cbb076af81aeff531c5d7dfd9, stripped
[root@localhost ~]# file /var/log/wtmp
/var/log/wtmp: data
wc 查看行数,字符数等:
-l 查看行数
-L 查看字符数
实践:
[root@localhost ~]# wc -l /etc/hosts
2 /etc/hosts
[root@localhost ~]# echo oldboy|wc -L
6
查找命令:
1) which查看二进制命令所在路径:(从PATH环境变量路径里查找)
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost ~]# which cp
alias cp='cp -i'
/usr/bin/cp
特殊注意:
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
2) Whereis查看文件及文件的帮助等的路径:
-b 查看二进制命令所在路径which:
[root@localhost ~]# whereis -b ls
ls: /usr/bin/ls
[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
3)locate查找文件及相关内容(内置数据库,通过updatedb)
[root@localhost ~]# yum install mlocate -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
[root@localhost ~]# locate ls
/usr/share/vim/vim74/compiler/modelsim_vcom.vim
/usr/share/vim/vim74/compiler/onsgmls.vim
/usr/share/vim/vim74/ftplugin/alsaconf.vim
/usr/share/vim/vim74/ftplugin/protocols.vim
/usr/share/vim/vim74/syntax/alsaconf.vim
/usr/share/vim/vim74/syntax/cuplsim.vim
/usr/share/vim/vim74/syntax/lscript.vim
/usr/share/vim/vim74/syntax/lsl.vim
/usr/share/vim/vim74/syntax/lss.vim
/usr/share/vim/vim74/syntax/plsql.vim
/usr/share/vim/vim74/syntax/protocols.vim
/usr/share/yum-cli/utils.py
/usr/share/yum-cli/utils.pyc
4) find查找:
(1)-name 按名字查找
find 路径 选项1 【参数1】 选项2 【参数2】
按名字查找:
find / -name “hosts”
[root@localhost date]# find / -name "hosts"
/etc/hosts
按名字模糊查找:
find / -name “ho*ts”
[root@localhost date]# find / -name "ho*ts"
/etc/hosts
(2) 按类型查找:
-type
-type c
b block (buffered) special
c character (unbuffered) special
d directory
f regular file
l symbolic link;
s socket
查找目录:
[root@localhost ~]# find /root -type d
/root
/root/dir
/root/dir1
/root/dir2
/root/dir3
/root/dir4
/root/dir5
查看文件:
[root@localhost ~]# find /root -type f
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/.bash_history
/root/.localhost.txt.swp
/root/seq
/root/a.txt
/root/test.txt
/root/u.txt
/root/ab.txt
/root/b.txt
/root/.viminfo
/root/oldboy.txt
/root/oldgirl.txt
/root/oldboy
/root/seq.txt
组合查找:find默认就是取交集(-a) and,并集(-o)or
查看文件类型为文件,并且名字为hosts
find / -name “hosts”-a -type f
find / -name “hosts”-type f
[root@localhost date]# find / -name "hosts" -a -type f
/etc/hosts
[root@localhost date]# find / -name "hosts" -type f
/etc/hosts
并集:查找名为hosts,或者类型为d
find / -name “hosts”-o -type d -name “oldboy”
[root@localhost date]# find / -name "hosts" -o -type d -name "oldboy"
/etc/hosts
取反: !查找名字不是file1
[root@localhost ~]# mkdir /date -p
[root@localhost ~]# touch /date/file{1..3}
[root@localhost ~]# find /date -name "file1"
/date/file1
[root@localhost ~]# find /date -! -name "file1"
/date
/date/file2
/date/file3
(3) 按大小查找:
-size +1M #大于1M
-size -1M #小于1M
-size 1M #1M
其他单位: K ,G
[root@localhost date]# find ./ -size -10M
./
./file1
./file2
./file3
[root@localhost date]# find ./ -size +10M
(4) -mtime按修改时间查找
-atime按访问时间查找
-ctime按改变时间查找
-mtime +7 #7天以前的
-mtime -7 #最近7天的
-mtime 7 #第7天
[root@localhost date]# find ./ -mtime -7
./
./file1
./file2
./file3
./file01
./file02
./file03
./file04
./file05
./file06
./file07
./file08
./file09
./file10
./file11
./file12
./file13
./file14
./file15
./file16
./file17
./file18
./file19
./file20
./file21
./file22
./file23
./file24
./file25
./file26
./file27
./file28
./file29
./file30
[root@localhost date]# find ./ -mtime +7
测试:复制即可,模拟每天创建一个文件,连续30天。
for n in {01..30};do date -s “2030/05/0$n”;touch /date/file$n;done
[root@localhost date]# for n in {01..30};do date -s “2030/05/0$n”;touch /date/file$n;done
date: invalid date ‘“2030/05/001”’
date: invalid date ‘“2030/05/002”’
date: invalid date ‘“2030/05/003”’
date: invalid date ‘“2030/05/004”’
date: invalid date ‘“2030/05/005”’
date: invalid date ‘“2030/05/006”’
date: invalid date ‘“2030/05/007”’
date: invalid date ‘“2030/05/008”’
date: invalid date ‘“2030/05/009”’
date: invalid date ‘“2030/05/010”’
date: invalid date ‘“2030/05/011”’
date: invalid date ‘“2030/05/012”’
date: invalid date ‘“2030/05/013”’
date: invalid date ‘“2030/05/014”’
date: invalid date ‘“2030/05/015”’
date: invalid date ‘“2030/05/016”’
date: invalid date ‘“2030/05/017”’
date: invalid date ‘“2030/05/018”’
date: invalid date ‘“2030/05/019”’
date: invalid date ‘“2030/05/020”’
date: invalid date ‘“2030/05/021”’
date: invalid date ‘“2030/05/022”’
date: invalid date ‘“2030/05/023”’
date: invalid date ‘“2030/05/024”’
date: invalid date ‘“2030/05/025”’
date: invalid date ‘“2030/05/026”’
date: invalid date ‘“2030/05/027”’
date: invalid date ‘“2030/05/028”’
date: invalid date ‘“2030/05/029”’
date: invalid date ‘“2030/05/030”’
.和./都表示当前目录
..和../都表示上级目录
【对找到的东西进行处理】
-exec 执行动作
方法一:
find /date -name “file*”-mtime +7 -exec rm -f {} \;
[root@localhost date]# find /date -name “file*” -mtime +7 -exec rm -f {} \;
[root@localhost date]# find /date -name “file*” -mtime +7
[root@localhost date]# ls
file01 file03 file05 file07 file09 file10 file12 file14 file16 file18 file2 file21 file23 file25 file27 file29 file30
file02 file04 file06 file08 file1 file11 file13 file15 file17 file19 file20 file22 file24 file26 file28 file3
[root@localhost date]# find /date -name “file*” -mtime 7
[root@localhost date]# find /date -name “file*” -mtime 7 -exec rm -f {} \;
[root@localhost date]# find /date -name “file*” -mtime 7
[root@localhost date]# ls
file01 file03 file05 file07 file09 file10 file12 file14 file16 file18 file2 file21 file23 file25 file27 file29 file30
file02 file04 file06 file08 file1 file11 file13 file15 file17 file19 file20 file22 file24 file26 file28 file3
方法二:
命令行:
$(命令)或`命令`,拼路径拼命令
rm -f $(find /date -name “file*”-type f -mtime +7)
rm -f `find /date -name “file*”-type f -mtime +7`
[root@localhost ~]# rm -f $(find /date -name “file*” -type f -mtime +7)
[root@localhost ~]# ls
ab.txt a.txt b.txt dir dir1 dir2 dir3 dir4 dir5 oldboy oldboy.txt oldgirl.txt seq seq.txt test.txt u.txt
[root@localhost ~]# rm -f $`find /date -name “file*” -type f -mtime +7`
[root@localhost ~]# ls
ab.txt a.txt b.txt dir dir1 dir2 dir3 dir4 dir5 oldboy oldboy.txt oldgirl.txt seq seq.txt test.txt u.txt
方法三:
Xargs
-n 分组
[root@localhost date]# seq 10 >oldboy.txt
[root@localhost date]# xargs -n 3 <oldboy.txt
1 2 3
4 5 6
7 8 9
10
-i
/data/file 30
[root@localhost date]# find /date -name “file*” -mtime 7|xargs -i rm -f {}
[root@localhost date]# ls
file01 file03 file05 file07 file09 file10 file12 file14 file16 file18 file2 file21 file23 file25 file27 file29 file30
file02 file04 file06 file08 file1 file11 file13 file15 file17 file19 file20 file22 file24 file26 file28 file3 oldboy.txt
[root@localhost date]# find /date -name “file*” -mtime +7|xargs -i rm -f {}