LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-19-2008, 10:26 AM   #1
Setya
Member
 
Registered: Apr 2007
Posts: 41

Rep: Reputation: 15
Find files by their contents than move them in 1 command


Hi,

Is it possible to find files by characters in their content and if you find them move them to another directory, all using 1 command as combination of 'find', 'grep' and 'mv' ?

Any help would be greatly appreciated.

Regards,

Setya
 
Old 11-19-2008, 10:50 AM   #2
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Hi,

Yes, it's possible.

Sounds like homework to me.
 
Old 11-19-2008, 11:17 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
A "combination of find, grep, and mv" does not sound like one command to me. You can enter a very long command sequence in interactive mode, but at some point it is easier to write a script.


The way to approach this is to first learn how to search files by content. GREP is the standard starting point, but there are others. My favorite starting point for Bash scripting is the Bash Guide for Beginners, by Machtelt Garrels---free at http://tldp.org

Here's the pseudocode for one way to do this:
for fname in `find <pathname> <keyword>` ; do
grep <keyword> fname
if <returncode> = true; then
mv $fname <dest>
else <whatever makes sense>
fi
done
 
Old 11-19-2008, 11:54 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or
if grep <keyword> $fname
then
mv $fname <dest>
else
<whatever>
fi
to avoid one step.
 
Old 11-20-2008, 09:09 AM   #5
Setya
Member
 
Registered: Apr 2007
Posts: 41

Original Poster
Rep: Reputation: 15
OK. Thanks a lot guys for your responses. I'll try it.

Setya
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot using FTP move command to move files adrianmak Linux - Networking 4 04-21-2009 12:01 PM
typo in move command - files gone now? Eusaphious Linux - General 6 07-23-2008 04:50 AM
Script to find all picture files on a HDD and move them scoops98 Linux - General 8 02-27-2007 02:40 AM
Command to select and move mutiple files from list in text file steve.paris Linux - Newbie 11 11-27-2005 12:44 PM
find and move most recent files in dir backnine_99 Linux - Software 2 06-14-2005 12:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration