views:

266

answers:

2

I have folders and subfolders with 10K photos in them.

Now I need to put all 10K files into one folder. I want to append the folder and subfolder name with the existing file name so that the file name becomes meaningful to me.

I need help

+1  A: 

You can write a quick shell script to do this for you. To get the name of the current folder:

pwd|awk -F'/' '{print $NF}'

and to get the current folder's parent:

pwd|awk -F'/' '{SL = NF-1; print $SL}'

Update

Create a file that looks like this:

#!/bin/bash

for f in `find . -name '*jpg'`
do
   filename=`echo $f|awk -F'/' '{SL = NF-1; TL = NF-2; print $TL "_" $SL  "_" $NF}'`
   cp $f newfolder/$filename
done

and then do chmod +x on it. Run the script in the top level. In this case, you need to make the destination, newfolder, ahead of time.

I took this directory structure:

./bar
./bar/baz
./bar/baz/1.jpg
./bar/baz/2.jpg
./bar/baz/3.jpg
./bar/buz
./bar/buz/1.jpg
./bar/buz/2.jpg
./bar/buz/3.jpg
./foo
./foo/baz
./foo/baz/1.jpg
./foo/baz/2.jpg
./foo/baz/3.jpg
./foo/buz
./foo/buz/1.jpg
./foo/buz/2.jpg
./foo/buz/3.jpg

And made it into this:

./newfolder/bar_baz_1.jpg
./newfolder/bar_baz_2.jpg
./newfolder/bar_baz_3.jpg
./newfolder/bar_buz_1.jpg
./newfolder/bar_buz_2.jpg
./newfolder/bar_buz_3.jpg
./newfolder/foo_baz_1.jpg
./newfolder/foo_baz_2.jpg
./newfolder/foo_baz_3.jpg
./newfolder/foo_buz_1.jpg
./newfolder/foo_buz_2.jpg
./newfolder/foo_buz_3.jpg

Hope that is what you were looking for.

jedberg
Thank you so much for prompt help. Would you elaborate a bit more. should I create batch file for this? or how to execute it?
I updated the answer to include a shell script.
jedberg
A: 

Here's how to do it in one-line of shell.

$ mkdir folder folder/subfolder{0,1}
$ touch folder/subfolder{0,1}/photo{0,1,2}.jpg
$ find folder -type f
folder/subfolder0/photo0.jpg
folder/subfolder0/photo1.jpg
folder/subfolder0/photo2.jpg
folder/subfolder1/photo0.jpg
folder/subfolder1/photo1.jpg
folder/subfolder1/photo2.jpg
$ mkdir newdir
$ find folder -type f -name '*.jpg' -exec bash -c 'ext="${0##*.}"; base="$(basename "$0" ."${ext}")"; dirs="$(dirname "$0" | tr / _)"; new="newdir/${base}_${dirs}.${ext}"; mv "$0" "${new}"' {} \;
$ find newdir -type f
newdir/photo0_folder_subfolder0.jpg
newdir/photo1_folder_subfolder0.jpg
newdir/photo2_folder_subfolder0.jpg
newdir/photo0_folder_subfolder1.jpg
newdir/photo1_folder_subfolder1.jpg
newdir/photo2_folder_subfolder1.jpg
ashawley
Forgot to mention that this actually "appends" as was asked. I assumed, as did jedberg, that you'd want to *prepend* the path to the filename. If prepend is what was wanted, it wouldn't be hard to change.
ashawley