0

I have a file that looks like the following:

/var/www/html/media/2018@03@01@vi_0005_20180301_110203.h264.v0005.th.jpg.

I would like to find this file in /var/www/html/media and rename h264 to mp4 within a bash script that is run from a different folder.

My variables are:

mediapath="/var/www/html/media"
filemp4=vi_0005_20180301_110203.mp4
fileh264=vi_0005_20180301_110203.h264

I've tried the rename command (which works without variables), but it complained when using them. I'm also not sure how to make it look in the media folder.

rename 's/${fileh264}/${filemp4}/' *${fileh264}*

Global symbol "$mediapath" requires explicit package name (did you forget to declare "my $mediapath"?) at (user-supplied code).
Global symbol "$fileh264" requires explicit package name (did you forget to declare "my $fileh264"?) at (user-supplied code).
Global symbol "$mediapath" requires explicit package name (did you forget to declare "my $mediapath"?) at (user-supplied code).
Global symbol "$filemp4" requires explicit package name (did you forget to declare "my $filemp4"?) at (user-supplied code).

I've also attempted to use find and mv, but I'm not sure where it's going wrong.

find ${mediapath} -name "*${fileh264}*" -exec bash -c 'mv $0 ${0/${fileh264}/${filemp4}}' {} \;

Any help would be much appreciated! Thanks.

1
  • Ahh, of course, thanks! My solution then is as follows: rename "s/${fileh264}/${filemp4}/" ${mediapath}/*${fileh264}*
    – allemtura
    Commented Mar 1, 2018 at 11:58

2 Answers 2

1

The solution is I needed double quotes:

rename "s/${fileh264}/${filemp4}/" ${mediapath}/*${fileh264}*
1
  • 1
    Tell us this is perl's rename, can be another command on some distros Commented Mar 1, 2018 at 12:27
0

Quoting can be a wired thing on bash.

You should keep in mind, that a singlequote does not interpret anything. A $ is interpreted as an ASCII $ and not as the beginning of a variable.

You can combine different ways of quotes to reach different goals. I would recommend to check the following link and read "Strong Quoting" section. http://wiki.bash-hackers.org/syntax/quoting

As @allemtura said, the correct way is to use doublequotes to interpret the variables correctly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .