Hi, i have to rename hunderts of files: 792041_100029699.mi 792090_100029501.mi . . . to 792041.mi 792090.mi . . . i tried: rename ??????_*.mi ??????.mi *.mi rename: ??????_.mi: kein Zugriff: Datei oder Verzeichnis nicht gefunden as you see, no sucess. (first file is gone, all others stay as they are) is there a easy way to do this (without writing a script)? simoN -- www.becherer.de ----------------------------------------------- - Das ist die vorlaeufig endgueltige Version! - Herbert C. Maier Dipl.-Ing. (FH) -----------------------------------------------
Hi Simon, you could try "mmv": https://www.systutorials.com/docs/linux/man/1-mmv/ Cheers, Bernd Am 16.01.2023 09:07 schrieb Simon Becherer:
Hi,
i have to rename hunderts of files: 792041_100029699.mi 792090_100029501.mi . . . to 792041.mi 792090.mi . . .
i tried: rename ??????_*.mi ??????.mi *.mi
rename: ??????_.mi: kein Zugriff: Datei oder Verzeichnis nicht gefunden as you see, no sucess.
(first file is gone, all others stay as they are)
is there a easy way to do this (without writing a script)?
simoN
On Mon, Jan 16, 2023 at 11:07 AM Simon Becherer <simon@becherer.de> wrote:
Hi,
i have to rename hunderts of files: 792041_100029699.mi 792090_100029501.mi . . . to 792041.mi 792090.mi . . .
i tried: rename ??????_*.mi ??????.mi *.mi
There are at least two commands "rename", one from util-linux and another Perl based rename, and this invocation is wrong for both of them.
rename: ??????_.mi: kein Zugriff: Datei oder Verzeichnis nicht gefunden as you see, no sucess.
(first file is gone, all others stay as they are)
And what did you expect? The pattern is expanded by shell before your command even sees it, and it ends by moving the first file onto the second file and skipping all the rest. Because your command becomes rename 792041_100029699.mi 792090_100029501.mi ... ??????.mi 792041_100029699.mi 792090_100029501.mi ... and the first two arguments are source and target replacement, so the name 792041_100029699.mi is replaced by 792090_100029501.mi.
is there a easy way to do this (without writing a script)?
And without reading and understanding documentation? I doubt it. It is not possible with the util-linux version which does pure string substitution. It is possible with the Perl version but you need to understand Perl regular expressions at which point you can just as well write the script. Actually, the internet is full of the same question and detailed examples of how to do it. If you are banned by Google https://stackoverflow.com/questions/6840332/rename-multiple-files-by-replaci...
Am 16.01.23 um 11:12 schrieb Andrei Borzenkov:
On Mon, Jan 16, 2023 at 11:07 AM Simon Becherer <simon@becherer.de> wrote:
Hi,
i have to rename hunderts of files: 792041_100029699.mi 792090_100029501.mi . . . to 792041.mi 792090.mi . . .
i tried: rename ??????_*.mi ??????.mi *.mi
There are at least two commands "rename", one from util-linux and another Perl based rename, and this invocation is wrong for both of them.
rename: ??????_.mi: kein Zugriff: Datei oder Verzeichnis nicht gefunden as you see, no sucess.
(first file is gone, all others stay as they are)
And what did you expect? The pattern is expanded by shell before your command even sees it, and it ends by moving the first file onto the second file and skipping all the rest. Because your command becomes
rename 792041_100029699.mi 792090_100029501.mi ... ??????.mi 792041_100029699.mi 792090_100029501.mi ...
and the first two arguments are source and target replacement, so the name 792041_100029699.mi is replaced by 792090_100029501.mi.
is there a easy way to do this (without writing a script)?
And without reading and understanding documentation? I doubt it.
It is not possible with the util-linux version which does pure string substitution. It is possible with the Perl version but you need to understand Perl regular expressions at which point you can just as well write the script. Actually, the internet is full of the same question and detailed examples of how to do it. If you are banned by Google
https://stackoverflow.com/questions/6840332/rename-multiple-files-by-replaci...
Hi andrei, well i was thinking about something similar to the dos "rename" command: ren *.mi ??????.mi should (if i remember correct) work in the dos days. and i am sorry, i have never understood the deeper internals of patterns/expressions how they get expanded in shell. i have read, but as more i read as more unclear it get to me. and i need this only a couple of times a year. so i forget the most when i need next time. for me its a lot of googeling and try and error if my scripts need something of this. so i was asking here. thanks for your online "script" simoN -- www.becherer.de ----------------------------------------------- - Das ist die vorlaeufig endgueltige Version! - Herbert C. Maier Dipl.-Ing. (FH) -----------------------------------------------
On 2023-01-16 19:19, Simon Becherer wrote:
Am 16.01.23 um 11:12 schrieb Andrei Borzenkov:
On Mon, Jan 16, 2023 at 11:07 AM Simon Becherer <> wrote:
Hi andrei,
well i was thinking about something similar to the dos "rename" command: ren *.mi ??????.mi should (if i remember correct) work in the dos days.
and i am sorry, i have never understood the deeper internals of patterns/expressions how they get expanded in shell. i have read, but as more i read as more unclear it get to me.
Then you should simply remember that msdos and unix/linux do this very differently. In msdos the pattern is given to the program you call, and this program interprets the pattern as it likes. In Linux, the pattern is interpreted by the shell before it calls the program, and then it calls the program, with a huge command line with all the patterns expanded (it may be too big to fit, there was a 65K limit). If you have difficulties with this (I still have), then use a gui. Use krename, seriously. I do. It is as safe as can be. -- Cheers / Saludos, Carlos E. R. (from 15.4 x86_64 at Telcontar)
On 2023-01-16 09:07, Simon Becherer wrote:
Hi,
i have to rename hunderts of files:
is there a easy way to do this (without writing a script)?
krename It is a GUI. You can see how the files will be renamed before doing it, and has undo. Needs a bit of learning. -- Cheers / Saludos, Carlos E. R. (from 15.4 x86_64 at Telcontar)
Am 16.01.23 um 11:15 schrieb Carlos E. R.:
On 2023-01-16 09:07, Simon Becherer wrote:
Hi,
i have to rename hunderts of files:
is there a easy way to do this (without writing a script)?
krename
It is a GUI. You can see how the files will be renamed before doing it, and has undo.
Needs a bit of learning.
Not much!
On 16/01/2023 11:15, Carlos E. R. wrote:
On 2023-01-16 09:07, Simon Becherer wrote:
Hi,
i have to rename hunderts of files:
is there a easy way to do this (without writing a script)?
krename
It is a GUI. You can see how the files will be renamed before doing it, and has undo.
Needs a bit of learning.
I've used krename for this type of renaming before. In this case, on opening the program, go to the fourth tab, 'Filename' at the top, then click the 'Advanced Filename' tab underneath. You can click on the 'Functions' button to get a descriptive list of possibilities, so one that would work here is [$x-y] character x to y of old filename. So you'd want to put [$1-6] in the Template field. It shows you the result in the box below on all the files you've selected, before having to commit. gumb
On Mon, Jan 16, 2023 at 09:07:30AM +0100, Simon Becherer wrote: [...]
i have to rename hunderts of files: 792041_100029699.mi 792090_100029501.mi . . . to 792041.mi 792090.mi . . .
[...] Although you said "without writing a script", for this kind of thing I have often used python's glob and pexpect modules. You can build up the command that you want, check what it would do by printing it, and then finally add a line to run it with pexpect's run. -- ============================ Roger Whittaker roger@disruptive.org.uk ============================
On Mon, 16 Jan 2023 09:07:30 +0100 Simon Becherer <simon@becherer.de> wrote:
Hi,
i have to rename hunderts of files: 792041_100029699.mi 792090_100029501.mi . . . to 792041.mi 792090.mi . . .
i tried: rename ??????_*.mi ??????.mi *.mi
rename: ??????_.mi: kein Zugriff: Datei oder Verzeichnis nicht gefunden as you see, no sucess.
(first file is gone, all others stay as they are)
is there a easy way to do this (without writing a script)?
Yes, RTFM ! :) man rename and look at the example for shortening file names. In particular the quoting. It might also be useful to consider -v -n first.
simoN
Simon Becherer wrote:
is there a easy way to do this (without writing a script)?
I think it would be faster writing the 3-line script :-) -- Per Jessen, Zürich (3.4°C) Member, openSUSE Heroes (2016 - present) We're hiring - https://en.opensuse.org/openSUSE:Heroes
Am 16.01.23 um 18:22 schrieb Andrei Borzenkov:
On 16.01.2023 16:14, Per Jessen wrote:
Simon Becherer wrote:
is there a easy way to do this (without writing a script)?
I think it would be faster writing the 3-line script :-)
You do not even need 3 lines ...
for i in *.mi; do echo mv "$i" "${i/_?????????/}"; done
thank you all for your suggestions. i will check what is best for my purpose. simoN -- www.becherer.de ----------------------------------------------- - Das ist die vorlaeufig endgueltige Version! - Herbert C. Maier Dipl.-Ing. (FH) -----------------------------------------------
Andrei Borzenkov wrote:
On 16.01.2023 16:14, Per Jessen wrote:
Simon Becherer wrote:
is there a easy way to do this (without writing a script)?
I think it would be faster writing the 3-line script :-)
You do not even need 3 lines ... for i in *.mi; do echo mv "$i" "${i/_?????????/}"; done
Haha, I was clearly too focused on readability :-) -- Per Jessen, Zürich (7.3°C) Member, openSUSE Heroes (2016 - present) We're hiring - https://en.opensuse.org/openSUSE:Heroes
SimoN, et al -- ...and then Simon Becherer said... % % i have to rename hunderts of files: % 792041_100029699.mi % 792090_100029501.mi % . % . % . % to % 792041.mi % 792090.mi ... % % is there a easy way to do this (without writing a script)? I think you've been beaten up enough already but it does boil down to either using some specialized piece of software or using shell processes. So does a single line with some semicolons in it count as a script? :-) This is pretty trivial stuff even though it's a classic example of a "do what I mean, not what I say" command. Personally I like the shell and the common OS tools because that's easier for me than remembering arcane command-line formats or digging up some clicky tool, but YMMV. HTH & HAND :-D -- David T-G See http://justpickone.org/davidtg/email/ See http://justpickone.org/davidtg/tofu.txt
On 1/16/23 02:07, Simon Becherer wrote:
i have to rename hunderts of files: 792041_100029699.mi 792090_100029501.mi . . . to 792041.mi 792090.mi . . .
cd /path/to/dir/with/files for fname in *; do [ -f "$fname" ] || continue # skip everything not a file mv "$fname" "${fname%_*}.mi" done (will work for any advanced or POSIX shell, ash, bash, dash, etc...) -- David C. Rankin, J.D.,P.E.
On 1/16/23 18:29, David C. Rankin wrote:
On 1/16/23 02:07, Simon Becherer wrote:
i have to rename hunderts of files: 792041_100029699.mi 792090_100029501.mi . . . to 792041.mi 792090.mi . . .
cd /path/to/dir/with/files
for fname in *; do [ -f "$fname" ] || continue # skip everything not a file mv "$fname" "${fname%_*}.mi" done
(will work for any advanced or POSIX shell, ash, bash, dash, etc...)
Another option for REGEX replacement is the gprename package in the main repo. Only issue is the interface looks terrible with the "courier new" font that is hardcoded. It's just a perl script, so you can change line 69 to: style 'my_text' { font_name ='DejaVu Sans Mono' } to use DejaVu Sans Mono as the font and it looks 100 times better. I've searched the repos for 15.4 and cannot find a perl-rename package for openSUSE. Perl-rename (packaged as rename on many distros) is so much more flexible than the rename (from the util-linux package) used by openSUSE, there is really no comparison. -- David C. Rankin, J.D.,P.E.
On Mon, 16 Jan 2023 20:08:39 -0600 "David C. Rankin" <drankinatty@suddenlinkmail.com> wrote:
On 1/16/23 18:29, David C. Rankin wrote:
On 1/16/23 02:07, Simon Becherer wrote:
i have to rename hunderts of files: 792041_100029699.mi 792090_100029501.mi . . . to 792041.mi 792090.mi . . .
cd /path/to/dir/with/files
for fname in *; do [ -f "$fname" ] || continue # skip everything not a file mv "$fname" "${fname%_*}.mi" done
(will work for any advanced or POSIX shell, ash, bash, dash, etc...)
Another option for REGEX replacement is the gprename package in the main repo. Only issue is the interface looks terrible with the "courier new" font that is hardcoded. It's just a perl script, so you can change line 69 to:
style 'my_text' { font_name ='DejaVu Sans Mono' }
to use DejaVu Sans Mono as the font and it looks 100 times better.
I've searched the repos for 15.4 and cannot find a perl-rename package for openSUSE. Perl-rename (packaged as rename on many distros) is so much more flexible than the rename (from the util-linux package) used by openSUSE, there is really no comparison.
Why is everybody suggesting variations on scripts and other commands, when the command that Simon used will do the job he requires and even has an exact example of how to do it on its man page?
Dave, et al -- ...and then Dave Howorth said... % % Why is everybody suggesting variations on scripts and other commands, % when the command that Simon used will do the job he requires and even % has an exact example of how to do it on its man page? Maybe because some of us are unfamiliar with handy little utilities like this only useful for one-off jobs and prefer to rely on the bare minimum installation of *NIX tools (sh, sed, mv). Yeah, I vaguely recall tripping across one of these rename utilities a few decades ago but also recall not bothering to hunt it down in subsequent installs over the years because, well, it's unnecessary. YMMV, of course, which is why it exists (and in so many forms 'cuz TMTOWTDI), but that's why I didn't go hunting for grammar for some util for him ... HAND :-D -- David T-G See http://justpickone.org/davidtg/email/ See http://justpickone.org/davidtg/tofu.txt
Am 17.01.23 um 21:26 schrieb David T-G:
Dave, et al --
...and then Dave Howorth said... % % Why is everybody suggesting variations on scripts and other commands, % when the command that Simon used will do the job he requires and even % has an exact example of how to do it on its man page?
Maybe because some of us are unfamiliar with handy little utilities like this only useful for one-off jobs and prefer to rely on the bare minimum installation of *NIX tools (sh, sed, mv). Yeah, I vaguely recall tripping across one of these rename utilities a few decades ago but also recall not bothering to hunt it down in subsequent installs over the years because, well, it's unnecessary.
YMMV, of course, which is why it exists (and in so many forms 'cuz TMTOWTDI), but that's why I didn't go hunting for grammar for some util for him ...
HAND
:-D
i have already tried some of the suggestions and in addition i have installed the pearl rename. (for the future) at the moment, because i recognized that its actually not a really rename what i need, its more a truncate the length of the name, and, because some of the names i have to rename are some chars longer than what i have written in the beginning, examples: 522684-0_100029439-00.mi 787200-02_100000512-00.mi 792090_100029501.mi i use for this now: for i in *.mi; do mv "$i" "${i:0:6}.mi"; done thanks again, i have learned a lot. if i now find a page who explained me (for me best if the page is in german language) the perl regex and the shell expressions i would be fine. i have to search for this when i find a little bit time. simoN -- www.becherer.de ----------------------------------------------- - Das ist die vorlaeufig endgueltige Version! - Herbert C. Maier Dipl.-Ing. (FH) -----------------------------------------------
On Wed, Jan 18, 2023 at 9:56 AM Simon Becherer <simon@becherer.de> wrote:
at the moment, because i recognized that its actually not a really rename what i need, its more a truncate the length of the name, and, because some of the names i have to rename are some chars longer than what i have written in the beginning,
Which is exactly why I hesitated to provide actual command - I was pretty sure it would be wrong for some filenames. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
Am 18.01.23 um 08:09 schrieb Andrei Borzenkov:
On Wed, Jan 18, 2023 at 9:56 AM Simon Becherer <simon@becherer.de> wrote:
at the moment, because i recognized that its actually not a really rename what i need, its more a truncate the length of the name, and, because some of the names i have to rename are some chars longer than what i have written in the beginning,
Which is exactly why I hesitated to provide actual command - I was pretty sure it would be wrong for some filenames.
Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
well, with this suggestions here, i was able to solve the problem. lets say with your words: i have seen how you catch the fish. and i have here seen also how other people catch there fishes. so i will be able to do it similar. simoN -- www.becherer.de ----------------------------------------------- - Das ist die vorlaeufig endgueltige Version! - Herbert C. Maier Dipl.-Ing. (FH) -----------------------------------------------
On 1/16/23 18:29, David C. Rankin wrote:
On 1/16/23 02:07, Simon Becherer wrote:
i have to rename hunderts of files: 792041_100029699.mi 792090_100029501.mi . . . to 792041.mi 792090.mi . . .
cd /path/to/dir/with/files
for fname in *; do [ -f "$fname" ] || continue # skip everything not a file mv "$fname" "${fname%_*}.mi" done
(will work for any advanced or POSIX shell, ash, bash, dash, etc...)
With perl-rename (executable 'file-rename') from the perl-File-Rename package, for 15.4 in https://download.opensuse.org/repositories/devel:/languages:/perl/15.4/ Use for your case: file-rename -e 's/_[^.]+//' *.mi You add the -n option for a dry-run to see what would be renamed without actually doing it. In your case that's $ file-rename -n -e 's/_[^.]+//' *.mi rename(792041_100029699.mi, 792041.mi) rename(792090_100029501.mi, 792090.mi) perl-rename is the way to go. Infinitely flexible using perl REGEX and has the dry-run option :) -- David C. Rankin, J.D.,P.E.
participants (11)
-
Andrei Borzenkov
-
Bernd Ritter
-
Carlos E. R.
-
Dave Howorth
-
David C. Rankin
-
David T-G
-
gumb
-
Per Jessen
-
Peter McD
-
Roger Whittaker
-
Simon Becherer