26 May 2010 ~ Comments Off

Finding Files from Ubuntu Commandline

This is a basic entry on how to search for files in Linux, and more specifically how to find a software package that a file belongs too.

Find files using “locate” from the commandline:

There are lots of different find tools out there, but I tend to go for the old-school one called “locate”.  Locate basically uses a little database to spit out what your looking for.  By default it will update nightly using cron.  But if you want to update the locate database manually type:

sudo updatedb &

That will update the database and run it in the background so you don’t have to wait for it to finish.

Here are some basic examples of how to use locate to find files:

locate foo

Will locate any files with foo somewhere in the document name.  That could mean Myfoodocument.txt, or hello.foo, etc.

locate .foo 

Would locate any files with .foo somewhere in the filename.

To get a little more specific you may want to widdle down your list.  So if you do a search for “ssh” for example you’ll get hundreds of entries.  But if you know your file is in the “etc” directory somewhere you could do a search like so:

locate ssh | grep etc

This will locate all files with ssh in it and pipe (send) those files to the “grep” command which lets you specify a search string.  Here is another example.  Lets say you want to find a image file and you know it’s a png file and it has logo somewhere in the name.  You could do:

locate logo | grep .png

If your not sure if logo or png is upper or lower case you can specify the case-insensitive option like so:

locate -i logo | grep -i .png

Locate files using the “find” command

The find command is great if you wanted to look in real-time.  Because it’s a real-time search it can be slow for the same reason if you are going to search through a lot of different things.

Basic find example:

find -name '*tmp'

This will find any files or directories with “tmp” somewhere in the name in the current directory and it’s sub-directories.

There is a lot of other things you can do with find.  You can search for files of a specific size, type, permission, etc.  You can search based on file attributes and/or it’s name.   I’m too lazy right now to give examples for all those.

One of my favorite find features is sending the results of the find to a command.  For example, lets save you want to find all the .svn folders in a directory and delete them.  Tedious by hand, but using find you could do it like so:

find . -name '*.svn' -exec /bin/rm -Rf '{}'

With this command you are searching for any file with *.svn the executing (-exec) the results.  The command is /bin/rm -Rf, the results of the find go inside of the ‘{}’.


Find software package that a file belongs too

First you’ll want to install this program that lets you do this kind of search:

apt-get install apt-file

Then you’ll need to update it’s database:

apt-file update

To search you type “apt-file search <packagename>” .  For example:

apt-file search ssh

This will show you any packages that have a file with “ssh” in it.  You can get more specific by typing out a full path.

apt-file search /etc/init.d/ssh

That is very specifc and for example would return:  openssh-server


Comments are closed.