LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-06-2015, 01:14 AM   #1
bobhurt
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: 0
Question How to replace filename whitespace with underscore?


I have a routine need to replace filename whitespace (space, period, hyphen, etc.) with underscore. While scrounging, I found a comment online referring to a program and downloaded it. It worked perfectly. Now I have no clue about its location and name. I know. I should have given it a friendly name, stashed it where I could find it, and put its location in the path.

Can anyone tell me what program will do the job and where I can find it?

This time I'll stash it where I can find it.

Thanks
 
Old 05-06-2015, 10:52 AM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
You can use shell script with sed to change them to '_'.

Code:
#!/bin/bash
mv "$1" `echo "$1" | sed -e 's@[ ]\+\|[.]\+\|[-]\+@_@g'`
And run this script with 'scrname filename'.
 
Old 05-07-2015, 06:48 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,788
Blog Entries: 13

Rep: Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831
You can use the rename(1) command:
Code:
$ echo 1 > "ab cd.txt"
$ echo 2 > "for this.txt"
$ ls
ab cd.txt  for this.txt
$ man rename
$ rename s/\ //g *.txt
$ ls
abcd.txt  forthis.txt
$
 
1 members found this post helpful.
Old 05-07-2015, 07:07 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,309

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Code:
[schneidz@hyper test]$ ll
total 428
-rw-rw-r--. 1 schneidz schneidz 438176 May  6 10:43 admin.php
-rw-rw-r--. 1 schneidz schneidz      0 May  7 07:58 file name-1.txt
[schneidz@hyper test]$ for file in *; do  mv "$file" `echo $file | tr ' .-' '___'`; done
[schneidz@hyper test]$ ll
total 428
-rw-rw-r--. 1 schneidz schneidz 438176 May  6 10:43 admin_php
-rw-rw-r--. 1 schneidz schneidz      0 May  7 07:58 file_name_1_txt
 
Old 05-07-2015, 07:34 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,413

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
http://centos.tips/fixing-troublesome-filenames/
 
Old 05-07-2015, 05:05 PM   #6
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,602

Rep: Reputation: 2648Reputation: 2648Reputation: 2648Reputation: 2648Reputation: 2648Reputation: 2648Reputation: 2648Reputation: 2648Reputation: 2648Reputation: 2648Reputation: 2648
you really do not want to rename " admin.php" to " admin_php"
 
1 members found this post helpful.
Old 05-07-2015, 06:46 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,309

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
^ i thought it was weird too but that is what the op describes.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Replace blank space by underscore in output of a command. vickyk Linux - Newbie 7 02-22-2012 08:44 AM
[SOLVED] bash: how to replace symbols inside the file with filename deswarf Linux - Newbie 2 01-26-2012 04:55 AM
How to replace string in filename? homer_3 Linux - General 4 09-24-2011 04:13 PM
replace filename with variable Siljrath Programming 2 11-12-2009 12:40 AM
sed, replacing underscore with whitespace fjkum Programming 3 10-31-2007 12:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration