1

I'm running a shell script to remove all metacharacters with sed with this code.

for file in /path/to/folder/*; do
   mv "$file" "$(echo $file | sed "s,[()\^\$\?*+=\|#!@%&-],,g")"
done

It works great, but when I want it to also look for forward slashes ("/") it deletes all files. this is what i used. s,[/()\^\$\?*+=\|#!@%&-],,g

Why is that? and how would I also target forward slashes.

Thanks.

anubhava
  • 761,203
  • 64
  • 569
  • 643
PhDeOliveira
  • 2,323
  • 4
  • 22
  • 25
  • 1
    What do you think `echo $file` will do if your file name contains white space, globbing characters, etc? What do you mean by "metacharacters"? A file name CANNOT contain a "/" character. Also, I see "=" in the list among others that are not RE metacharacters nor are they globbing metacharacters so what is it you're REALLY trying to accomplish? – Ed Morton Aug 08 '13 at 00:51

2 Answers2

1

Better use this (don't try to re-invent a square wheel) :

detox /path/to/dir/*

See http://detox.sourceforge.net/

Description : An utility designed to clean up filenames by replacing characters with standard equivalents

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

No it didn't delete any file but they may have become hidden for you since they all start with dot . now. Run ls -al to see those files.

What has happened is that by including forward slash / in your sed command your files are being renamed to something like: .filename since very first / has been stripped off by sed.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I don't see them as hidden. I ran ls -al and just saw the typical . .. .DS_Store. – PhDeOliveira Aug 07 '13 at 18:48
  • Any other way around it? I just have co-workers that tend to use metacharacters in their filenames... i know.. i know.. – PhDeOliveira Aug 07 '13 at 18:50
  • Why are you including `/` in your sed command since that is also used in file's path. – anubhava Aug 07 '13 at 18:51
  • because occasionally i'll see filenames that contain `/`. Just want to strip them out. – PhDeOliveira Aug 07 '13 at 18:54
  • No, filenames in Unix/Linux cannot contain a forward slash. – anubhava Aug 07 '13 at 18:55
  • 1
    Well, your command would have resulted in something like `mv /path/to/folder/foo.txt pathtofolderfoo.txt`, or given a relative path `mv ./path/to/folder/foo.txt .pathtofolderfoo.txt`, so, unless you got a bunch of error messages from `mv`, you'll need to check those sorts of file names now... – twalberg Aug 07 '13 at 18:55
  • Thats essentially the problem. My co-workers sometimes will save files with a `/` on them.. don't know why they do it.. they just do. So i want to find out a way strip them out.. and by the looks of it there isn't? I would just have to individually go and rename them? – PhDeOliveira Aug 07 '13 at 19:02
  • As I commented earlier: **filenames in Unix/Linux cannot contain a forward slash** Try creating a file on your system with a forward slash and you will get error. – anubhava Aug 07 '13 at 19:05
  • @anubhava thats not the case. I'm precisely testing that and I can save a file with a `/` in them. Otherwise I wouldn't be asking. – PhDeOliveira Aug 07 '13 at 19:09
  • I have tried: `touch 'foo/bar'` or `touch 'foo\/bar'` on Linux and OSX and it didn't work any where. Can you please tell me one Unix command/util that succeeds creating a file with / in its name. – anubhava Aug 07 '13 at 19:12
  • @anubhava these are simply saved files. Say for instance textedit or a photoshop or excel. we're not creating files through command line. – PhDeOliveira Aug 07 '13 at 19:16
  • I'm still puzzled to know how a software succeeded in creating a file with `/`. I tried saving to `foo/bar.rtf` from textedit on my Mac and it created filename `foo:bar.rtf` – anubhava Aug 07 '13 at 19:22
  • Pls read: http://stackoverflow.com/questions/9847288/is-it-possible-to-use-in-a-filename and then http://superuser.com/questions/512584/can-i-rename-a-file-that-contains-a-forward-slash-in-unix – anubhava Aug 07 '13 at 19:27
  • @anubhava don't know how else to explain it either. I'm baffled by my coworkers.. but it happens and I have to fix it. http://pbrd.co/1767GCn – PhDeOliveira Aug 07 '13 at 19:29
  • 1
    Is the `/` in those file names possibly not an ASCII 47 (0x2f), and is instead something Unicode-ish or some other encoding? Unix/Linux may not catch that if it's created through Samba/CIFS or something, I suppose, but I'd have to test that to be sure... Also, your `sed` bits likely wouldn't have caught it either... – twalberg Aug 07 '13 at 19:30
  • Please check my linked Q&A. Also can you show me output of `ls -al` from a directory that contains filenames with `/` in it. – anubhava Aug 07 '13 at 19:31
  • @twalberg you're right. it's really a `:` and added that to the regex and it took it out. – PhDeOliveira Aug 07 '13 at 19:36
  • Yes that is expected. This is what I wrote earlier: `I tried saving to foo/bar.rtf from textedit on my Mac and it created filename foo:bar.rtf` – anubhava Aug 07 '13 at 19:38