LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-02-2013, 07:59 PM   #1
shayne391
LQ Newbie
 
Registered: Dec 2013
Posts: 5

Rep: Reputation: Disabled
Shell Script to rename many files


I have a directory full of files and the format of the names vary somewhat, for example:
John Doe - Data.dat John_Doe_-_Info.dat JohnDoe_Account-Nov.dat
Nov_-_information_-_John_Doe.Dat
There are other patterns but loosely follow the ones above. What I need is help writing a shell script "bash or perl" to put all these files in one standard context. This will be my first attempt at anything other than simple one line shell scripts. How do and Where do I begin?
running F20 and KDE 4.11.3 if that makes any difference.
Thanks
 
Old 12-02-2013, 08:09 PM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
You've only stated half of your requirements. What standard format to you want all of the files to eventually end up as? What have you tried so far? What you ask is a relatively simple task in bash but this sounds like homework so I'm less inclined to give you an answer outright.
 
Old 12-02-2013, 08:27 PM   #3
shayne391
LQ Newbie
 
Registered: Dec 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Shell script to rename many files

The final format doesn't so much matter as long as the group is cohesive, that said however, data before user, eg. Data-Nov_John-Doe.dat would be sufficient. There is over 6000 of these files. The only thing I have tried so far is mv *.dat *.dat the star meaning source and destination names. This is the only way I know. Sorry it sounds like homework to you but to me it is a masters thesis on a topic i did not study.
Thanks
 
Old 12-02-2013, 08:58 PM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Just to be clear: this is more of a hobbyist forum where like minded individuals come together and discuss various topics. It's not really a "script farm" (best term I could come up with) for generating scripts upon request. In any case, I can point you in the right direction for researching how this could be accomplished. One thing to keep in mind is among the plethora of available tools in Linux there are hundreds of ways this can be accomplished.

First I'll reference sources recommended for general reading and then more to the point.
The feature you referred to as *.dat is known as name globbing. This will get you nowhere with just the mv command alone. What you need to do is iterate through all of the file names using a loop (learn about loops in the bash docs see compound commands). I would likely use sed to perform a substitution from the coreutils package which would involve using it likely with an echo command with command substitution (see command substitution under shell expansions in the bash manual). With sed I'd be using a regex to match common patterns. Then once I have the source file name and destination file name (likely each stored in a variable) I would then perform the mv command. This process would happen through each iteration of the loop. Be aware that this approach has a few non-obvious pitfalls such as not handling leading or trailing spaces.

This is a start for you.
 
Old 12-02-2013, 09:15 PM   #5
shayne391
LQ Newbie
 
Registered: Dec 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
shell script to rename multiple files

Thanks, as I am new to Linux. A place to start is all I ask. I never want a hand out only a hand up and you have given it to me. Now that I know where to start I am no longer overwhelmed.
 
Old 12-02-2013, 09:32 PM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by shayne391 View Post
Thanks, as I am new to Linux. A place to start is all I ask. I never want a hand out only a hand up and you have given it to me. Now that I know where to start I am no longer overwhelmed.
I understand which is why I worked so hard on crafting an answer for you. If I thought all you wanted was a simple script I wouldn't have bothered. I just wanted to convey the general attitude of this community because the framing of your question looked a lot like elementary "introduction to Linux" course work college students generally sludge through. Often, students come here expecting to be handed answers without effort on their own part and then label the community useless when their expectations are not met.

I'm not categorizing you with that. I'm just stating where my original mentality stemmed.

Also, the find utility is occasionally used to solve a problem like this as well. Both name globbing in a for loop and the find utility generally work.

e.g.
Code:
for x in *.dat;do
  echo "${x}"
done
find . -type f -name '*.dat' | while read line;do
  echo "${line}"
done
Definitely use the man pages. Almost every utility ever comes with a man page. There's even a man page for the man command.

Code:
man man
Learn the navigation shortcuts because man pages (such as man bash) can be very large.

Unrelated but also a valuable resource: You should also check out IRC. That's just another place to get help on any subject involving Linux or programming. See freenode.net for that. There's a #bash channel on freenode.

Last edited by sag47; 12-02-2013 at 09:45 PM.
 
Old 12-02-2013, 09:41 PM   #7
shayne391
LQ Newbie
 
Registered: Dec 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks. I will spend the night seeing what damage I can accomplish. Much Respect.
 
Old 12-02-2013, 09:44 PM   #8
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Once you make progress on a script feel free to post it in this thread. I can help you work out the details and improve it. You'll generally find this community is most helpful when you are able to provide a small sample of what you're trying to accomplish. Members usually jump at the opportunity to collaborate in this regard.
 
  


Reply



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
[SOLVED] Need help to rename file through shell script prasad.bh Linux - Server 4 02-06-2013 06:16 AM
[SOLVED] Shell Script to rename and move files to a different folder in Windows Simon1984 Linux - Newbie 10 05-18-2012 10:50 AM
"How to rename files with current date stamp appended into name using shell script?" nandi Linux - Newbie 4 10-05-2011 10:36 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:50 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration