4

I am trying to run a command example:

sudo mv /home/vaishnavi/Downloads/*.ttc /home/vaishnavi/Downloads/*.ttf /usr/local/share/fonts/ms_fonts/

It works fine in the terminal, but when I try to add this in a shell script (.sh) I get an error saying:

mv: cannot stat '/home/vaishnavi/Downloads/*.ttf': No such file or directory

Simply, I want to move all the files of .ttf and .ttc type to /usr/local/share/fonts/ms_fonts/, in this case. Frankly speaking, I am not so expert in shell scripts. Please explain to me what wrong I did and also an alternative to achieve my expected results.

My sample script:

sudo mv /home/vaishnavi/Downloads/*.ttc /home/vaishnavi/Downloads/*.ttf /usr/local/share/fonts/ms_fonts/  
...

exit 0

  • 1
    Can you post the script you are writing? Or at least the lines around the failing command – dariofac Oct 14 '20 at 09:42
  • 1
    Aren’t you trying to move the files using the script after you moved them all manually? – Melebius Oct 14 '20 at 09:43
  • @Melebius I actually need the script to move each time after downloading something, it's not a one-time process. Whenever I download many .pngs for example, I want to move them using the script. – Ubuntovative is here Oct 14 '20 at 11:11
  • Not sure, but might be duplicate of this or this? See also https://unix.stackexchange.com/questions/204803/why-is-nullglob-not-default – pLumo Oct 14 '20 at 11:28
  • This might help: https://askubuntu.com/q/1201366/968501 – Raffa Oct 14 '20 at 11:31

1 Answers1

3

You'll get this error if there are no *.ttf files there. By default, if no files match a glob pattern, then the pattern itself remains in the command. Turn this off with the bash nullglob shell option

To be a bit safer, verify there are actually some files to move:

#!/usr/bin/env bash
...
shopt -s nullglob
# store files in an array
files=( /home/vaishnavi/Downloads/*.{ttc,ttf} )
# and if the array is not empty, move the files.
if (( ${#files[@]} > 0 )); then
    sudo mv -t /usr/local/share/fonts/ms_fonts/ "${files[@]}" 
fi
glenn jackman
  • 17,900