Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Misc > Find command

Forgot Password?
 Misc   Any questions or Linux discussion that does not fit in any of the other technical areas.

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 07-01-2009   #1 (permalink)
Just Joined!
 
Scallywag's Avatar
 
Join Date: Jul 2009
Location: Black Mountain, NC
Posts: 13
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?
Scallywag is offline  


Reply With Quote
Old 07-02-2009   #2 (permalink)
tpl
Linux User
 
Join Date: Jan 2007
Location: cleveland
Posts: 367
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)
tpl is offline   Reply With Quote
Old 07-02-2009   #3 (permalink)
Just Joined!
 
Scallywag's Avatar
 
Join Date: Jul 2009
Location: Black Mountain, NC
Posts: 13
Thanks

I'll give it a try. I appreciate it....
Scallywag is offline   Reply With Quote
Old 07-03-2009   #4 (permalink)
Just Joined!
 
Scallywag's Avatar
 
Join Date: Jul 2009
Location: Black Mountain, NC
Posts: 13
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.
Scallywag is offline   Reply With Quote
Old 07-03-2009   #5 (permalink)
Linux Engineer
 
Freston's Avatar
 
Join Date: Mar 2007
Location: The Netherlands
Posts: 826
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
Freston is offline   Reply With Quote
Old 07-04-2009   #6 (permalink)
Just Joined!
 
Scallywag's Avatar
 
Join Date: Jul 2009
Location: Black Mountain, NC
Posts: 13
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...
Scallywag is offline   Reply With Quote
Old 07-06-2009   #7 (permalink)
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.
bills is offline   Reply With Quote
Old 07-11-2009   #8 (permalink)
Linux Engineer
 
Lazydog's Avatar
 
Join Date: Jun 2004
Location: The Key Stone State
Posts: 1,169
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
Lazydog is offline   Reply With Quote
Old 07-11-2009   #9 (permalink)
Linux User
 
saivin's Avatar
 
Join Date: Dec 2008
Location: Bengaluru, India
Posts: 292
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
saivin is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 10:22 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2