Although perl
's rename
tool has become an invaluable companion for my daily work, there are still some quirks that make work more difficult - especially when files are not in current work directory.
Let's suppose there are lots of invoices, and we know that all from customer1 are already assigned to an account. So, to make script processing easier later, we want to prepend assi_
for "assigned".
For example:
/home/user/bizz/invoices $ ls -1 t1/inv*
t1/inv_customer1_20130506.txt
t1/inv_customer1_20130823.txt
t1/inv_customer1_20130930.txt
t1/inv_customer2_20131207.txt
t1/inv_customer2_20131113.txt
Now look what will happen:
/home/user/bizz/invoices $ rename 's/^/assi_/' t1/inv_customer1*
Can't rename t1/inv_customer1_20130506.txt assi_t1/inv_customer1_20130506.txt: No such file or directory
(etc.)
Heheh. rename
has chosen the whole path for prepending assi_
to, instead of only the file. So no wonder it cannot find inv_customer1_20130506.txt in the non-existing directory "assi_t1".
So how can rename
be taught to apply the regular expression to the file name only?
Notes:
1. I don't always want to cd
to the subdirectories.
2. I would refrain from using loops as well as find
, if possible.
rename 's/\//\/assi_/' t1/inv_customer1*
– Costas Nov 08 '14 at 21:48rename 's/inv/assi_inv/' t1/inv_customer1*
– Costas Nov 08 '14 at 21:49(cd t1 ; rename 's/^/assi_/' inv_customer1*)
is acceptable to you? – Costas Nov 08 '14 at 22:18rename 's|([^/]+$)|assi_$1|' t1/inv_customer1*
you'd like more? – Costas Nov 08 '14 at 22:49