LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-02-2012, 01:05 AM   #1
OutsiderFilms
LQ Newbie
 
Registered: May 2012
Posts: 10

Rep: Reputation: Disabled
Script to compare filenames in a directory, sort and delete


Hello All,

I shoot RAW and JPEG images on my camera. These are then copied to a folder on the computer.
The photo folder, as a result, contains image pairs -- file1.cr2, file1.jpg... file2.cr2, file2.jpg and so on.

Since jpg files load faster, I view and then delete the unwanted jpgs from the folder... So now I'm left with some file pairs and some isolated *.cr2 files.

Some folders have 2000+ files in them.

Is there a script/programme to find *.cr2 files that do NOT have a corresponding *.jpg file and delete them?

Thank you, in advance, for your help.

Amit
 
Old 05-02-2012, 01:13 AM   #2
lazardo
Member
 
Registered: Feb 2010
Location: SF Bay Area
Posts: 181

Rep: Reputation: Disabled
Note: Remove the 'echo' command in front of 'rm' to actually delete the files.

Cheers,

Code:
#!/bin/bash

find . -name \*.cr2 |\
while read CR2FILE; do
   JPGFILE=$( echo $CR2FILE | sed 's/cr2$/jpg/' )
   [ -e $JPGFILE ] && continue

   echo rm -v $CR2FILE
done

Last edited by lazardo; 05-02-2012 at 01:21 AM.
 
Old 05-02-2012, 01:31 AM   #3
OutsiderFilms
LQ Newbie
 
Registered: May 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks for the quick response, Lazardo.

Here's what I did (Probably something I did was incorrect):

I copied your script
In a terminal, I went to the folder of photos
ran /bin/bash
then, pasted (shift-control-v) the script into the terminal
pressed enter.

Nothing happened. I was expecting the name (or names) of the cr2 files to be 'echoed' back.

What am I doing wrong?

Perhaps you could modify the script to rename all isolated files by prefixing a "1" to the filename -- that would cause them to relocate to the top of the folder if I sort by name. I could then easily select them and delete them...

Thanks for you help. I really appreciate this.

Cheers,

Amit
 
Old 05-02-2012, 01:45 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
You can simplify the line with sed.
Code:
JPGFILE=${CR2FILE%cr2}jpg
Run the find command on its own and see if it finds the .cr2 files. You need to be in the directory with the .cr2 files or a parent.

You could use the base directory of your photos directory in the find command, e.g.:
find /media/photos/ -name "*.cr2"

Also, the case of letters matters in Linux. If the extensions are .CR2, then change it in the script.

Last edited by jschiwal; 05-02-2012 at 01:46 AM.
 
Old 05-02-2012, 02:00 AM   #5
OutsiderFilms
LQ Newbie
 
Registered: May 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi jschiwal,

Thanks for the input -- I changed references in the script to CR2 and JPG
instead of cr2 and jpg. Now, it seems to be parsing the files...

Here's the output now:

------

amit@amit-desktop:/media/FA1000/5D/29 April/D1$ /bin/bash
amit@amit-desktop:/media/FA1000/5D/29 April/D1$
amit@amit-desktop:/media/FA1000/5D/29 April/D1$ find . -name \*.CR2 |\
> while read CR2FILE; do
> JPGFILE=$( echo $CR2FILE | sed 's/CR2$/JPG/' )
> [ -e $JPGFILE ] && continue
>
> echo rm -v $CR2FILE
> done
bash: [: too many arguments
rm -v ./WIEGO (GC - IMG) (2012.04.29) (10016).CR2
bash: [: too many arguments
rm -v ./WIEGO (GC - IMG) (2012.04.29) (10017).CR2
bash: [: too many arguments

-------- there's many more "echoes" with the same error.


Also, the file *(10017).CR2 does have a JPG pair... I'm testing the script on a backup drive that has ALL pairs except 10016.CR2 (I've deleted just one JPG file in this directory)


Cheers,

Amit
 
Old 05-02-2012, 02:32 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Enclose the variables inside double quotes. "${JPEGFILE}" and "${SC2FILE}". The spaces in filenames cause them to be split into separate entries. The parenthesis launch sub shells or create arrays. Dumb filenames will make working in the terminal and writing scripts more difficult.

http://transnum.blogspot.com/2008/11...orts-0-as.html
This blog has an example using read -d $'\0' and -print0 in find to use null characters as separators.

Note, don't quote variables to the left of the equals sign, but to the right, where they are being referenced.
 
Old 05-02-2012, 02:56 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Also, you'll find it much easier if you put the code into a bash file and then run that file.
 
Old 05-02-2012, 03:25 AM   #8
OutsiderFilms
LQ Newbie
 
Registered: May 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hello jschiwal,

It works now! Thank you.

I did not know that parentheses caused these problems. Their usage as separators seemed natural to me.
I should have provided an example filename with my first post. Sorry about that.

For future reference, here's the code that worked:

--------------------------------------------

/bin/bash

find . -name \*.CR2 |\
while read CR2FILE; do
JPGFILE=$( echo "$CR2FILE" | sed 's/CR2$/JPG/' )
[ -e "$JPGFILE" ] && continue

echo rm -v "$CR2FILE"
done


--------------------------------------------

Am marking the post "Solved". Thank you all for your help.

Cheers,

Amit
 
Old 05-02-2012, 07:46 AM   #9
abhinav4
Member
 
Registered: Jun 2010
Location: India
Distribution: Fedora/Cent OS
Posts: 123

Rep: Reputation: 0
Quote:
Originally Posted by OutsiderFilms View Post
[ -e "$JPGFILE" ] && continue
Can someone tell what it does [ -e "$JPGFILE" ]
 
Old 05-02-2012, 08:38 AM   #10
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Rep: Reputation: 11
It checks if the file mentioned in the $JPGFILE variable exists.
 
Old 05-03-2012, 01:20 AM   #11
lazardo
Member
 
Registered: Feb 2010
Location: SF Bay Area
Posts: 181

Rep: Reputation: Disabled
Quote:
Originally Posted by OutsiderFilms View Post
Hi jschiwal,

Thanks for the input -- I changed references in the script to CR2 and JPG
instead of cr2 and jpg. Now, it seems to be parsing the files...
...
Lesson 1: Be accurate. 'cr2' is not equal to 'CR2', file and directory names are case-sensitive.

Lesson 2: Filenames and directories can have characters that are interpreted with special meanings by the shell. The errors were because spaces and parens were being interpreted outside the context of a filename.

Sorry about that, I don't use special characters in filenames

Cheers,

Last edited by lazardo; 05-03-2012 at 01:22 AM.
 
  


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 sort and delete directory going back for years? ladhibi SUSE / openSUSE 19 11-18-2010 09:27 AM
[SOLVED] Delete files with complex filenames with a script NickJH Linux - Newbie 24 05-26-2010 09:55 AM
script to change multiple filenames in a directory jeffreybluml Linux - Newbie 8 12-06-2006 02:46 AM
Shell script: Delete filenames containing a substring? Drack Linux - General 4 02-12-2006 04:11 PM
Help with a Directory Compare Script bullfrog Linux - General 1 02-04-2003 09:05 AM


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