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?

8
  • 2
    A while loop doesn't look right here. This Q&A might help: unix.stackexchange.com/questions/19654/… The error you are seeing looks to be due to <"$var" which perhaps should have been <<<"$var".
    – guest
    Jun 21, 2020 at 6: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, 2020 at 6:54
  • I think this answer about backticks could be useful for you.
    – Francesco
    Jun 21, 2020 at 7:28
  • 1
    You declare an array of glob, then call the variable as $eighteen, but it's not a simple variable Jun 21, 2020 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 Jun 21, 2020 at 12:56

1 Answer 1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.