2

I am often saving documents from my Windows-using coworkers, and the filenames usually contain spaces.

For example, I'm always renaming

Important CEO Spreadsheet v2.xls

to...

Important_CEO_Spreadsheet_v2.xls

Is there a way to script the Save As dialog box to automatically detect whitespace and replace it with underscores?

1
  • If this is something that can't be addressed at the operating-system level, and must instead be addressed at the application-level, then this might not be answerable. Feb 16, 2012 at 21:15

1 Answer 1

3

I can't think of anything that would change that in the box but you could run:

rename 's/ /_/g' *.xls

... in the directory that these are being saved to. You could save that as an alias in ~/.bash_aliases like: alias underscore="rename 's/ /_/g' *.xls" and then just run underscore.

Or you could create a new shortcut/link on your desktop that did something similar. It would probably need to run through Bash so you'd have a command like:

bash -c "cd yourXlsDir; rename 's/ /_/g' *.xls"
4
  • This would be helpful in the Downloads folder, since many documents are downloaded from web-based tools like the company wiki. However, I'm hoping for a solution that addresses the dialog box. Feb 16, 2012 at 21:14
  • @Oli: Instead of rename 's/ /_/' *.xls one could consider rename 's/ /_/g' *.txt because the g would take care of more than one space in the filename. Using -n with rename would do a dry run to see what the result will be without actually modifying anything.
    – user25656
    Feb 17, 2012 at 4:46
  • rename 's/ +/_/g' *.xls in case there are some files with more than one space between words.
    – Sylwester
    Jun 21, 2013 at 0:31
  • @Sylwester That's not needed as it's a global replace (it'll find it in the next scan) but it will replace each space with an underscore. Multiple concurrent underscores might be undesirable.
    – Oli
    Jun 21, 2013 at 1:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .