Showing posts with label find. Show all posts
Showing posts with label find. Show all posts

Find executables

To find all the executables files

find . -type f -executable -name "whatever*"

Using commands recursively with find

I copied my home folder to Windows for a backup and when I copied them back for some weird reason all the directories had executables privileges.

This is what I did to get rid of it recursively

$ find . -type d -exec chmod 755 {} \;

you can pass any command as an argument to -exec. Some useful things that you can do with find are here

Performing an operation on multiple files using a single command line in bash

This is quite useful if you need to move files from multiple locations to a single place.

Let's assume that you have been searching for all *.jpg files on your system and you need to move all of them to a specific folder /home/deadpan/images,

To get the files do,

$ find . -name "*.jpg" . This would get you all the .jpg files.

To move all these files from various location to a single folder do,

$ for file in `find . -name "*.jpg"`; do mv $file /home/deadpan/images; done

By using regular expressions in find, as discussed here, you can get very specific in your search.

Using find and grep on linux to search for strings within files

If you are working from the console and you need to find a string or let us say a function name in some file you can use grep and find to help you out along with some regular expressions.

Getting the files first
Lets say that you are looking only for files with ext *.c use:

$ find . -type f -name "*.c"

This finds all such files from (.) the current directory. If you need to find something from some other directory use:

$find /path_to_the_directory -type f -name "*.c"

The -type f looks only for files.

Now if you want to find both *.c and *.h files use:

$find . -type f -name "*.c" -or -name "*.h"

You can use -or (OR) -o to add more such extensions

$find . -type f -name "*.c" -or -name "*.cpp" -or -name "*.h"

This would list all the files with the extension *.c, *.cpp and *.h

If you want to use regular expressions you can use:

$find . -type f -name '\*\.[ch]'

This will list all the files with extension *.h or *.c

Similarly if you want files which start with a Capital letter you can use

$find . -type f -name '[A-Z][a-x]*\.[ch]' and so on.

or if you want a complex regular expression:
$find . -type f -name '[A-Z0-9._%+-]+\.[ch]'

Searching the string in the files got
Now that we can got our list of files which we are interested in, let us try to search for our string. Assuming that I am trying to find (say) "MyFunction" I would use

$ grep "MyFunction" `find . -type f -name '[A-Z]\*\.[ch]'`

This would search in each and every file as found by find and would try to grep for MyFunction.

If you don't know the case, you can ignore it using -i with grep

grep -i "MyFunction" `find . -type f -name '[A-Z]\*\.[ch]'`

Update: Using egrep with Xargs to handle long argument list -> here