Here are some examples that we can take advantage of when we use Find, a command to search for files or folders.
To get help for the command run:
man find
to leave the manual, just press the key [q] (valid for any manual).
In the following examples, the period (.) After find (find.) Means that we are looking in the folder indicated by the prompt. It can be substituted for any valid path like / home /.
Examples:
Just search for files with a pattern.
find . -type f -name "*.deb"
find and copy to / home / pepe /
find . -type f -name "*.deb" -exec cp -f {} /home/pepe/ \;
Find the Thumbs.db files and delete them.
find . -type f -name "Thumbs.db" -exec rm -f {} \;
Create a pure text file with the md5 files in the directory.
find . -type f -print0 | xargs -0 -n 1 md5sum >> md5.txt
Delete annoying .svn folders.
find | grep "\.svn$" | xargs rm -fr
Replace one text with another.
find -type f | xargs sed -i "s/TEXTO/OTRO/g" *.php
Find files updated up to a day ago.
find /var/log/[a-z]* \*.sql -mtime +1
To Generate the md5sums files of the DEB packages:
find . -type f ! -regex ‘.*\.hg.*’ ! -regex ‘.*?debian-binary.*’ ! -regex ‘.*?DEBIAN.*’ -printf ‘%P ‘ | xargs md5sum > DEBIAN/md5sums
delete all files other than .txt (obviously .txt can be anything)
find. ! -name "* .txt" -exec rm {} \;
search without case-sensitive matches:
find. -iname «* foobar *»
Remark: The -exec command cannot be executed with the -iname parameter.
Excellent 😉 this command should be mandatory to know, before I was scared by the options that existed in 'man' to be able to do searches, but giving it the opportunity I could see how powerful it is when it comes to finding what I forgot on my hard drive 😐
Find is certainly useful, especially for dealing with filenames that include spaces and other unusual characters. For example, I remember once that there was no way I could manage to compress a set of directories, until it occurred to me to use find with xargs (which is much faster than -exec by the way), and problem solved.
Another one of my preferred uses for the find command is to recursively change permissions:
find . -type d -print0 | xargs -0 chmod 755
find . -type f -print0 | xargs -0 chmod 644
Interesting ^ _ ^
How cool the Chakra icon looks hahahahahaha 😀
Mageia's missing 🙂
Yup right 😀
Right now I am based on this hehehehe. Thanks 🙂
And one for my distro ...
Thanks, the commands very useful, I have a doubt I remember that in Ubuntu I once used the locate command, is it some alias of find or ...?
Negative. locate, mlocate y locate are other search commands that unlike find, they use a database that needs to be periodically updated with the command updatedb.
Both types of commands have their uses. I for example usually use updatedb followed by mlocatelocate when I really want to search for something quick in a directory with a lot of data that I know is not updated too frequently (for example, a package in a repository), and find when I want to do something more complex like combine the search results with another command, or when I just don't want to generate the database because I know that the directory I'm going to search doesn't contain too much information.
Thank you very much Hugo, excellent explanation, I can only say how powerful the terminal is in gnu / linux!
Hi, I see this is an old topic, but hopefully you can still help me.
I am learning to use the regexp since I saw I have a document and I am looking for the words error or fail and its derivatives errors or failed or failure etc and my regexp is:
: / \ (. * \ (error | fail \). * \) /
When executing it, it tells me that there are no matches 🙁 but to
:/\(.*\(mistake\).*\)/
o
: / \ (. * \ (fail \). * \) /
If you find matches, could you tell me how I'm wrong?
What is the full line that you are putting?
To test and see if I find the solution.
On the other hand, anyway if you want you can check here: https://blog.desdelinux.net/?s=expresiones+regulares
Please help me, I want to copy the files that end in * _ZFIR0069.TXT to another path and and add a date at the end, I am doing a command:
date = $ (date + »% Y% m% d%»)
find / xcom_rep / FATF / exit / 42 -name * _ZFIR0069.TXT -exec cp -p {} / backup / FATF / exit / 42 / {} _ $ date \;
But the result is:
{} _20160225% -> but it only copies one file of all and it is renamed that way
What I want is for it to copy all the files and have this format * _ZFIR0069_ $ date .TXT
Greetings.
What is the difference between find * -type d and find / home / pepe -type d? I want to list the directories of my account and I don't understand why the first one does it correctly and the second one does not. help to
How can I find files that end in numbers? Thanks