I have a limited bash shell on my NAS and a lot of files that contains the char ~ in the filename. For example, I have a file in 2018/10/example~1.jpg and I have to move and rename it to allFiles/2018-10-example_1.jpg.

I have to replace the ~ with _, and add the path with the - instead of / in the filename.

Could you please help me?

P.S. Only files with .jpg/.png/.mov extension

  • 3
    Since you say that you have "a limited bash shell", it would be good to know in what way it is limited.
    – Kusalananda
    Apr 5, 2020 at 15:59
  • it's a QNAP NAS, there's a shell with all base commands but I can't install more program.
    – Agosh
    Apr 5, 2020 at 16:32
  • Can you assess the files from a Unix (UNIX, BSD, Gnu/Linux, MacOSX) machine? or is only access via its built in shell? Apr 5, 2020 at 16:33
  • What is the output of cat /proc/$$/cmdline Apr 5, 2020 at 16:41

1 Answer 1


I don't have a QNAP, but I believe its shell is actually BusyBox. This should work with both BusyBox and bash (although with bash, you'd also need find+cut+awk to be installed):

Assuming the year directories (ie "2018") and the "allFiles" directories are at the same level, and that level is your current working directory in your shell:

find . -name '*.jpg' | cut -c3- | awk '{ newname=$0; gsub(/~/, "_", newname); gsub(/\//, "-", newname); gsub(/^/, "allFiles/", newname); print "mv " $0 " " newname; }'

That will produce a set of commands to move all your .jpg files. Review the commands to make sure they're reasonable first. Then run them by tacking on "| sh" to the end of the above command.

Then replace '*.jpg' in the above with another file extension you wanted to process, and repeat.

  • I'd suspect sed(1) is more likely to be available than awk(1).... and I'd write out a script for doing the renaming, to check and run later.
    – vonbrand
    Apr 5, 2020 at 20:07
  • As I mentioned, I believe QNAP uses BusyBox, and BusyBox has both sed and awk builtin, in which case there's no issue. But if it's really using bash, then sure.
    – Stingray
    Apr 5, 2020 at 20:10

You must log in to answer this question.

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