LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Add a file extension to a bunch of file names (https://www.linuxquestions.org/questions/linux-newbie-8/add-a-file-extension-to-a-bunch-of-file-names-839353/)

taylorkh 10-20-2010 02:01 PM

Add a file extension to a bunch of file names
 
This should not be this hard :(

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
Quote:

The Douglas MacArthur Story
THEY WERE THERE (1960)
I wish to add the extension ".mp4" In Windows this is simply done with the command
Quote:

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.

Any suggestions?

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.

TIA,

Ken

Tinkster 10-20-2010 02:09 PM

Not knowing whether there's any pattern to your file names
the safest bet is a loop.
Code:

for i in *; do mv "$i" "$i".mp4;done


Cheers,
Tink

taylorkh 10-20-2010 02:28 PM

Thanks Tink - that does the trick!

Ken

grail 10-20-2010 07:55 PM

Or if using your rename in ubuntu:
Code:

rename -n 's/\..*/.mp4/' *
Remove '-n' to make it happen.

taylorkh 10-20-2010 08:52 PM

Thanks grail but that does not seem to work - I did remove the -n.

Ken

grail 10-20-2010 09:37 PM

Did it show output with -n still in?

taylorkh 10-21-2010 05:59 AM

No output with -n

grail 10-21-2010 08:14 AM

So last sill question, you are running it in a directory with files that have dot and an extension?

taylorkh 10-21-2010 08:23 AM

I do not have any .extension files in the directory
Quote:

ken@taylor12:~/test$ ls -la
total 710864
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
ken@taylor12:~/test$
When I execute the command as recommended
Quote:

ken@taylor12:~/test$ rename -n 's/\..*/.mp4/' *
ken@taylor12:~/test$
Ken

grail 10-21-2010 11:59 AM

Quote:

I do not have any .extension files in the directory
This is the problem. And hence my mistake as I thought we were replacing extensions, but now that I have re-read your question properly, the following should work:
Code:

rename -n 's/$/.mp4/' *
Sorry for the confusion

taylorkh 10-21-2010 01:57 PM

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...

Thanks again,

Ken


All times are GMT -5. The time now is 03:32 AM.