LinuxQuestions.org
Help answer threads with 0 replies.
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 03-31-2012, 11:51 AM   #1
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Rep: Reputation: 0
Pipe GREP to copy filenames based on keywords


Hi everybody,

A while ago i posted that i lost everything due to a malicious script. I however succeeded in recovering almost every bit of data - most importantly my photo's. Thanks to photorec and testdisk.

Now comes the next problem. Sorting all my pictures.

I gave my photo's keywords. So when i look at the image properties, i see:
Code:
Image Type: jpeg (The JPEG image format)
Camera Brand: Canon
Date Taken: 2010:08:15 14:57:51
Keywords: Gambia
Etc
1st problem
Now i'm trying to copy all my files with the keywords Gambia to another directory. I can find the images with the following command, but i can't append a copy command to it.
Code:
find . -type f | xargs grep -lri "gambia"
2nd problem - SOLVED
Other things i want to know is how i can delete all jpg's < 500kb (junk).

3rd problem
I wrote some scripts, but i can seem to find them (too many data recovered). I rememberd some of the code: "changing IFS to newline means spaces", and tried:
Code:
mainstream@dopamine-pc:~/Desktop/Recovery 500GB$ find . -type f | xargs grep -lri "changing IFS to newline means spaces"
But no luck.

I get the following output after a few hours.
Code:
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
I tried to append the -0, but that gives me an error message (xargs: argument line too long).

Thanks in regards

Last edited by mainstream; 04-04-2012 at 09:07 AM.
 
Old 03-31-2012, 12:11 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Try using the "file" command to identify file types.

For grep, you can use the -R option to enter directories and grep each file. You don't need large. However you could use the find command to locate small files. That way you wont be searching the contents of large opaque files.


For problem #1, pipe the file name output to xargs, | xargs cp -t /test/dir
Use the -L option to limit the number of arguments.


Problem 2: find /dir -type f -name "*.jpg" -size -500k -delete
 
Old 04-01-2012, 06:01 AM   #3
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
Thanks, i'll try ;-)

1st problem
Something like this:
Quote:
find . -type f | xargs cp -t Gambia/ | grep -lri "gambia"

Last edited by mainstream; 04-01-2012 at 06:09 AM.
 
Old 04-01-2012, 12:13 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I personally would write up a separate script for processing the files, then execute the script on each file using find.
 
Old 04-02-2012, 12:00 PM   #5
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
You mean to first use the find command and list all files containing "Gambia" (grep) and copy these files? Isn't that a bit off a cumbersome manner?

Code:
#!/bin/bash

for file in `find . -type f | xargs grep -lri "gambia"` ; do
mv -v $file backup/


done
This is what i tried. But it just keeps hanging when executing - nothing happens.

Last edited by mainstream; 04-02-2012 at 05:52 PM.
 
Old 04-04-2012, 07:53 AM   #6
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
No one who can help me using grep and cp or mv?
 
Old 04-05-2012, 12:51 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Looking more closely now, from what I see, the commands you've been using so far try to run grep directly on the binary photo files. I don't think that's going to work very well, if at all. You really need to be running them through a program that extracts the metadata, and parse that. I personally recommend the perl-based exiftool.

As I suggested before, what you need to do is wrap up up your copy commands into a separate script, one that will parse the data of a single file at a time and move it appropriately. Just to throw out a quick proof-of-concept:

Code:
#!/bin/bash

metatags=$( exiftool "$1" )

case $metatags in

	*Gambia*) mv -vn "$1" /gambiadir	;;
	*Kenya* ) mv -vn "$1" /kenyadir		;;
	*)	  echo "no matching keyword found in $1" ;;

esac

exit 0
Then just make the script executable and run it in find.


Code:
find . -type f -exec /path/to/sortscript '{}' \;

You could expand the script to incorporate the find command directly, but this should do for a start.

Last edited by David the H.; 04-05-2012 at 12:54 PM. Reason: small word change
 
  


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
How to Recover Deleted Files when rm command was used (I recovered using ext3grep) rtoney5 Linux - Software 8 01-14-2013 06:19 AM
Can data be recovered? md0 bad superblock? jlg3 Linux - Software 7 06-19-2007 06:48 AM
Installed linux linux on wrong device. Data needs to be recovered. shookie Linux - General 1 10-03-2006 05:47 AM
Mounting drive with recovered data SunOS 4.1 meb1008 Solaris / OpenSolaris 4 09-18-2006 10:14 AM
Corrupt/missing data fat32 need recovered cormack Linux - General 3 04-12-2006 06:18 PM


All times are GMT -5. The time now is 07:38 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