On Thu, Jan 12, 2023 at 05:49:13PM +1000, Piers Rowan wrote:
> I have a structure like:
>
> /Dir1/123.junk.doc
> /Dir1/456.junk.pdf
> /Dir1/SubDir/1123.junk.doc
> /Dir1/SubDir/1456.junk.pdf
> /Dir2/SubDir/4321.junk.doc
> /Dir2/SubDir/7676.junk.pdf
> ...etc...
>
> I want some guidance as to how to make:
>
> 1123.junk.doc > 1123.doc
>
> $ID.junk.$EXT > $ID.$EXT

Using find and the perl rename utility (which is not the same as the rename
program in util-linux - that has completely different and incompatible command
line options):

    find /Dir1/ -type f -exec rename -n 's/\.junk\././' {} +

That's a dry-run, it will only print what **would** be renamed, without
actually doing it.  Once you've confirmed that it's going to do what you want,
run it without -n, or change -n to -v for verbose operation.

Optionally add a `g` regex modifier to the s/// operation ('s/\.junk\././g')
if filenames might contain .junk. more than once)

perl rename allows you to use **any** perl code to rename files - from simple
sed-like regex transformations like the one above to quite complex scripts
(it's pretty simple to use sprintf to, say, zero-pad numbers in filenames so
that they sort correctly with just a plain numeric sort rather than a natural
sort).



Depending on your distro, the perl rename command might be rename, prename,
file-rename, or perl-rename. Try running each of them to find out what it's
called on your system.

On Debian and related distros it's in the `rename` package and (via the
/etc/alternatives system is executed as just "rename"):

Package: rename
Version: 2.00-1
Installed-Size: 57
Maintainer: Debian Perl Group <[email protected]>
Architecture: all
Depends: perl:any
Description-en: Perl extension for renaming multiple files
 This package provides both a perl interface for renaming files (File::Rename)
 and a command line tool 'file-rename' which is intended to replace the version
 that used to be supplied by the perl package.




You can confirm which variant of rename you have installed with the -V option,
which works for both perl rename and util-linux rename:

If you have the perl version installed, it will mention either perl or
File::Rename depending on how old your version is.

$ rename -V
/usr/bin/rename using File::Rename version 2.00, File::Rename::Options version 
1.99

With the util-linux version, it will mention util-linux:

$ rename -V
rename.ul from util-linux 2.38.1

WARNING: Again, these two programs are not at all compatible. Aside from -V,
you can't use perl rename options with util-linux rename or vice-versa.


(Debian systems often have both installed, with perl as /usr/bin/rename and
util-linux rename as /usr/bin/rename.ul. Other distros might have util-linux
as rename and perl rename as prename)

craig
_______________________________________________
luv-main mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to