LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-26-2009, 09:00 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
Need to add an extension to all files in a directory


Hi all,

I have a directory with around 5,000 files that do not have an extension (or any regularity that I could take advantage of using rename). I want to append the extension ".ab1" to the name of each of these files. Can anyone help me out?

Example filenames:
Pl_xlvO_36A01
Pl_xlvO_35H12
Pl_xlvO_35H07

Thanks!
Kevin
 
Old 09-26-2009, 09:10 PM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by kmkocot View Post
Hi all,

I have a directory with around 5,000 files that do not have an extension (or any regularity that I could take advantage of using rename). I want to append the extension ".ab1" to the name of each of these files. Can anyone help me out?

Example filenames:
Pl_xlvO_36A01
Pl_xlvO_35H12
Pl_xlvO_35H07

Thanks!
Kevin
Do it this way:

Code:
path="/path/of/interest"

find $path -type f | grep -iPv "\.ab1$" | while read filepath
do
   mv $filepath $filepath.ab1
done
The trick in a situation like this is to avoid renaming already-renamed files. That's what the grep filter does -- it eliminates files that are already renamed.

WARNING: Test this script on a copied sample of the files in a separate, disposable directory before applying it to the original files. I say this because, for reasons that should be obvious, I haven't tested it.
 
Old 09-26-2009, 09:57 PM   #3
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
Perfect. That did it! Thanks!

I think I understand everything except the function of the $ after .ab1 in the grep command. To try to figure that out I tried it again without the $ but it still worked.
 
Old 09-27-2009, 11:19 AM   #4
verdeboy2k
Member
 
Registered: Jan 2004
Location: /dev/random
Distribution: Gentoo amd64, CrunchBang amd64
Posts: 350

Rep: Reputation: 32
The '$' matches 'End of Line' in a regular expression, just as '^" matches the beginning.
 
  


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
command tofind files by extension through a directory tree, and copy them all to the budword Linux - Newbie 2 06-27-2009 01:48 PM
OpenCV compilation problem (add directory for including header files) maikki Programming 2 01-07-2009 09:07 AM
Bash script: symbolic linking all files with same extension in a directory Katachi Programming 1 12-25-2006 10:42 AM
Need script to add extension stefaandk Linux - General 1 10-05-2005 11:07 PM
symlink multiple files to new directory with new extension jmanjohn61 Linux - General 1 01-06-2005 01:55 PM


All times are GMT -5. The time now is 12:30 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration