Help - Script to move files to subdir depending on file extension.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help - Script to move files to subdir depending on file extension.
# 1  
Old 05-12-2011
Help - Script to move files to subdir depending on file extension.

Hi,

First off I'm pretty new to scripting so please be gentle.

I am looking for some help with a script that will move all files with a certain extension into a folder within their current location.

Just for clarity I have all my photos organised in directories such as:

/photos/Birthdayparty2009
/photos/Birthdayparty2010
/photos/Christmas2009/Dir1
/photos/Christmas2009/Dir2
/photos/Christmas2010

In each folder I have a jpg version of the photo and a nef (RAW) version.

This can be a problem viewing the photos in slide shows as you get each photo twice and as the RAW files are large it can be quite slow.

So the solution would be the above. Organising the RAW files into a RAW directory for each directory of photos.

The complication is that, as you can see above, there are some dirs that have several sub dirs so it needs to do it recursively.

Anyway down to business:

If been trying to use find / -execdir to do the above but as it is recursive is seems to move the RAW files in the dir into a the RAW dir but then go into the RAW dir and mv the ones its just moved there to a RAW file.

So if I run -

find ./ -name *.nef -execdir mv {} ./RAW \;


on the below dir structure:

.
|-- dir1
| |-- dir3
| | |-- photo1.jpg
| | |-- photo1.nef
| | |-- photo2.jpg
| | |-- photo2.nef
| | |-- photo3.jpg
| | |-- photo3.nef
| | `-- RAW
| |-- photo1.jpg
| |-- photo1.nef
| |-- photo2.jpg
| |-- photo2.nef
| |-- photo3.jpg
| |-- photo3.nef
| `-- RAW
`-- dir2
|-- photo1.jpg
|-- photo1.nef
|-- photo2.jpg
|-- photo2.nef
|-- photo3.jpg
|-- photo3.nef
`-- RAW



I end up with:

.
|-- dir1
| |-- dir3
| | |-- photo1.jpg
| | |-- photo2.jpg
| | |-- photo3.jpg
| | `-- RAW
| | |-- photo1.nef
| | `-- RAW
| |-- photo1.jpg
| |-- photo2.jpg
| |-- photo3.jpg
| `-- RAW
| |-- photo1.nef
| `-- RAW
`-- dir2
|-- photo1.jpg
|-- photo2.jpg
|-- photo3.jpg
`-- RAW
|-- photo1.nef
`-- RAW



I hope that makes sense.

Any help would be appreciated as I've spent hours trying to figure this out.
# 2  
Old 05-12-2011
Code:
du -a /photos_directory|grep ^// |grep -v "RAW"|while read file
do
dir=`dirname $file`
f=`basename $file`
mkdir -p $dir/RAW
mv $file $dir/RAW/$f
done

Try this code with some dummy directory with few sample files.
# 3  
Old 05-13-2011
The "du -a" part gives me -

0 ./dir2/photo3.jpg
0 ./dir2/photo3.nef
0 ./dir2/photo2.jpg
0 ./dir2/photo1.jpg
0 ./dir2/photo2.nef
4 ./dir2/RAW
0 ./dir2/photo1.nef
8 ./dir2
0 ./dir1/photo3.jpg
0 ./dir1/photo3.nef
0 ./dir1/dir3/photo3.jpg
0 ./dir1/dir3/photo3.nef
0 ./dir1/dir3/photo2.jpg
0 ./dir1/dir3/photo1.jpg
0 ./dir1/dir3/photo2.nef
4 ./dir1/dir3/RAW
0 ./dir1/dir3/photo1.nef
8 ./dir1/dir3
0 ./dir1/photo2.jpg
0 ./dir1/photo1.jpg
0 ./dir1/photo2.nef
4 ./dir1/RAW
0 ./dir1/photo1.nef
16 ./dir1
36 .

So the "grep ^//" returns nothing. Am I missing something from the du command that will return the listing in a different format or is there a way of stripping out the part of the line I don't need.

I've played with sed to strip out the first 2 characters but its not always 2 characters so doesn't work.
# 4  
Old 05-13-2011
Can you try this.

Code:
find /photos/dir -type f|while read file do 
dir=`dirname $file`
f=`basename $file`
if [[ $f =~ $nef ]]
then
    mkdir -p $dir/RAW
    mv $file $dir/RAW/$f
fi
done


Last edited by kumaran_5555; 05-13-2011 at 07:23 AM..
# 5  
Old 05-13-2011
Sorted!

The ls didn't quite return the required output either so I went back to find.

A few other small adjustments to your script and its testing fine. I'm just doing a quick backup before I run it on the actual data though.

What I've got now for ref:

#!/bin/sh

find . -type f -print|grep "nef"|while read file
do
dir=`dirname $file`
f=`basename $file`
mkdir -p $dir/RAW
mv $file $dir/RAW/$f
done



Thanks for your help kumaran. Appreciated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to move .300 extension file from 1 dir to other and email

Hi, I am looking for a shell script which move .300 extension files from /home/sofia/ to /home/sofia/test and sends me an email with the list of files moved like this :- The following files has been moved from /home/sofia/ to /home/sofia/test 1. alpha.300 2. beta.300 Email only if the... (4 Replies)
Discussion started by: sofia8679
4 Replies

2. Shell Programming and Scripting

Move all files from dir and subdir to Archive with File name as complete path_filename

HI, I need to move all files from a dir & its all subdir to Archive folder which is indise dir only. and moved filename should changed to complete path ( Like Dir_subdir_subdir2_.._filename ). also all files names shoud capture in a file in order to mail I written below code ... (11 Replies)
Discussion started by: minijain7
11 Replies

3. Shell Programming and Scripting

move set of files to the target path with different extension

I have the following files in the dir /home/krishna/datatemp abc.xml cde.xml asfd.txt asdf_20120101-1.xml asdf_20120101-2.xml asdf_20120101-3.xml asdf_20120101-4.xml Now I need to move the files having the pattern asdf_20120101-*.xml to the dir /home/krishna/dataout with the extn as... (1 Reply)
Discussion started by: kmanivan82
1 Replies

4. Shell Programming and Scripting

Help related to Script to move files depending on config values

Hi All, I am new to Unix scripting, I have requirement where I need to read the key value pair from config file. Sample Config file: Key(File Pattern) Value(File Directory location) test /Users/Bkumar/Downloads/testdir prod ... (1 Reply)
Discussion started by: sbpkumar7
1 Replies

5. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

6. Shell Programming and Scripting

Copy files from subdir

Hey, How can I copy files from subdirectories without copy the subdir and copy it to a higher dir For example: /home/test/subdir/file1 copy file1 to /home or another dir thanx (11 Replies)
Discussion started by: Eclecticaa
11 Replies

7. Shell Programming and Scripting

write to multiple files depending on file type (solved)

I want a script that will add comments to a file before check-in files. comments depend on type of files. i have a script to list files in the directory that will be checked-in There are xml, js, html, vm files. vm will use comments like c/c++ ( // or /*..*/) can you help me add a comment line... (0 Replies)
Discussion started by: pradeepmacha
0 Replies

8. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

9. Shell Programming and Scripting

move files and retain subdir structure

hi: I have some files like this folder1/recording1.mp3 folder1/docs/budget.doc folder2/others/misc.mp3 folder3/others/notes.doc all this folders and files are under the mp3 folder. I would like to move just the mp3s to another folder but retain the subdir structure i have. So if... (4 Replies)
Discussion started by: jason7
4 Replies

10. Shell Programming and Scripting

Split file into multiple files depending upon first 4 digits

Hi All, I have a file like below: 1016D"ddd","343","1299" 1016D"ddd","3564","1299" 1016D"ddd","3297","1393" 1016D"ddd","32989","1527" 1016D"ddd","346498","1652" 2312D"ddd","3269","1652" 2312D"ddd","328","1652" 2312D"ddd","2224","2100" 3444D"ddd","252","2100" 3444D"ddd","2619","2100"... (4 Replies)
Discussion started by: deepakgang
4 Replies
Login or Register to Ask a Question