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

sofasurfer

Active Member
Joined
May 24, 2022
Messages
138
Reaction score
46
Credits
1,157
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/
 


KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
11,205
Reaction score
9,720
Credits
92,979
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.
 
OP
S

sofasurfer

Active Member
Joined
May 24, 2022
Messages
138
Reaction score
46
Credits
1,157
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.
 

camtaf

Active Member
Joined
May 14, 2023
Messages
350
Reaction score
224
Credits
2,336
Try....

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

craigevil

Well-Known Member
Joined
Feb 24, 2021
Messages
448
Reaction score
454
Credits
3,078
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
 

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
11,205
Reaction score
9,720
Credits
92,979
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.
 
OP
S

sofasurfer

Active Member
Joined
May 24, 2022
Messages
138
Reaction score
46
Credits
1,157
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.
 

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
11,205
Reaction score
9,720
Credits
92,979
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.
 

Diputs

Member
Joined
Jul 28, 2021
Messages
75
Reaction score
26
Credits
606
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
 


Latest posts

Top