I want to rename every File in a folder-subsystem that contains a slash to be replaced with an underscore

Current filenames example:




The files are in different subfolders of the same folder-system and the slashes are in different positions. I have tried to use the rename-command as follows:

rename "s/\//_/g" *

But I could not get any success. Could you give me a lead in the right direction?

  • 3
    Hello/there.txt in GNU/Linux normally means file there.txt in directory Hello. Do you want to move the files or rename them? – Bruni May 14 at 7:59
  • 1
    @Bruni Yes, I'm well aware of that, but the files I'm targetting are actually from a usb-stick created on a windows-system and need to be renamed and have all slashes on the usb-stick removed. – Bob Ross May 14 at 8:00
  • 5
    Maybe there is confusion because the slashes were created in Windows, and slashes are not valid in file names in Linux because it denotes the boundary between directories (which corresponds to backslash in Windows). Maybe it works better to remove the slashes in Windows. – sudodus May 14 at 8:16
  • 1
    385 8 drwxr-xr-x 2 user user 8192 May 14 09:24 ./Main/_Ein\342\201\204Slash _______________ 390 8 -rw-r--r-- 1 user user 2 May 14 09:24 ./Main/_Ein\342\201\204Slash/Zw\342\201\204ei\342\201\204Slash – Bob Ross May 14 at 9:18
  • 2
    \342\201\204 would be a Unicode fraction slash - in a UTF-8 locale that would be an ordinary character not a directory separator and there should be no issues replacing it ex. rename -n 's/\342\201\204/_/g' * – steeldriver May 14 at 11:53

Based on the ls output in your comment, the character in question is a Unicode fraction slash rather than an ordinary ASCII slash (which would be illegal in a Unix filename, since it is used as the path separator character).

In a UTF-8 locale, (U+2044, octal \342\201\204 or hex 0xE2 0x81 0x84) is an ordinary character that you can manipulate / replace in the usual way with the perl-based rename command for example. Ex.:

$ touch hello⁄there.txt File⁄name.jpg

$ rename -n 's/\342\201\204/_/g' *
rename(File⁄name.jpg, File_name.jpg)
rename(hello⁄there.txt, hello_there.txt)


$ rename -n 's/\xe2\x81\x84/_/g' *
rename(File⁄name.jpg, File_name.jpg)
rename(hello⁄there.txt, hello_there.txt)

Remove the -n once you are happy that it is doing the right thing.

If you want to use find to descend directories recursively you can do so like

find . -type f -name "$(printf '*\342\201\204*')" -execdir rename -n 's/\342\201\204/_/g' {} +

(If the directory names can contain \342\201\204 and you want to replace those as well, then you will need to add -depth and remove the -type f restriction).

  • 1
    Thanks, your solution worked out so far. I would've never came to that solution myself, thank you for that in-depth explanation aswell. – Bob Ross May 17 at 8:08
  • 1
    Using find . -depth -name "$(printf '\342\201\204')" -execdir rename 's/\342\201\204/_/g' {} + I renamed the folders aswell. – Bob Ross May 17 at 8:10

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.