11.12.2017, 07:27 UhrDeutsch | English
Hallo Gast [ Registrierung | Anmelden ]

Neues Thema eröffnen   Neue Antwort erstellen
Vorheriges Thema anzeigen Druckerfreundliche Version Einloggen, um private Nachrichten zu lesen Nächstes Thema anzeigen
Autor Nachricht
LRC
Titel: Renaming files  BeitragVerfasst am: 26.08.2006, 16:19 Uhr



Anmeldung: 21. Apr 2006
Beiträge: 152
Wohnort: Ice and Snow
I have a prob. I have a large number of files that read something like this
A B C job
A B C bob
A B C ann
A B C cal
A B C dan
..... and I want to drop A B C . Now I do know about rename, but whenI tried it, it coughed at the spaces in between A B C . Is there someway of using rename or another command that can read the new file naming abilities that use spaces, and make modifications that drop or add parts of file names that include spaces in them? I could do it one at a time by gui, but then I could also get carple-tunnel.
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
devil
Titel: Renaming files  BeitragVerfasst am: 26.08.2006, 18:54 Uhr
Team Member
Team Member


Anmeldung: 06. Mai 2005
Beiträge: 3087
Wohnort: berlin
man rename should help.

greetz
devil

_________________
<<We are Xorg - resistance is futile - you will be axximilated>>

Host/Kernel/OS "devilsbox" running[2.6.19-rc1-git5-kanotix-1KANOTIX-2006-01-RC4 ]
CPU Info AMD Athlon 64 3000+ clocked at [ 803.744 MHz ]
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
h2
Titel: RE: Renaming files  BeitragVerfasst am: 26.08.2006, 20:01 Uhr



Anmeldung: 12. Mar 2005
Beiträge: 1005

I second rename, and excellent application - samples and links

_________________
Read more on dist-upgrades using du-fixes-h2.sh script.
New: rdiff-backup script
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
hubi
Titel: RE: Renaming files  BeitragVerfasst am: 26.08.2006, 20:18 Uhr



Anmeldung: 22. Jan 2006
Beiträge: 1296
Wohnort: Budapest
krename is a fine GUI:
# apt-get install krename

hubi

_________________
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
LRC
Titel: RE: Renaming files  BeitragVerfasst am: 26.08.2006, 20:38 Uhr



Anmeldung: 21. Apr 2006
Beiträge: 152
Wohnort: Ice and Snow
h2. All the examples I have seen up to this point deal with suffix changes, which are easy and I can handle. Also if the part of the name I wanted to change was writen A_B_C_, I know how to do that, The example I wanted to change was A<space>B<space>C<space>, and that folks as far as I can see rename cannot handle. Now as for Krename, that will have to wait. ran into a major glich when last nite after upgrading kernel install-fglrx-debian.sh installed fglrx with a dependency prob that requires an apt that has major problems with it. I've got a posting on that elsewhere.
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
h2
Titel: RE: Renaming files  BeitragVerfasst am: 26.08.2006, 22:11 Uhr



Anmeldung: 12. Mar 2005
Beiträge: 1005

for file in *;do file2=$(echo $file | sed 's/A B C //g');mv -T "$file" $file2;done

worked for me on a test case, run it inside the directory, that was a tricky one, the "$file" is required to handle the spaces in this case.

or, to keep it simple:

rename 's/A\ B\ C\ //' *

also worked for me. Note that you have to escape each space character

LRC, while the examples may deal with prefix/suffix changes, this is simply a regular expression search and replace of any file name, so it will work for anything. But thanks for the interesting question, it's always fun working out a new thing I didn't know how to do, personally I never use spaces, but a lot of people do, and this actually would be a great way to replace spaces with something else on large groups of files.

For names where two parts need to be redone, it might require two rename operations, unless you are very good with regular expressions, then one might do it.

I added this example to the page I linked to, plus a few other more complicated and random space replacement scenarios.

rename really is an excellent tool, one of the better ones out there, I'd put it right up there with rdiff-backup for well thought out design. I used to dream about a good built in tool like that in windows, that was a huge pet peeve for me, especially on large websites where I needed to rename hundreds of files. Happily, those days are fast becoming a distant memory, lost in the foggy days of pre linux legacy operating systems.

_________________
Read more on dist-upgrades using du-fixes-h2.sh script.
New: rdiff-backup script
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
LRC
Titel: RE: Renaming files  BeitragVerfasst am: 27.08.2006, 04:40 Uhr



Anmeldung: 21. Apr 2006
Beiträge: 152
Wohnort: Ice and Snow
Amazing h2! Worked like a charm. Using M$ you might have been able to do it with a $100 program. Thanx a bunch. Sehr glücklich
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
Swynndla
Titel: RE: Renaming files  BeitragVerfasst am: 27.08.2006, 07:56 Uhr



