Find the answer to your Linux question:
Results 1 to 9 of 9
I'm trying to find a way to search multiple directories using the find command. For example, I want to find all files in directories called /home/username/HTML, /home/username/Documents, & /var/www that ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! Scallywag's Avatar
    Join Date
    Jul 2009
    Location
    Black Mountain, NC
    Posts
    24

    Find command


    I'm trying to find a way to search multiple directories using the find command.

    For example, I want to find all files in directories called /home/username/HTML, /home/username/Documents, & /var/www that end with ~ (tilde) and move or copy them to a directory called .Trash.

    Any ideas on how to do this?

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    476
    welcome to the forum

    "find" lets you specify directories to search, so try something like this:

    find /home/username/HTML /home/username/Documents /var/www *~ \
    -exec cp '{}' .Trash \;

    where the first '\' represents the line continuing.
    v. "man find"

    another way would be with a "for loop" in the shell, something like this

    for i in /home/username/HTML /home/username/Documents /var/www
    do
    cd $i; cp *~ /.Trash; cd /
    done
    the sun is new every day (heraclitus)

  3. #3
    Just Joined! Scallywag's Avatar
    Join Date
    Jul 2009
    Location
    Black Mountain, NC
    Posts
    24

    Thanks

    I'll give it a try. I appreciate it....

  4. #4
    Just Joined! Scallywag's Avatar
    Join Date
    Jul 2009
    Location
    Black Mountain, NC
    Posts
    24
    Quote Originally Posted by Scallywag View Post
    I'll give it a try. I appreciate it....
    I've tried the code you sent me and found that it takes a long time to run through the file system directories.

    I've create a bash script as follows:
    # Cleanup bash script
    find /home/username/Business -iname '*.*~' -type f -exec mv '{}' /home/username/.Trash \;
    find /home/username/HTML -iname '*.*~' -type f -exec mv '{}' /home/username/.Trash \;
    find /home/username/Documents -iname '*.*~' -type f -exec mv '{}' /home/username/.Trash \;
    find /var/www -iname '*.*~' -type f -exec mv '{}' /home/username/.Trash \;
    echo "All files cleaned up."

    It works very well. I changes it so it moves the files instead of copying them. I, also, copied it to the /usr/local/bin directory so its available globally.

  5. #5
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    What you can do to speed up the find command is running (as root):
    Code:
    updatedb
    And find your stuff with
    Code:
    slocate filename
    Note that `updatedb` creates a database of all your files, and `slocate` is 'just' a `grep pattern database` equivalent, so the output is different from what you get with the find command.

    But it is a lot quickerer
    Can't tell an OS by it's GUI

  6. #6
    Just Joined! Scallywag's Avatar
    Join Date
    Jul 2009
    Location
    Black Mountain, NC
    Posts
    24

    Found something that works...

    I've found the following that I'm using as a bash script that works the best:

    # Cleanup bash file cleans /home/username & /var/www directories of all files ending with ~
    # and sends error messages to /dev/null directory.
    find /home/username -iname '*.*~' -type f -exec mv '{}' /home/username/.Trash \; 2>/dev/null
    find /var/www -iname '*.*~' -type f -exec mv '{}' /home/username/.Trash \;
    echo "All files cleaned up."

    I appreciate everyones help...

  7. #7
    Just Joined!
    Join Date
    Jul 2009
    Posts
    7
    updatedb is also good but depending on file system size and new files created on system it takes time to update the DB so find might give fast output. If the file system is not that big then definately updatedb is good.

  8. #8
    Linux Guru Lazydog's Avatar
    Join Date
    Jun 2004
    Location
    The Keystone State
    Posts
    2,677
    Dumb question but why are you using find for this? Why not a simple 'mv' call?

    # Cleanup bash file cleans /home/username & /var/www directories of all files ending with ~
    mv /home/username/*~ /home/username/.Trash
    mv /var/www/*~ /home/username/.Trash
    echo "All files cleaned up."

    Regards
    Robert

    Linux
    The adventure of a life time.

    Linux User #296285
    Get Counted

  9. #9
    Linux User saivin's Avatar
    Join Date
    Dec 2008
    Location
    Bengaluru, India
    Posts
    305
    Quote Originally Posted by Scallywag View Post
    find /home/username -iname '*.*~' -type f -exec mv '{}' /home/username/.Trash \; 2>/dev/null
    Is '*.*~' different from '*~' ?

    Quote Originally Posted by Lazydog View Post
    Dumb question but why are you using find for this? Why not a simple 'mv' call?
    Scally wants to search those files recursively in those directories, I guess.
    A candle looses nothing by lighting other candles. - Khalil Zibran.
    Registered Linux User #490076

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •