3

I want to use the rename command to remove characters on a file basename, based on characters entered (input) by the user in an interactive script.

Basically, I would like to do this:

read characters_to_remove_inputted_by_the_user

rename 's/$characters_to_remove_inputted_by_the_user//g' file

But I'm having trouble with properly inserting the variable and keep the command structure during the script execution.

2
  • I have to say that I have never used the rename command. However, according to the rename manpage, your syntax appears to be also wrong. The synopsis is rename [options] expression replacement file...
    – fpmurphy
    Commented Mar 25, 2017 at 19:21
  • Yes probably i am not using it correctly at the first place. I will try the first answer, but thanks for your help ! Commented Mar 25, 2017 at 19:51

2 Answers 2

4

Assuming -- as your own command line suggests -- perl's rename implementation (an example script shipped with perl since version 3 in 1989, and often included as a command in many OSes, sometimes as a rewrite with extensions separate from the perl package like this one), sometimes called prename on some Linux distributions where rename is otherwise the one from util-linux, you can use the environment:

STRING=$characters_to_remove_inputted_by_the_user rename 's/\Q$ENV{STRING}\E//g' ./*

Above, the content of the $characters_to_remove_inputted_by_the_user shell variable is passed as the value of the STRING environment variable which perl retrieves with $ENV{STRING}.

Alternatively, you could use the export shell special builtin so that that shell variable be passed in the environment of every command it executes including that rename:

export characters_to_remove_inputted_by_the_user
rename 's/\Q$ENV{characters_to_remove_inputted_by_the_user}\E//g' ./*

The \Q/\E are to make sure $STRING is treated as a fixed string and not a regexp. If you want it to be treated as a regexp, that's just:

REGEXP=$characters_to_remove_inputted_by_the_user rename 's/$ENV{REGEXP}//g' ./*

Avoid things like:

rename "s/$characters_to_remove_inputted_by_the_user//g" ./*

Above the content of the shell variable is expanded by the shell before calling rename, so that could amount to a command injection vulnerability, as the value of the variable would be interpreted as perl code (think for instance of a value like //;system("reboot");s/).

3
  • Does not work on Fedora 25. Error message is rename: not enough arguments
    – fpmurphy
    Commented Mar 26, 2017 at 6:32
  • @fpmurphy1, you've got the wrong rename, the one from util-linux. The OP seems to be using the perl one (might be called prename on Fedora). Commented Mar 26, 2017 at 7:17
  • on arch, install perl-rename
    – crazystick
    Commented Nov 15, 2019 at 17:06
1

With util-linux rename:

$ touch myfile1
$ ll
total 0
-rw-rw-r--. 1 fpm fpm 0 Mar 25 15:30 myfile1
$ read DELCHARS
my
$ rename -- "$DELCHARS" "" myfile1
$ ll
total 0
-rw-rw-r--. 1 fpm fpm 0 Mar 25 15:30 file1
$ 
4
  • Hi there, what i want to do is to have the user entering a sequence of all the characters he wants to remove from a filename. Commented Mar 25, 2017 at 19:50
  • Have you run my example? Are the characters to be removed contiguous or random? Your example assumes they are contiguous.
    – fpmurphy
    Commented Mar 26, 2017 at 6:28
  • hey @fpmurphy1 the idea was : User input all the characters he want to remove this way : &#"/ to remove & and # and " and / The script use rename to go through all files basenames and remove those characters if found. The answer below from Stéphane definitely helped and solved my issue. Commented Apr 1, 2017 at 15:47
  • @VinhFrancisGuyait. You initially failed to specify which rename you were using. I provided answer for the more common util-linux version - not the Perl version.
    – fpmurphy
    Commented Apr 2, 2017 at 2:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.