Anmeldung: 05. Dez 2005
Beiträge: 414
Wohnort: Auckland, New Zealand
Regular expressions (regex's) are very very powerful, although they are also very geeky, in that they are definately not user friendly.

For example, it may have been safer to use:
rename 's/^A\ B\ C\ //' *
instead of:
rename 's/A\ B\ C\ //' *
... as otherwise a file like "my backup - do not change - A B C bob" will also be changed (but probably didn't matter in your case, and I don't think h2 wanted to confuse you).
... in fact you could have been even safer with:
rename 's/A\ B\ C\ //' A\ B\ C\ *

Now if the files began with various different upper case letters instead of always being A's B's and C's ... eg:
A B C bob
G E F jon
O C V sue
etc
... then changing all the files is slightly more complicated, and would be an absolute nightmare in M$ windows, but it's a breeze for regex's ... all you'd need to do is something like:
rename 's/^[A-Z]\ [A-Z]\ [A-Z]\ //' [A-Z]\ [A-Z]\ [A-Z]\ *

Don't think that the two patters in the above line are the same though, one is a regex, and the other is a glob. This stuff isn't user friendly, and you have to do some geeky things like reading regex documentation.

And of course regex's can keep going, doing more and more complicated things. Regex's are a standard pattern matching technique in all of linux (and so it's not just "rename" that uses them) ... and it's partly because of things like regex's that attacted me to linux/unix.

_________________
Linux is evolution, not intelligent design - Linus Torvalds
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
Swynndla
Titel: RE: Renaming files  BeitragVerfasst am: 27.08.2006, 11:25 Uhr



Anmeldung: 05. Dez 2005
Beiträge: 414
Wohnort: Auckland, New Zealand
Since regex's are more flexible than glob expressions, we can tie down the pattern even more to make sure we rename only what we want, eg:
rename 's/^A B C ([a-z]{3})$/$1/' *
would rename all the files that began with "A B C " and then 3 lower-case letters (and nothing after) to only the 3 lower-case letters.

I've seen kano use similar regex's in some of his scripts, in order to pick out exactly what he wants.

Edit: I forgot that the spaces in rename do not need escaping.

_________________
Linux is evolution, not intelligent design - Linus Torvalds
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
h2
Titel: RE: Renaming files  BeitragVerfasst am: 27.08.2006, 20:47 Uhr



Anmeldung: 12. Mar 2005
Beiträge: 1005

Swynndla pointed out some slight errors in the first solution I posted, which, although it works, was not quite technically correct, I've updated the page I linked to to reflect those corrections.

Regular expressions are definitely one of the more obscure, and arcane, of the computing sciences, lol...

why it would not work without escaping spaces in rename yesterday but today it worked fine is beyond me, unless I just assumed it didnt' work and forgot to test if it did, which I thought I had done... but that's the nature of regular expressions..

_________________
Read more on dist-upgrades using du-fixes-h2.sh script.
New: rdiff-backup script
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
Swynndla
Titel: RE: Renaming files  BeitragVerfasst am: 27.08.2006, 23:37 Uhr



Anmeldung: 05. Dez 2005
Beiträge: 414
Wohnort: Auckland, New Zealand
I'm definately no master at them, I've used them for a couple of years at my work, and I still haven't got them all figured out.

But in saying that, regex's are a good example of what linux and all unix based OS's are all about ... you see, linux places heaps of power in the user's hands. This can be dangerous, yet powerful. Other OS's try to protect the user from themselves. It's a different philosophy. Some people hate it because they end up pulling all their hair out. Others love it because they are free to do powerful things.

This philosophy was also behind the C programming lanuage. Newbies pull their hair out, and experts can't stop grinning ear to ear.

_________________
Linux is evolution, not intelligent design - Linus Torvalds
 
 Benutzer-Profile anzeigen Private Nachricht senden  
Antworten mit Zitat Nach oben
Beiträge vom vorherigen Thema anzeigen:     
Gehe zu:  
Alle Zeiten sind GMT + 1 Stunde
Neues Thema eröffnen   Neue Antwort erstellen
Vorheriges Thema anzeigen Druckerfreundliche Version Einloggen, um private Nachrichten zu lesen Nächstes Thema anzeigen
PNphpBB2 © 2003-2007 
 
Deutsch | English
Logos and trademarks are the property of their respective owners, comments are property of their posters, the rest is © 2004 - 2006 by Jörg Schirottke (Kano).
Consult Impressum and Legal Terms for details. Kanotix is Free Software released under the GNU/GPL license.
This CMS is powered by PostNuke, all themes used at this site are released under the GNU/GPL license. designed and hosted by w3you. Our web server is running on Kanotix64-2006.