[SOLVED] Add a file extension to a bunch of file names
Linux - NewbieThis 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!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I have a considerable number of files in a subdirectory (some fascinating old military clips from archive.org - search on Big Picture if interested). Anyhow, I am downloading them using Internet Download Manager running in an XP virtual machine in VMWare on my Ubuntu 10.04 PC (due to the queuing, restart and speed capabilities of IDM). But I digress - the files are being saved on the host (Samba share) without a file extension.
So I have a collection of files with names like
The Douglas MacArthur Story
THEY WERE THERE (1960)
I wish to add the extension ".mp4" In Windows this is simply done with the command
rename *. *.mp4
This of course does not work in Linux. I have researched the Linux rename command and reviewed a lot of examples. However, I have not found a way to add an extension to a batch of files which are named with no extension to start with. The spaces in the file names also seem to present an issue.
At the moment I am renaming them from the Windows VM while they are sitting on the Samba share using the ancient File Manager program from Windows NT which works great on XP. I have experimented with the file rename facility in Gnome Commander however, it does not seem to want to do something so simple.
I do not have any .extension files in the directory
ken@taylor12:~/test$ ls -la
drwxr-xr-x 2 ken ken 4096 2010-10-21 09:19 .
drwxr-xr-x 61 ken ken 4096 2010-10-21 09:19 ..
-rwx------ 1 ken ken 118547151 2010-10-20 20:44 7th Infantry Division
-rwx------ 1 ken ken 118775949 2010-10-20 11:36 Above the Best
-rwx------ 1 ken ken 121092948 2010-10-20 16:26 A Day in Korea
-rwx------ 1 ken ken 125235395 2010-10-20 22:04 A Debt is Honored
-rwx------ 1 ken ken 120928821 2010-10-20 21:39 Aid to Nationalist China
-rwx------ 1 ken ken 122576484 2010-10-20 14:02 Airborne to Battle
Thanks grail, that did the trick. As you have put so much effort into answering my question I feel obligated to take the time to understand what your command does.
I understand that -n tells rename to show what would be done but not to do it.
- and that s means to substitute the second expression .mp4 for the first.
- and the * means to match any file - that is to process all flies in the directory
However, the $ as me stumped. I understand that $ in a regular expression matches the (right hand) end of a string. In the example at hand I wish to concatenate .mp4 to the end of the file name, not replace the last character or characters with .mp4.
Well wait a minute. I reread the description of $. Since it matches the position at the end of the string - now that makes sense. The command is replacing the nothingness following the file name with .mp4 - exactly what I wished to do.
These simple things always trip me up - but give me something more complex like moving 80k engineering design documents for a nuclear power plant from one server to another while changing the file names and directory structure to comply with a new document management system convention and at the same time providing traceability of where the old files went and where the new files came from... a piece of cake. (Windows servers but what can I say - that is where the data was).
dump a "dir /s" to a text file
load the text file into a data table in Visual FoxPro
a little programming to generate the new path and file name and populate "new" fields in the table
write a few SQL commands to generate shell scripts to
- create an md5 digest of each original file in its original location and echo to a text file
- create the new directory structure on the receiving server
- copy the files to the new server and rename them on the fly
dumped dir /s on the new server to a text file
loaded that text file into the database
a little more sql to generate a script to generate the md5 digest of all files on the new server
loaded the before and after md5 data into tables, joined them and produced a report showing the old file name and location, old md5, matching new md5, new file name and location
The clients were amazed, I was bored. But let me come across something a simple as the situation we have been discussing and...