您的位置:首页 > 产品设计 > UI/UE

4 Great Tools to Find Files Quickly in Ubuntu

2012-02-23 23:57 274 查看
4 Great Tools to Find Files Quickly in Ubuntu :: Chris Jean

4 Great Tools to Find Files Quickly in Ubuntu

by Chris JeanMarch
4th,
2009

Many of you fellow Ubuntu users will be familiar with the “Search for Files” tool that allows you to look for files. As is true with most things in Linux, there are great desktop tools, but more power can be found in Terminal than any streamlined desktop tool can match.

Today, I’d like to introduce you to a few tools that can turn a chore of finding files into an easy process.

locate

The first tool that you should become comfortable with is also one of the simplest. The locate program works in a similar fashion to most graphical search tools.

Using

Let’s say that you’ve read up on how to modify X to provide enhanced video performance. The document that you are reading says to modify the xorg.conf file, but the document doesn’t say where that is and you don’t know how to find it. You can use locate to find it for you quickly:

locate xorg.confMost likely, your system will return a number of results. For example, on one of my systems, I receive the following output:

/etc/X11/xorg.conf

/usr/share/man/man5/xorg.conf.5.gz

/var/lib/x11/xorg.conf.md5sum

/var/lib/x11/xorg.conf.rosterIt’s clear that /etc/X11/xorg.conf is the file we’re looking for, but what if dozens or even hundreds of results were returned? How could you manage all of that? If you know a little bit about regular expressions (every Linux user really should learn the basics), you can easily use regular expressions to search for directories or files. For example, we can search for an exact match for xorg.conf with the following command:

locate -r '/xorg.conf
If you want to look for files without case sensitivity, you can use the ‘-i’ option. For example:

locate -i 'monthly report'
Something that may confuse you when you first start using locate is that it not only returns matches, but if the match is a directory, it also lists all of the directories and files under that directory. To stop this behavior, use the ‘-b’ option. For example:

locate -b DesktopThis will return a much shorter list of results than if you did not include the ‘-b’ option and all the matches will contain the search term as the final portion of the path.

Important Note

The locate command relies on a slocate database to function. This database caches the file system directory and file names and allows the locate command to operate in moments rather than taking long periods of time to search the entire file system.

This ability doesn’t come without cost though. By default, your system will run a command to update this database once a day at 6:25am. This command will update the database with any changes made to the file system since the last update.

If you’ve made changes recently, such as added or removed large amounts of files and directories, and you want to use the locate command to search through these new directories and files, you can manually update the slocate database at any time with the following command:

sudo updatedbDepending on the volume of changes made since the slocate database was last updated, this could take a few seconds to a few minutes to update. Once it’s done, you’re ready to search again with locate.

which

locate is great for searching the entire file system. However, sometimes you just want to find where a command lives. That’s where which comes into play.

which only does one thing, but it does it well. Given the name of a command, which will tell you where the file for that command resides in the file system.

Let’s imagine that we want to find out where there firefox command resides. We can simply run the following to find out:

which firefoxOn my system, and most likely on your’s as well, which will tell us that it is located at /usr/bin/firefox.

Why would you ever want to search specifically for a command? Imagine that you have some code that needs to replace an old command. You really need to remove the the old one (I usually rename it to the same name followed by “.bak” to keep it around in case I need it) first before putting the new one in its place. which will help you find the file quickly and easily so you don’t have to search everywhere to find it.

whereis

The whereis command is similar to which but goes one step further. Not only does it tell you where the executable file is located at, but it will also locate the source, man page, and other associated directories as well.

Keeping with the which example, let’s ask whereis about the firefox command:

whereis firefoxOn my system, I get the following output:

firefox: /usr/bin/firefox /usr/lib/firefox /usr/lib64/firefox /usr/share/firefoxThis helped me find /usr/lib/firefox/plugins earlier when working on the solution for Flash. That location is where the global add-ons for Firefox are stored.

find

find is the ultimate search tool, the Swiss Army knife of Linux search if you will. find can do everything from search for files based on last modified times, owners and groups, permissions, type (file, directory, symbolic link, etc), whether the file is readable/writable/executable by the current user, file size, whether the file or directory is empty, and much more. You can also search for matches by regular expression.

When you use this tool in combination with the xargs command (which I’ll have to cover some other time), you can do truly amazing things on the command line. Imagine being able to recursively search through the current directory for files ending with “.bak” that were last modified more than 100 days ago and then deleting them with one command:

find . -name '*.bak' -type f -mtime +100 -print0 | xargs -0 /bin/rm -f
It will look very complex at first, but when you master the art of using find, it will become second nature. Soon, you’ll wonder how you ever searched for things without it.

To be frank, there is much more to find than I am willing to cover and there are many, many great introductions to the tool that do a better job than I would. So, I’ll simply point you to three great references that can be used to get going with find.

Example uses of the Linux Command find
(I apologize for the horrible colors in advance) Unix/Linux “find” Command Tutorial
Using the find command
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