With the terminal: Examples with the Find command

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


16 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   proper said

    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.

  2.   taregon said

    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 😐

  3.   Hugo said

    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

  4.   electron222 said

    Interesting ^ _ ^

    1.    KZKG ^ Gaara said

      How cool the Chakra icon looks hahahahahaha 😀

      1.    sieg84 said

        Mageia's missing 🙂

        1.    KZKG ^ Gaara said

          Yup right 😀
          Right now I am based on this hehehehe. Thanks 🙂

          1.    lesterzone said

            And one for my distro ...

  5.   Archero said

    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 ...?

    1.    Hugo said

      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.

      1.    Archero said

        Thank you very much Hugo, excellent explanation, I can only say how powerful the terminal is in gnu / linux!

  6.   Sandra said

    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?

    1.    KZKG ^ Gaara said

      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

  7.   esthefani said

    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.

  8.   pepG said

    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

  9.   kaike said

    How can I find files that end in numbers? Thanks