Sunday, July 5, 2009

GNU/Linux: copy , organize and rename all files in directrory with new mask.

On this page, presented simple how-to for GNU/Linux newbie, that can be used for renaming files, for example photos in one directory.

This script will also work for other GNU/Linux's.
Please, be very accurate, to not lost all your files!

I have a set of folders, and each folder, contain some digital photo's.

the structure is like following:

~/
 Pictures/
    |
    +------- Folder1
               +---- Picture1.jpg
               +---- Picture2.jpg
               +---- Picture3.jpg
    +------- Private <--- be="" br="" excluded.="" folder="" have="" this="" to="">    |
    +------- Folder2 
    |
    +------- Folder3


I want to rename photo's and to put them into separate folder named AllPicts.

I also want to have the new name of file being concatenation (sum) of folder name and just some unique figure. For example Folder1/Picture1.jpg can be copied into AllPhotos/Folder1_1.jpg by this schema.

How to:

1) Start terminal. Alt-F2, xterm.

2) In terminal,
Prepare folders,
 
cd ~/Pictures
mkdir AllPhotos

check if you have enough space
du -sk
df


do renaming and organizing:
a=0 
for i in `find ./ -type d | sed "s/^\.\///" | egrep -v "^$" | egrep -v ^Private$`
do cd $i
echo $i
for j in `ls -1 | egrep -i \.*\.jpg`
do cp $j ~/Pictures/AllPhotos/$i$a.jpg
let a=(a+1) 
done 
cd ~/Pictures/
done 


Please be very patient in using and modifying of script.
to avoid lost data or trashed disk.

It is better to understand and check your man pages for meaning of commands in script.

From the other side, if you understand script, than it is probably
easy for you to modify this script for your needs.

No comments: