Tip 3: find

From Vlsiwiki
Jump to: navigation, search

One of the most useful unix commands is "find". This command can recursively search through directories and either perform an operation or just check for file/directory attributes.

For example, to find all files (and not directories) below the current directory, you can do this:

find . -type f

It will output something like this:

[09:40:59][dhcp-59-165:~/vlsi/vcdutils]$ find . -type f
./.svn/entries
./.svn/format
./.svn/prop-base/vcd2csv.pl.svn-base
./.svn/text-base/lfsr.vcd.svn-base
./.svn/text-base/vcd2csv.pl.svn-base
./lfsr.vcd
./vcd2csv.pl


You can find all files with user execute permissions like this:

[09:51:59][dhcp-59-165:~/vlsi]$ find . -type f -perm +u+x

The + before u+x specifies that at least that permission bit must be set.


If you want to find a file with a certain file size, for example, you can specify that. This will find all large files (>50M) under the current directory:

[09:45:03][dhcp-59-165:~/vlsi]$ find . -type f -size +50000k
./adaptive_clock/counter/.svn/text-base/log2.svn-base
./adaptive_clock/counter/log2
./mdensity/MS8/.svn/text-base/f000001109.svn-base
./mdensity/MS8/f000001109
./mdensity/WIMSflat/CHIP/layout/.svn/text-base/layout.oa.svn-base
./mdensity/WIMSflat/CHIP/layout/layout.oa
./thermal_sta/unique_thermal_maps/fpu/thermal_testing/add/low/joules/.svn/text-base/fpu.ptrace.svn-base
./thermal_sta/unique_thermal_maps/fpu/thermal_testing/add/low/joules/fpu.ptrace


You can match files of a given name with:

[09:47:40][dhcp-59-165:~/vlsi]$ find . -name foo\*
./signalstorm_flow/IIT_template/.svn/text-base/footprint.def.svn-base
./signalstorm_flow/IIT_template/footprint.def
./signalstorm_flow/work/.svn/text-base/footprint.def.svn-base
./signalstorm_flow/work/footprint.def
./vlsida/.svn/text-base/footer.html.svn-base
./vlsida/footer.html

Note that the asterick is escaped since it is passed to the find command. If you don't, it will get expanded to all the files in the current directory.



Given the matches, you can also perform an action on the files with "- exec". This can be dangerous if you use it with "rm", so be careful!! To display the file info of all files under the current subdirectory:

[09:47:46][dhcp-59-165:~/vlsi]$ find . -type f -name foo\* -exec ls -al {} \;
-r--r--r--  1 mrg  staff  95 Feb 11  2008 ./signalstorm_flow/IIT_template/.svn/text-base/footprint.def.svn-base
-rw-r--r--  1 mrg  staff  95 Feb 11  2008 ./signalstorm_flow/IIT_template/footprint.def
-r--r--r--  1 mrg  staff  95 Feb 13  2008 ./signalstorm_flow/work/.svn/text-base/footprint.def.svn-base
-rw-r--r--  1 mrg  staff  95 Feb 13  2008 ./signalstorm_flow/work/footprint.def
-r--r--r--  1 mrg  staff  181 Jun 20 07:46 ./vlsida/.svn/text-base/footer.html.svn-base
-rw-r--r--  1 mrg  staff  181 Jun 20 07:46 ./vlsida/footer.html

The {} gets replaced with the matching filename and the \; ends the exec command. I rarely use this to remove some files mat


You can find more information with "man find". find is also useful with the command "xargs" which accepts a list of files and can do a more advanced "-exec" with it.