Linux find Examples
Last updated:- Find by partial name
- Find all files with given extension except those in directories starting with dot (hidden directories on linux)
- Find, ignore directories
- Delete all ".txt" files in current directory and child directories
- Use {} as placeholder for find + xargs
- Restrict output to files only
- Restrict output to directories only
- Use sudo, find and xargs
find
is frequently used withxargs
. See also Gnu Xargs Examples
Find by partial name
Find files whose name match "foobar"
in current directory and child directories
$ find . -name "*foobar*"
Find all files with given extension except those in directories starting with dot (hidden directories on linux)
(useful to leave stuff like .svn and .git untouched by changes)
$ find . -not -path '*/\.*' -type f -name '*.rb*'
Find, ignore directories
EXAMPLE: Find all files with given extension except those in directories starting with dot (hidden directories on linux)
(useful to leave stuff like .svn and .git untouched by changes)
$ find . -not -path '*/\.*' -type f -name '*.rb*'
Delete all ".txt" files in current directory and child directories
Caution is advised, because actions here may not be undone!
$ find . -name "*.txt" | xargs rm -rf
Use {} as placeholder for find + xargs
this is used when you have complex commands and you want to tell the shell where you want to place the argument.
Move all .txt
files in the current directory and down to ~/
$ find . -name "*.txt" | xargs -I {} mv {} ~/
Restrict output to files only
Directories are files too so, by default, find returns them as well. If you want to only return files in a call to find
, use the -type f
modifier
$ find . -type f | xargs grep foo
Restrict output to directories only
Useful find directories like nbproject and .svn and .git, for instance:
$ find . -type d -name "foobar" | xargs rm -rf
Use sudo, find and xargs
When you need to use a command that requires sudo
to run and you want that command to run with the results of find
, write it just after the pipe(|
).
Assuming you only want to return files (-type f
modifier):
$ find /etc/some/restricted/directory/ -type f | sudo xargs sed -i 's/foo/bar/g'
Other resources