1

This is partly because I have a very shallow understanding of BASH.

Here is the script:

eighteen=(2018*/*)
newe=`echo "$eighteen" | sed -e 's/\(-\)*$//g'`

while read f <&3 && read nf <&4; do
mv -v "${f}" "${nf}" break done 3<"$eighteen" 4<"$newe"

I am trying to compensate the lousy script I wrote, and delete - symbol at the end of each filename in multiple subfolders.

and the error I get: ./cleanup.sh: line 16: 2018-08-13/untitled-102.arw: No such file or directory

It is worth to note, that the error comes not from mv line, but from this: done 3<"$eighteen" 4<"$newe".

I copied the while loop from the answer on StackExchange. Quick food, usually, is not good for digestion, and quick solutions - for understanding, please help me to understand. Give me a hint on how previously mentioned substitution 4<"$newe" nf <&4 is called, how should I search to learn more about it. Is it applied outside while statement context?

Andrew
  • 13
  • 3
  • 2
    A while loop doesn't look right here. This Q&A might help: https://unix.stackexchange.com/questions/19654/how-do-i-change-the-extension-of-multiple-files The error you are seeing looks to be due to <"$var" which perhaps should have been <<<"$var". – guest Jun 21 '20 at 06:46
  • @guest Thanks a lot, that is just what I was looking for. Turns out, there was no need for such a complicated solution I was thinking of. Will learn more at Greg's Wiki. – Andrew Jun 21 '20 at 06:54
  • I think this answer about backticks could be useful for you. – Francesco Jun 21 '20 at 07:28
  • 1
    You declare an array of glob, then call the variable as $eighteen, but it's not a simple variable – Gilles Quénot Jun 21 '20 at 12:51
  • 1
    And you don't have to struggle with IO redirections (>&4...) for this simple tasks. Copying without understanding is randomly useful – Gilles Quénot Jun 21 '20 at 12:56
  • Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers. Please see the relevant help-center article – Gilles Quénot Jun 24 '20 at 02:26
  • @GillesQuenot thanks for the advice. I have not tried your answer, and therefore won't flag it as the one that solved my problem. Greg's wiki and @ guest's comment helped me a lot though. – Andrew Jun 24 '20 at 09:02
  • @guest See question answer's comment above. You may want to write a proper answer. – Kusalananda Jun 24 '20 at 09:53

1 Answers1

5

You seems to use a plain buggy shell script where you can just use the proper tool, keeping your regex:

rename -n 's/-*$//' 2018*/*

Remove -n switch when the output looks good to rename for real.

man rename

warning There are other tools with the same name which may or may not be able to do this, so be careful.

The rename command that is part of the util-linux package, won't.

If you run the following command:

$ rename

and you see perlexpr, then this seems to be the right tool =)

If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

$ sudo apt install rename
$ sudo update-alternatives --set rename /usr/bin/file-rename

For archlinux:

pacman -S perl-rename

For RedHat-family distros:

yum install prename

The 'prename' package is in the EPEL repository.


For Gentoo:

emerge dev-perl/rename

For *BSD:

pkg install gprename

or p5-File-Rename


For Mac users:

brew install rename

If you don't have this command with another distro, search your package manager to install it or do it manually (no deps...)


This tool was originally written by Larry Wall, the Perl's dad.