LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-09-2009, 10:11 PM   #1
zorblart
LQ Newbie
 
Registered: Jan 2009
Posts: 1

Rep: Reputation: 0
Trouble with a script to manipulate files within a large number of directories


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
 
Old 01-10-2009, 02:11 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,749

Rep: Reputation: 461Reputation: 461Reputation: 461Reputation: 461Reputation: 461
First, spread the code out over several lines instead of trying to make it a one liner! This will make it easier to read and to work on.
Also, when starting out, substitute the real command with echo statements which tell what the real command would do. This will keep you from ruining a bunch of files. Once you have debugged the loops and other complexities, then you can substitue the ecgo staements with the real commands.
 
  


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
Renaming large number of files. checkmate3001 Linux - General 16 06-20-2009 11:16 AM
ext3 performance -- very large number of files, large filesystems, etc. td3201 Linux - Server 5 11-25-2008 09:28 AM
Deleting a large number of files msteudel Linux - General 4 01-26-2005 01:36 AM
Large Number of files? mikeshn Linux - Security 2 01-10-2004 06:11 AM
Number of files and directories ALF Linux - Software 4 06-11-2003 10:34 AM


All times are GMT -5. The time now is 02:34 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration