Hi all.
As someone who is inexperienced in programming, I have attempted to code up a little bash script to manipulate a number of files in a set of directories based on certain criteria/conditions etc.
By way of example, I have directory X, and a set of files in it. I want to rename certain types of files (i.e. those with certain extensions) to the name of the directory they are in - preserving their extensions of course. I also want the script to delete files/subdirectories that do not match these criteria.
One catch is that some of the directories contain two or more of the same file types, (for example, file1.mpg, file2.mpg, file3.mpg). In such a case, I want the files to be renamed directoryX-1.mpg, directoryX-2.mpg etc.
So far, I have come up with the below:
Code:
find -type d | while read d; do cd "$d"; for i in *.mpg; do if [ ! -f "$i" ]; then continue; fi; r="${PWD##*/}.mpg"; mv -f "$i" "$r"; ((c++)); done; cd "$OLDPWD"; done
I repeat this command, substituting .mpg for the other extensions I want to manipulate. This means I can achieve the renaming-to-inherited-directory aspect of things. This is obviously very inelegant, and only gets me halfway there in terms of what I want the script to do.
After much reading, I am thinking of an if condition that does basically 'if there are more than one of files *.mpg, then append integers beginning with 1 to the end of the directory name; else proceed with standard rename'. After this portion of the script completes, I'm thinking that it is then I insert something that 'recursively delete all files not having extension X Y or Z'.
It is on the above two points I am stuck.
Excuse the long post - I have tried to be as clear as possible.
Any help is appreciated.
Cheers,
zorblart