How to find files in Linux using 'locate'

本文介绍如何使用locate命令在Linux系统中高效查找文件。通过不同选项如-q、-n、-i和-l,用户可以定制搜索过程,实现权限受限环境下的错误消息抑制、结果数量限制、大小写不敏感搜索及调整搜索速度与安全性。

原文:http://www.codecoffee.com/tipsforlinux/articles/20.html

The simplest way to find files under Linux is to use the locate program. This article explains how to use locate tool to find your files easily. When you run locate for the first time, you might get an error message. To get it working refer to the Note at the bottom of the page.

If you want to do more than simply find the location of files then you can use the excellent find tool. The find tool allows you do refined searches. Article No. 21 explains the find tool in detail.


Procedure :

Here are various ways to use locate tool

-

$ locate index.html

It would produce a list of the locations where you could find files that are named as index.html. This might produce results as follows

/home/pamela/index.html
/usr/local/games/pam/index.html

-

You could use the -q option to suppress error messages. Error messages would typically be messages stating that permission to access files were not allowed since you are only a user (not superuser). The -q option would suppress any other error messages as well

$ locate "*.dat" -q

-

You could use the -n option to limit the number of returned results to a specific number. E.g. you could ask for only 10 search results by the following command

$ locate "*.c" -n 10

This would return the first 10 files that end in .c that Linux finds.

-

You could use the -i option in case you wanted to perform a case insensitive search. The case of the filenames would not be considered

$ locate INDEX.HTML -i

-

You could make your search faster by typing the following

$ locate index.html -l 0

Typing -l 1 takes longer time for the search to complete but is more secure. This is the default action that takes place when the -l option is not mentioned.

-

There are few more parameters that can be used with locate, but they are generally not used. You can find them in the man pages.

Note : When you run 'locate' for the first time, you will get a message stating that the db is not available. Linux requires a database of all the files, which locate uses to find the files. The message would also state that you can log in as root and run a particular command. Simply doing so would get locate to work perfectly. To change to the superuser mode type 'su' at the prompt. Then type the command you were asked to run (as indicated in the message). Type <Ctrl>+D to exit the superuser mode. Now you can continue with locate commands as normal.

转载于:https://www.cnblogs.com/qike/p/5417213.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值