I have on a Samba server lots of files in directories/subdirectories containing special chars. The problem is that, the files (containing a special chars) are not visible when I try to view files using Windows. So I wrote command (on the below) to change names of files with special characters, but putting back renamed files to dirs/subdirs doesn't work properly:

find . -type f -exec rename -v 's/[^a-zA-Z0-9 &](?=[^.]*\.)/_/g' '{}' \;

On the bottom I post current directory containing files to rename:

./dir 1/fil[special char]e3.jpg
./dir 1/another dir2/m[special char]r.file1.txt
./dir 1/another dir2/file[special char]2.doc
./dir 3/next dir4/dir 5/fi[special char]le5.xml
./file 4.zip

Expect - after using command described above (files in dirs/subdirs):

./dir 1/fil_e 3.jpg
./dir 1/another dir2/m_r_file1.txt
./dir 1/another dir2/file_2.doc
./dir 3/next dir4/dir 5/fi_le5.xml
./file 4.zip

reality - files in same folder where I run command:

__dir 1_fil_e 3.jpg
__dir 1_another dir 2_m_r_file1.txt
__dir 1_another dir 2_file_2.doc
__dir 3_next dir4_dir 5_fi_le5.xml
__file 4.zip

and I have 2 questions:

  1. how I can exclude pathname from filename?
  2. how to put files in to directories?
  • 1
    Does your implementation of find have a -execdir? Jul 4, 2017 at 20:05
  • On 99% - yes, samba runs on debian 8 so give me a moment to check Jul 4, 2017 at 20:54
  • it works! ;) Thank you @steeldriver for help, problem solved Jul 4, 2017 at 21:19

1 Answer 1


If you have it, use the -execdir option for find instead of -exec.

  • -exec runs the command from the root of the command with each path as an argument
  • -execdir runs the command in the target directory with each filename as an argument


find . -type f -execdir rename -v 's/[^a-zA-Z0-9 &](?=[^.]*\.)/_/g' '{}' \;

Please note, that since this answer was posted, the rename command referenced in the question and this answer has been overtaken by an alternative: rename [options] expression replacement file…. The rename shown here may be available on your system as prename. If not, you can download and install it using this recipe.

  • Thank you too @roaima for explanation of the difference between -exec and -execdir, problem solved Jul 4, 2017 at 22:08
  • Watch out, on some linux rename is a util-linux simple string replacement Usage: rename [options] expression replacement file..., on others rename is a perl tool (as in this example) with more complex expression replace learnbyexample.github.io/learn_perl_oneliners/…
    – gaoithe
    Jul 18 at 11:44
  • @gaoithe yes. However, when I wrote my question the perl version of rename was far more prevalent, and it matched the version the OP was using in their question
    – roaima
    Jul 18 at 11:59
  • @roaima Your answer is a very good one. The combination of -execdir and rename with find should be more widely known. I just added comment as the other rename is the default on many systems - unfortunately! - because the perl rename has proper expression replacement. Anyway, it's all good. Link to GNU rename reference: man7.org/linux/man-pages/man1/rename.1.html
    – gaoithe
    Jul 18 at 12:08
  • 1
    @gaoithe thank you for the information. I've amended the answer to incorporate a similar suggestion
    – roaima
    Jul 18 at 13:11

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.