LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 11: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, 11:50 AM   #2
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,549
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
Hi,

Yes, it's possible.

Sounds like homework to me.
 
Old 11-19-2008, 12:17 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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, 12:54 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Or
if grep <keyword> $fname
then
mv $fname <dest>
else
<whatever>
fi
to avoid one step.
 
Old 11-20-2008, 10: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 01:01 PM
typo in move command - files gone now? Eusaphious Linux - General 6 07-23-2008 05:50 AM
Script to find all picture files on a HDD and move them scoops98 Linux - General 8 02-27-2007 03:40 AM
Command to select and move mutiple files from list in text file steve.paris Linux - Newbie 11 11-27-2005 01:44 PM
find and move most recent files in dir backnine_99 Linux - Software 2 06-14-2005 01:03 PM


All times are GMT -5. The time now is 10:49 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration