I just learned a new command to remove the spaces in all my filenames-detox

sofasurfer

Active Member
Joined
May 24, 2022
Messages
175
Reaction score
59
Credits
1,498
The command is <detox>. Have tested it on a couple of folders and it works. It has lots of options which I have not learned to use yet but it does work great. It removed my spaces and replaced them with <_> (underscore). It also removes and adds other symbols when used properly (theoretically).
Code:
  https://detox.sourceforge.net/
 


Lemme try something...

Code:
kgiii@kgiii:~/Documents/test$ touch 'foo bar' 'bar foo' 'foo foo' 'bar bar'
kgiii@kgiii:~/Documents/test$  ls
'bar bar'  'bar foo'  'foo bar'  'foo foo'
kgiii@kgiii:~/Documents/test$ for i in *' '*; do   mv "$i" `echo $i | sed -e 's/ //g'`; done
kgiii@kgiii:~/Documents/test$ ls
barbar  barfoo  foobar  foofoo

That's quick and dirty.

If you want to replace them with an underscore:

Code:
for i in *' '*; do   mv "$i" `echo $i | sed -e 's/ /_/g'`; done

That should do the trick.

That's not going to work with symbols, however. So, that's pretty neat. I suppose you could add symbols if you wanted.
 
Thanks for the effort. Your method seems to only remove the apostrophes, not replace the spaces with underscores. I did manage to remove the spaces and replace them with underscores
Code:
$ for i in *  *; do   mv "$i" `echo $i | sed -e 's/ /_/g'`; done
I was not able to replace underscores with hyphens. More to learn.
I think that is supposed to be possible with <detox> but I'm still working on that.
Thanks.
 
Try....

Code:
$ for i in *  *; do   mv "$i" `echo $i | sed -e 's/_/-/g'`; done
 
Renames files to make them easier to work with.
It removes spaces and other such annoyances like duplicate underline characters.
More information: https://github.com/dharple/detox.

- Remove spaces and other undesirable characters from a file's name:
detox path/to/file

- Show how detox would rename all the files in a directory tree:
detox --dry-run -r path/to/directory

- Remove spaces and other undesirable characters from all files in a directory tree:
detox -r path/to/directory
 
I was not able to replace underscores with hyphens.

You should be able to edit it for most anything. Though, you'd probably have to escape apostrophes. I am not quite sure how to do that. I'd have to tinker.
 
Code:
for i in *' '*; do   mv "$i" `echo $i | sed -e 's/ /_/g'`; done
While researching your script I stumbled on this website...
Code:
  https://www.redhat.com/sysadmin/bash-scripting-loops [code] This offers a great explanation of the 'for' loop command. I thought this might be very useful to others who struggle with loops, as I do.
 
While researching your script I stumbled on this website...
Code:
  https://www.redhat.com/sysadmin/bash-scripting-loops [code] This offers a great explanation of the 'for' loop command. I thought this might be very useful to others who struggle with loops, as I do.

You can easily edit the code I shared. It's not as fancy as the script you came across, but it's what was in my notes. As such, I'm 100% confident that I didn't come up with the code. Well, 98% sure...

But, if you look at the differences between the two, one has you searching for and replacing the spaces with an _ and the other has you replacing it with nothing - meaning it removes the searches. You could insert stuff there and mix it up a little.
 
Using whitespaces in names ... it's a bad idea in Windows,

... but it's literally the worst idea in Linux !

It's good that it can be fixed, but have a check who or what is creating these files
 

Members online


Top