LinuxQuestions.org
Help answer threads with 0 replies.
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 10-20-2010, 03:01 PM   #1
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,637

Rep: Reputation: 112Reputation: 112
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
 
Old 10-20-2010, 03:09 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
1 members found this post helpful.
Old 10-20-2010, 03:28 PM   #3
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,637

Original Poster
Rep: Reputation: 112Reputation: 112
Thanks Tink - that does the trick!

Ken
 
Old 10-20-2010, 08:55 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Or if using your rename in ubuntu:
Code:
rename -n 's/\..*/.mp4/' *
Remove '-n' to make it happen.
 
Old 10-20-2010, 09:52 PM   #5
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,637

Original Poster
Rep: Reputation: 112Reputation: 112
Thanks grail but that does not seem to work - I did remove the -n.

Ken
 
Old 10-20-2010, 10:37 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Did it show output with -n still in?
 
Old 10-21-2010, 06:59 AM   #7
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,637

Original Poster
Rep: Reputation: 112Reputation: 112
No output with -n
 
Old 10-21-2010, 09:14 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
So last sill question, you are running it in a directory with files that have dot and an extension?
 
Old 10-21-2010, 09:23 AM   #9
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,637

Original Poster
Rep: Reputation: 112Reputation: 112
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
 
Old 10-21-2010, 12:59 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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
 
Old 10-21-2010, 02:57 PM   #11
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,637

Original Poster
Rep: Reputation: 112Reputation: 112
Thumbs up

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
 
  


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
Using key to match against source.txt file to add xml tags to names in Perl ginny2010 Programming 3 06-22-2010 02:16 PM
Linux file extension vs Dos file Extension? manaa Linux - Newbie 6 02-12-2009 05:19 PM
bash: renaming file extension based on actual file type alekone Linux - General 9 12-28-2008 10:12 AM
Add text to the beginning of file names xebix Linux - Newbie 5 10-02-2008 01:43 AM
a script that go read the users names in a file and automaticly add then with a commo tumemanques211 Programming 2 03-22-2002 03:13 PM

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

All times are GMT -5. The time now is 08:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration