was successfully added to your cart.

Useful commands to do search in ubuntu

By June 11, 2011IT Stuffs

There are lots of time we want to look for a file but we forgot where we put it. Or, we want to search through multiple log files to find what’s causing the application to fail and what the error message is. We can do this easily by using some simple commands under Linux command shell.

To find a file, you want to first sudo to root so you have the rights to look into all directories to search for a file. I usually use one of these two ways:

find . | grep myfile.txt

or

find /home -iname 'myfile.txt'

The “find” command is very powerful. You can even use it to do things like, “find all test.txt files and delete them”. Here is how

find /home -iname 'test.txt' -exec rm {} ;

The “{}” means whatever the “find” command’s output is, put it there. The “” means CR so it will run the next rm command in the next line.

How about move all *.doc files in my home directory to my /home/myhome/docs folder? Now you know it is easy, right?

find /home/myhome -iname '*.doc' -exec mv {} /home/myhome/docs/. ;

Another common things to do is to look for a key word in multiple files. Let’s say you want to look for the word “error” in all the files under the /log folder:

grep -i -n 'error' *

Boy, Linux really make things easy, isn’t it?

Leave a Reply