
The Linux Newbies
(noobs) may find it diffcult to search for files. X users may be able
to search for files with a GUI tools but if you want to start learning
the real stuff you need to start using the terminal more often. Thats
why We are going to show you how to search without X with the
find
command. This command searches the directory tree rooted at each given
file name by evaluating the given expression from left to right,
according to the rules of precedence. Here are some useful examples for
using the find command :
kickar@linnewbies ~ $ find /home -user
kickar
........................................................
kickar@linnewbies ~ $
I have cut the output here, because it is pretty long. This example
lists every file in the /home directory under the user
kickar.
kickar@linnewbies ~ $ find /usr -name
*java
/usr/portage/dev-java
/usr/portage/dev-java/ant-owanttask/files/MultipleCopy.java
/usr/portage/dev-java/java-dep-check/files/Main-0.2.java
..............................................
kickar@linnewbies ~ $
This one lists every file in the /usr directory ending with
java.
If you want the listed files to start with java for example put the
*
in front of the file name.
In the next example i have created an example directory foo and i have
put a folder and a file in it. I have modified the folder 13 days ago
and the file 14 :
kickar@linnewbies ~/foo $ ls -l
total 4
drwxr-xr-x 2 kickar kickar 4096 Apr 16 17:37 Bone Thugs 'n' Harmony -
The Collection: Volume One
-rw-r--r-- 1 kickar kickar 0 Apr 15 09:28 xorg-config.patch
kickar@linnewbies ~/foo $ find -mtime +14
./xorg-config.patch
kickar@linnewbies ~/foo $
With this example you can see that I used
-mtime
paratemter. I listed the files that were modified more than 14 days
ago.
kickar@linnewbies ~/foo $ ls -l
total 4
drwxr-xr-x 2 kickar kickar 4096 Apr 16 17:37 Bone Thugs 'n' Harmony -
The Collection: Volume One
-rw-r--r-- 1 kickar kickar 0 Apr 15 09:28 xorg-config.patch
kickar@linnewbies ~/foo $ find -name xorg*
| xargs rm -f
kickar@linnewbies ~/foo $ ls -l
total 4
drwxr-xr-x 2 didkoddd didkoddd 4096 Apr 16 17:37 Bone Thugs 'n' Harmony
- The Collection: Volume One
kickar@linnewbies ~/foo $
Here i combined
find and
xargs
(Please see
Howto xargs
for more info). My purpose was to find all the files starting with xorg
in the foo derectory and delete them. Seccess :) Really easy a?
Another great thing on the find command is that if you have a couple of
users on your computer you can search for files and folders by owner.
In the following example i am searching for all files and folder
starting with Boo* for the user kickar :
linnewbies / # find /home -name Boo* -user
kickar
/home/kickar/BookShelves.ZIP
/home/kickar/Books
/home/kickar/Books/Books
/home/kickar/.wine/drive_c/Program Files/StacK's SOFT/SMS Messager/Book
/home/kickar/.wine/drive_c/Program Files/Adobe/Adobe Dreamweaver
CS4/configuration/Content/Reference/JavaScript/Boolean.html
linnewbies / #
In the next example is shown how to find writable directories and list
them :
kickar@linnewbies /home $ find -type d
-perm 777 -ls
2129923 12
drwxrwxrwx 65 kickar kickar 12288 May 2 10:42
./kickar
kickar@linnewbies /home $ ls -l
total 12
drwxrwxrwx 65 kickar kickar 12288 May 2 10:42 kickar
-rw-r--r-- 1 root root 0 Feb 19 15:47 test1
kickar@linnewbies /home $
Another useful example is searching for a file or a directory that a
newer than file or a dir:
kickar@linnewbies ~/test $ ls -l
total 8
drwxr-xr-x 2 didkoddd didkoddd 4096 May 2 14:05 test1
drwxr-xr-x 2 didkoddd didkoddd 4096 May 2 14:07 test2
kickar@linnewbies ~/test $ find -newer
test1 -type d -ls
6930597 4
drwxr-xr-x 4 kickar kickar 4096 May 2 14:07 .
6930599 4
drwxr-xr-x 2 kickar kickar 4096 May 2 14:07 ./test2
kickar@linnewbies ~/test $
NOTE : If you want to search for files just replace
-type
d with
-type f.
This is the most commong usage of the find command. You can combine a
lot more commands with find as well. We will describe that in later on
articles.