2017-07-22 每天2个Linux loacte命令
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案。
(1)用法:
用法: Locate [选项] [参数]
(2)功能:
功能: 在mlocate数据库中搜索条目,用来快速查找文件或目录
(3)选项参数:
1) -d<目录>或--database=<目录>: 指定数据库所在的目录
2) -i, --ignore-case 匹配模式时忽略大小写区别
3) --help: 显示帮助
4) --version: 显示版本信息
5) -d, --database DBPATH 用 DBPATH 替代默认的数据库(/var/lib/mlocate/mlocate.db)
(4)实例:
1)[root@localhost Documents]# loacte less1 在各个目录下查找名为less1的这个文件或者文件夹
复制代码
[root@localhost Documents]# ll
总用量 20
-rw-r--r--. 1 root root 664 5月 9 07:59 head_text
-rw-r--r--. 1 root root 45 5月 9 08:15 less1
-rw-r--r--. 1 root root 57 5月 9 08:16 less2
-rw-r--r--. 1 root root 259 5月 12 21:53 tail_text
-rw-r--r--. 1 root root 216 5月 12 22:24 tempory
[root@localhost Documents]# slocate less1 //经验证,已经没有slocate这个命令
bash: slocate: 未找到命令...
相似命令是: 'locate'
[root@localhost Documents]# loacte less1
bash: loacte: 未找到命令...
相似命令是: 'locate'
2)[root@localhost Documents]# locate Documents
如果是搜索的是文件夹的名称,则默认会先显示该文件夹及以下的各个文件及文件夹
复制代码
[root@localhost Documents]# locate Documents
/home/sunjimeng/Documents
/home/sunjimeng/Documents/head_text
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/usr/share/dbus-1/services/org.gnome.Documents.GDataMiner.service
/usr/share/dbus-1/services/org.gnome.Documents.SearchProvider.service
/usr/share/dbus-1/services/org.gnome.Documents.ZpjMiner.service
/usr/share/glib-2.0/schemas/org.gnome.Documents.enums.xml
3)[root@localhost Documents]# locate newlocate 和updatedb
为了避免新建的文件夹找不到,可以立即更新数据库(updatedb命令)
复制代码
[root@localhost Documents]# touch newlocate //新建文件
[root@localhost Documents]# ll //已经存在
总用量 20
-rw-r--r--. 1 root root 664 5月 9 07:59 head_text
-rw-r--r--. 1 root root 45 5月 9 08:15 less1
-rw-r--r--. 1 root root 57 5月 9 08:16 less2
-rw-r--r--. 1 root root 0 5月 15 18:21 newlocate
-rw-r--r--. 1 root root 259 5月 12 21:53 tail_text
-rw-r--r--. 1 root root 216 5月 12 22:24 tempory
[root@localhost Documents]# locate newlocate //但并找不到
[root@localhost Documents]# updatedb //更新数据库
[root@localhost Documents]# locate newlocate //待计算机反应2秒后,再执行locate命令,就能找到了
/home/sunjimeng/Documents/newlocate
4)查找指定目录下的某个文件,也可以用通配符
[root@localhost /]# locate /home/sunjimeng/Documents/*e
/home/sunjimeng/Documents/newlocate
5)[root@localhost /]# locate -i /home/sunjimeng/Documents/*Cate 在使用通配符时忽略大小写
[root@localhost /]# locate -i /home/sunjimeng/Documents/*Cate
/home/sunjimeng/Documents/newlocate
[root@localhost /]# locate /home/sunjimeng/Documents/*Cate
[root@localhost /]#
6)[root@localhost /]# locate /home/sunjimeng/Documents/le 寻找以特定字符串开头的文件或文件夹
复制代码
[root@localhost /]# locate /home/sunjimeng/Documents/le //不加通配符也可以
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
[root@localhost /]# locate /home/sunjimeng/Documents/le*
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
[root@localhost /]#
7)[root@localhost Documents]# locate --help
复制代码
[root@localhost Documents]# locate --help
Usage: locate [OPTION]... [PATTERN]...
Search for entries in a mlocate database.
-A, --all only print entries that match all patterns
-b, --basename match only the base name of path names
-c, --count only print number of found entries
-d, --database DBPATH use DBPATH instead of default database (which is
/var/lib/mlocate/mlocate.db)
-e, --existing only print entries for currently existing files
-L, --follow follow trailing symbolic links when checking file
existence (default)
-h, --help print this help
-i, --ignore-case ignore case distinctions when matching patterns
-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries
-m, --mmap ignored, for backward compatibility
-P, --nofollow, -H don't follow trailing symbolic links when checking file
existence
-0, --null separate entries with NUL on output
-S, --statistics don't search for entries, print statistics about each
used database
-q, --quiet report no error messages about reading databases
-r, --regexp REGEXP search for basic regexp REGEXP instead of patterns
--regex patterns are extended regexps
-s, --stdio ignored, for backward compatibility
-V, --version print version information
-w, --wholename match whole path name (default)
将 bug 报告给 mitr@redhat.com.
(5)其他:
说明:
locate命令其实是find -name的另一种写法,但是要比后者快得多,
原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/locatedb,
这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,
所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前
,先使用updatedb命令,手动更新数据库。
locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,
updatedb是由cron daemon周期性建立的,locate命令在搜寻数据库时比由整个由硬盘资料来搜寻资料来得快,
但较差劲的是locate所找到的档案若是最近才建立或 刚更名的,可能会找不到,
在内定值中,updatedb每天会跑一次,可以由修改crontab来更新设定值。(etc/crontab)
locate指定用在搜寻符合条件的档案,它会去储存档案与目录名称的数据库内,
寻找合乎范本样式条件的档案或目录录,可以使用特殊字元(如”*” 或”?”等)来指定范本样式,
如指定范本为kcpa*ner, locate会找出所有起始字串为kcpa且结尾为ner的档案或目录,
如名称为kcpartner若目录录名称为kcpa_ner则会列出该目录下包括 子目录在内的所有档案。
2017-07-22 每天2个Linux find命令_初识
Linux下find命令在目录结构中搜索文件,并执行指定的操作。
(1)用法:
用法: find pathname -option [-print | -exec | -ok]
find 路径名 选项参数 [-print | -exec | -ok]
(2)功能:
功能:用于在文件树种查找文件,并作出相应的处理。
(3)命令参数:
1) pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
2) -print: find命令将匹配的文件输出到标准输出。
3) -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;注意{ }和\;之间的空格。
4) -ok:
和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,
在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
(4)选项参数:
1) -name 按照文件名查找文件。
2) -perm 按照文件权限来查找文件。
3) -prune 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth 选项,那么-prune将被find命令忽略。
4) -user 按照文件属主来查找文件。
5) -group 按照文件所属的组来查找文件。
6) -mtime -n +n 按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,
+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。
7) -nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
8) -nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
9) -newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。
10) -empty 查找长度为0的文件或文件夹
11) -type 查找某一类型的文件,诸如:
b -块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
12) -size n: [c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
13) -depth: 在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
14) -fstype: 查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,
该配置文件中包含了本系统中有关文件系统的信息。
15) -mount: 在查找文件时不跨越文件系统mount点。
16) -follow: 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
17) -cpio: 对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
18) -regex<范本样式>: 指定字符串作为寻找文件或目录的范本样式
另外,下面三个的区别:
-amin n 查找系统中最后N分钟访问的文件
-atime n 查找系统中最后n*24小时访问的文件
-cmin n 查找系统中最后N分钟被改变文件状态的文件
-ctime n 查找系统中最后n*24小时被改变文件状态的文件
-mmin n 查找系统中最后N分钟被改变文件数据的文件
-mtime n 查找系统中最后n*24小时被改变文件数据的文件
(5)实例:
1)[root@localhost findDir]# find . 列出当前目录及子目录下所有文件和文件夹
复制代码
[root@localhost Documents]# touch ./findDir/t1.txt //在当前目录的子目录findDir目录下创建t1.txt文本文件
[root@localhost Documents]# cd findDir
[root@localhost findDir]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月 17 03:50 t1.txt
[root@localhost findDir]# find .
.
./t1.txt
[root@localhost findDir]# cd ../
[root@localhost Documents]# find .
.
./less1
./less2
./head_text
./tail_text
./tempory
./newlocate
./uText
./findDir
./findDir/t1.txt
[root@localhost Documents]#
复制代码
用ls命令列出当前目录及子目录下所有文件和文件夹:
复制代码
[root@localhost Documents]# ls -Rl
.:
总用量 20
drwxr-xr-x. 2 root root 19 5月 17 03:50 findDir
-rw-r--r--. 1 root root 664 5月 9 07:59 head_text
-rw-r--r--. 1 root root 45 5月 9 08:15 less1
-rw-r--r--. 1 root root 57 5月 9 08:16 less2
-rw-r--r--. 1 root root 0 5月 15 18:21 newlocate
-rw-r--r--. 1 root root 259 5月 12 21:53 tail_text
-rw-r--r--. 1 root root 216 5月 12 22:24 tempory
-rw-r--r--. 1 root root 0 5月 15 18:34 uText
./findDir:
总用量 0
-rw-r--r--. 1 root root 0 5月 17 03:50 t1.txt
2)[root@localhost /]# find /home/sunjimeng/Documents -name "*.txt" 在特定目录下找到以.txt结尾的文件
[root@localhost /]# find /home/sunjimeng/Documents -name "*.txt"
/home/sunjimeng/Documents/findDir/t1.txt
3)[root@localhost /]# find /home/sunjimeng/Documents -iname "T*" 在特定目录下找到以T开头的文件或文件夹,但忽略大小写(-i操作)
复制代码
[root@localhost /]# find /home/sunjimeng/Documents -name "T*" //不忽略
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
[root@localhost /]# find /home/sunjimeng/Documents -iname "T*" //忽略大小写
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
4)[root@localhost /]# find /home/sunjimeng/Documents \( -name "*.txt" -o -name "*.pdf" \) 用两个条件来查询文件
复制代码
[root@localhost /]# touch /home/sunjimeng/Documents/findDir/{p1.pdf,p2.pdf}
[root@localhost /]# find /home/sunjimeng/Documents \( -name "*.txt" -o -name "*.pdf" \)
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
[root@localhost /]# cd /home/sunjimeng/Documents/findDir
[root@localhost findDir]# find . -name "*.txt" -o -name "*.pdf"
./t1.txt
./T1.txt
./T2.txt
./p1.pdf
./p2.pdf
5)[root@localhost /]# find /home/sunjimeng/Documents ! -name "*.txt" 找寻目录下不是以.txt结尾的文件或目录
复制代码
[root@localhost /]# find /home/sunjimeng/Documents ! -name "*.txt"
/home/sunjimeng/Documents
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
/home/sunjimeng/Documents/head_text
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/home/sunjimeng/Documents/newlocate
/home/sunjimeng/Documents/uText
/home/sunjimeng/Documents/findDir
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
6)[root@localhost sunjimeng]# find /home/ -path "*cume*"
在指定目录下的后代目录中进行路径匹配,匹配完成后列出匹配目录下的所有文件及文件夹及其子目录及文件。
复制代码
[root@localhost sunjimeng]# find /home/ -path "*cume*" //等价于find /home -path "*cume*"
/home/sunjimeng/Documents
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
/home/sunjimeng/Documents/head_text
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/home/sunjimeng/Documents/newlocate
/home/sunjimeng/Documents/uText
/home/sunjimeng/Documents/findDir
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
/home/sunjimeng/Document
/home/sunjimeng/Document/newDir
/home/sunjimeng/Document/newDir/text1
/home/sunjimeng/Document/newDir/text2
/home/sunjimeng/Document/text1
/home/sunjimeng/Document/text1/newDir
/home/sunjimeng/Document/text1/newDir/text1
/home/sunjimeng/Document/text1/newDir/text2
/home/sunjimeng/Document/text2
/home/sunjimeng/Document/text2/newDir
/home/sunjimeng/Document/text2/newDir/text1
/home/sunjimeng/Document/text2/newDir/text2
/home/sunjimeng/Document/text3
/home/sunjimeng/Document/text3/text1
/home/sunjimeng/Document/text3/text2
/home/sunjimeng/Document/text4
/home/sunjimeng/Document/text4/text1
/home/sunjimeng/Document/text4/text2
/home/sunjimeng/Document/mytext
/home/sunjimeng/Document/mytext.txt
7)[root@localhost /]# find /home/sunjimeng -regex ".*\(txt\|pdf\)$" 进行字符串匹配
复制代码
[root@localhost /]# find /home/sunjimeng -regex ".*\(\.txt\|\.pdf\)$" //原始
[root@localhost /]# find /home/sunjimeng -regex ".*\(.txt\|.pdf\)$" //去掉两个斜杠
[root@localhost /]# find /home/sunjimeng -regex ".*\(txt\|pdf\)$" //把点去了
/home/sunjimeng/.cache/tracker/db-version.txt
/home/sunjimeng/.cache/tracker/db-locale.txt
/home/sunjimeng/.cache/tracker/miner-applications-locale.txt
/home/sunjimeng/.cache/tracker/last-crawl.txt
/home/sunjimeng/.cache/tracker/first-index.txt
/home/sunjimeng/.local/share/Trash/files/test1.txt
/home/sunjimeng/.local/share/Trash/files/test2.txt
/home/sunjimeng/.local/share/Trash/files/test3.txt
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
/home/sunjimeng/Document/mytext.txt
8)[root@localhost /]# find /home/sunjimeng/Documents -type d | p | f | c | l | b 查找特定类型的文件
复制代码
[root@localhost /]# find /home/sunjimeng/Documents -type d
/home/sunjimeng/Documents
/home/sunjimeng/Documents/findDir
[root@localhost /]# find /home/sunjimeng/Documents -type p
[root@localhost /]# find /home/sunjimeng/Documents -type f
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
/home/sunjimeng/Documents/head_text
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/home/sunjimeng/Documents/newlocate
/home/sunjimeng/Documents/uText
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
9)[root@localhost /]# find /home -maxdepth 3 -type f 找指定目录下的普通文件,文件目录深度不超过3
复制代码
[root@localhost /]# find /home -maxdepth 3 -type f
/home/sunjimeng/.bash_logout
/home/sunjimeng/.bash_profile
/home/sunjimeng/.bashrc
/home/sunjimeng/.config/user-dirs.dirs
/home/sunjimeng/.config/user-dirs.locale
/home/sunjimeng/.config/gnome-initial-setup-done
/home/sunjimeng/.cache/event-sound-cache.tdb.localhost.localdomain.x86_64-redhat-linux-gnu
/home/sunjimeng/.ICEauthority
/home/sunjimeng/.esd_auth
/home/sunjimeng/.bash_history
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
/home/sunjimeng/Documents/head_text
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/home/sunjimeng/Documents/newlocate
/home/sunjimeng/Documents/uText
/home/sunjimeng/Document/mytext
/home/sunjimeng/Document/mytext.txt
10)[root@localhost /]# find /home/sunjimeng -mindepth 5 -type d
找指定目录下的普通文件,文件目录深度不低于5
复制代码
[root@localhost /]# find /home/sunjimeng -mindepth 5 -type d
/home/sunjimeng/.local/share/evolution/addressbook/trash
/home/sunjimeng/.local/share/evolution/addressbook/system
/home/sunjimeng/.local/share/evolution/addressbook/system/photos
/home/sunjimeng/.local/share/evolution/calendar/trash
/home/sunjimeng/.local/share/evolution/calendar/system
/home/sunjimeng/.local/share/evolution/mail/trash
/home/sunjimeng/.local/share/evolution/memos/trash
/home/sunjimeng/.local/share/evolution/tasks/trash
/home/sunjimeng/.local/share/Trash/files/未命名文件夹
11)[root@localhost /]# find /home/sunjimeng/Documents -atime -2 查找指定时间内修改过的文件
复制代码
[root@localhost /]# find /home/sunjimeng/Documents -atime -2 //两个以内
/home/sunjimeng/Documents
/home/sunjimeng/Documents/newlocate
/home/sunjimeng/Documents/uText
/home/sunjimeng/Documents/findDir
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
[root@localhost /]# find /home/sunjimeng/Documents -atime -1 //一天以内
/home/sunjimeng/Documents
/home/sunjimeng/Documents/findDir
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
12)[root@localhost /]# find . -perm 777 查找权限为777的文件及文件夹
[root@localhost /]# find . -perm 777
[root@localhost /]# find /usr/libexec/gcc -perm 777
/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/liblto_plugin.so
/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/liblto_plugin.so.0
/usr/libexec/gcc/x86_64-redhat-linux/4.8.3
(6)其他:
权限:
读取权限 r = 4
写入权限 w = 2
执行权限 x = 1
775这三个数字代表拥有者,组用户,其他用户的权限。
例如: 7 拥有者有 读取,写入,执行权限
7 组用户有 读取,写入,执行权限
5 其他用户有 读取,执行权限(4+1 = 5)
777 与 775的区别是:其他用户有写入权限,而775的没有。
linux下设置777权限和用户权限
设置www目录的所有文件可写777sudo chmod 777 -R www (这里-R是继承)
设置www目录下的所有文件权限为user所有
sudo chown -hR user www
exit
linux下账户的权限是:drwx------ 表示是那些权限?
首先d是代表这个是一个目录文件,rwx是属主权限rw代表可读写x代表可执行,
后面三个---是属主同组的权限,如果是---代表没有权限,最后三个是除文件属主组以外所有人的权限,
---也是没有权限,所以这个目录只有属主有权限进入,其他人连读的权限也没有,更不用说写入和执行。