Bash replace space with underscore in file names




















Hi all, I'm hoping you can help.. I've used this forum a couple of times and I am back again now i've moved onto something more complex for me! I am searching while I await a response to this so if it has been asked already I apologize.

I have a file with lines in it that look like: bob johnson email email. How to replace all entries of comma in text file by space or other character. Hi , How to replace all entries of comma in text file by space or other character. Hi I have a file which have say about , records.. RedHat Commands. OpenSolaris Commands. Linux Commands. SunOS Commands. FreeBSD Commands.

Asked 11 years, 8 months ago. Active 7 months ago. Viewed k times. Improve this question. Good question. I wouldn't want to overwrite existing files or lose any data.

It should leave it unchanged.. Add a comment. Active Oldest Votes. Improve this answer. Naidim Naidim 6, 2 2 gold badges 21 21 silver badges 20 20 bronze badges. Simple and work in mac. Unlike the 'find -name' answer, this one worked on my OS X! Thank you sir!

The globstar option is internal to bash, whereas the rename command is a common Linux tool and not part of bash. Show 4 more comments. Do it in two steps: find. Somasundaram Sekar 4, 5 5 gold badges 37 37 silver badges 71 71 bronze badges. Dennis Williamson Dennis Williamson k 87 87 gold badges silver badges bronze badges.

If you're running this on OS X, you'll need to brew install rename — loeschg. This doesn't work on Centos 7, as the rename command is completely different it's a binary, not a perl script , and it doesn't accept data from stdin. Show 15 more comments. Michael Krelin - hacker Michael Krelin - hacker k 22 22 gold badges silver badges bronze badges. Works like a charm. Thanks Michael. The difference being the last 2 are for whitespace catching tabs and spaces , with the last needed for POSIX compliance.

However this only accomplishes half of your task. You also need to be able to do this for every file within a directory. Your only solution is to iterate through each file and overwrite them individually.

For loops by default should come equipped with file iteration syntax , and when used with wildcards expands out to all files in a directory. However sed's used in this manner appear to completely lose output when redirecting to a file. To correct this, you must specify sed with the -i flag so it will edit its files. Whatever item you pass after the -i flag will be used to create a backup of the old files. If no extension is passed -i '' for instance , no backup will be created.

Which looks for all files in your current directory and echos the sed output to all files Directories do get listed but no action occurs with them. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.

Replace spaces in all files in a directory with underscores Ask Question. I deleted the comments but he really did have a point.

Please don't do this. File names with spaces are a very bad idea and will make anything you need to do down the line more complex for no reason. I strongly urge you to reconsider and avoid using spaces. And, this should still be linked here: unix.

Add a comment. Active Oldest Votes. Improve this answer. Wildcard Probably a better answer now. Changed to -i. Great answer; I'd never used the bash variable substitution feature. One note about mv : when the target name exists already as a directory , mv will simply put the file in that directory without renaming either the file or the directory.

Neither -i nor -n will prevent this behavior; you would have to check for the existence of the target first.



0コメント

  • 1000 / 1000