Linux学习之路(二)

4.Linux文件查找工具。

Linux经常使用locate与find作为文件查找命令。find可以认为是系统自带的命令,功能也挺多但是使用方法相对有点繁琐。find查找的是实时文件数据,一般用于查询明确知道文件目录及文件名的时候,可以按照参数将查询出来的文件做进一步操作如:打印、删除、执行命令等。find命令对使用者的要求比较高,要相当熟练的操作,但是功能相对丰富。

相对于find命令locate命令就比较简单而且效率比较快,因为locate命令安装时会创建一个档案数据库,里面记录了当前文件系统的文件数据,locate命令查找的就是这个数据库中的数据所以效率相对find命令会比较高。缺点就是因为数据库中的数据不会实时更新,如果最近修改了文件名那么可能使用该命令就会搜索不到文件。要更新数据库需要执行updatedb命令执行locate数据库。一般在crontab定时任务中会加入一个定时任务每天跑一次updatedb,这个动作一般都会在命令安装的时候自动设置一次(未经确认,请自行研究)。另外一个比较耐用的功能就是locate默认是模糊查找的哦,所以这里选择安装locate命令作为查找工具

root@454009d432a4:/# apt-cache search locate            //搜索locate安装包
mlocate - quickly find files on the filesystem based on their name    //我们要安装的资源
dlocate - fast alternative to dpkg -L and dpkg -S
gameconqueror - locate and modify a variable in a running process (GUI)
libcommons-discovery-java - locates classes that implement a given Java interfac
e
libcommons-discovery-java-doc - locates classes that implement a given Java inte
rface (documentation)
...
...
...
root@454009d432a4:/# apt-get install mlocate      //安装资源
...
...
...

root@454009d432a4:/# locate -h          //命令说明
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
  -p, --ignore-spaces    ignore punctuation and spaces when matching patterns
  -t, --transliterate    ignore accents using iconv transliteration 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)

root@454009d432a4:/# locate cron    //查找cron相关的文件
/etc/cron.d
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
/etc/crontab
/etc/cron.d/.placeholder
/etc/cron.daily/.placeholder
/etc/cron.daily/apt-compat
/etc/cron.daily/dpkg
/etc/cron.daily/mlocate      //安装mlocate资源时默认添加的每天定时任务
...
...
...

更详细的命令说明参考下面链接

资料来源链接:https://blog.csdn.net/looper66/article/details/55254682

5.Linux管道命令“|”

管道命令是命令模式下非常好用的技术,是一种将命令的结果作为其他命令的输入信息的技术。

该命令只能将命令的正确结果输出传递给下一个命令作为输入,如果命令报错则不能传递给下个命令

举例:

//正常的结果
root@454009d432a4:/# apt-cache search make | more      //将apt-cache search make 命令的输出结果作为more命令的输入信息,因为more命令有分页输出的效果所以就形成了分页输出搜索结果的效果
cmake-extras - Extra CMake utility modules
cmake-fedora - Set of scripts and cmake modules that simplify the release proces
s
cmake-qt-gui - Qt based user interface for CMake (cmake-gui)
colormake - simple wrapper around make to colorize output
colortail - log colorizer that makes log checking easier
--More--

//错误的结果
root@454009d432a4:/# 66666 | apt-cache search    //如果66666不是一个正确的命令所以apt-cache search没有输入,这时候相当于两个命令都会报错
bash: 66666: command not found            //66666的错误提示
E: You must give at least one search pattern    //apt-cache search的错误提示

资料链接:https://blog.csdn.net/wirelessqa/article/details/8968381

6.Linux命令输出重定向“>”、“>>”

管道命令的概念其实跟 “>”与“>>”符号的概念非常像。只不过“>”与“>>”符号一般只作用于文件而不是命令。

以下将“>”与“>>”暂且称为输出重定向符号。输出重定向符号的作用是将命令的正确输出重定向到指定文件中(即写入到指定文件中),如果文件不存在则新增文件。“>”,“>>”区别在于“>”是将文件中的内容覆盖为输出内容,“>>”是在文件后面追加输出内容。例如:

root@454009d432a4:/# apt-cache search net-tools >> workdir/check.log    //将apt-cache search net-tools的结果写入workdir/check.log文件中,如果文件不存在自动创建文件
root@454009d432a4:/# apt-cache search net-tools > workdir/check.log    //将apt-cache search net-tools的结果覆盖workdir/check.log文件中的内容,如果文件不存在自动创建文件

资料链接:https://blog.csdn.net/qq_36076233/article/details/69061684

猜你喜欢

转载自www.cnblogs.com/walterfong/p/9717923.html