How to replace spaces in file names with underscores?
How to replace spaces in file names with underscores?
Please have a look at the third post of this thread.
I do re-use this one.
Thanks
I do re-use this one.
Thanks
Last edited by R-S-H on Fri 08 Mar 2013, 17:17, edited 2 times in total.
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]
The new LazY Puppy Information Centre[/url][/b]
How to replace spaces in file names with underscores?
Yes, sunburnt. Did it!sunburnt wrote:Hey RSH; Just rewrite and reuse this thread for another Q !
Ok.
I want to search for files with spaces ' ' in its names and replace them with underscores '_'.
There could be just a single one but also three or four of those spaces ' '.
How can I do this?
Thanks
RSH
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]
The new LazY Puppy Information Centre[/url][/b]
Re: How to replace spaces in file names with underscores?
Here's a script I've been using for some time:R-S-H wrote:Yes, sunburnt. Did it!sunburnt wrote:Hey RSH; Just rewrite and reuse this thread for another Q !
Ok.
I want to search for files with spaces ' ' in its names and replace them with underscores '_'.
There could be just a single one but also three or four of those spaces ' '.
How can I do this?
Thanks
RSH
Code: Select all
#!/bin/sh
if [ -d $1 ]; then #check if $1 is a directory
cd $1
for file in *; do mv "$file" `echo $file | sed -e 's/ */_/g' -e 's/_-_/-/g'`; done
else
mv "$1" `echo $1 | sed -e 's/ */_/g' -e 's/_-_/-/g'`
fi
Oh, thank you very much.jrb wrote:Here's a script I've been using for some time:It will work for a single file or a directory of files.Code: Select all
#!/bin/sh if [ -d $1 ]; then #check if $1 is a directory cd $1 for file in *; do mv "$file" `echo $file | sed -e 's/ */_/g' -e 's/_-_/-/g'`; done else mv "$1" `echo $1 | sed -e 's/ */_/g' -e 's/_-_/-/g'` fi
I knew it would be needed such stuff, not programmable by myself!
I will try it.
Thanks again
RSH
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]
The new LazY Puppy Information Centre[/url][/b]
RSH,
I use this one-
Usually in directories with downloaded media. It could be used for any file with just *.
Regards,
s
I use this one-
Code: Select all
for file in *.mp* *.flv;do
newfile=${file// /_}
mv -v "$file" $newfile;done
Regards,
s
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
in current dir?
or recursively?
Note: for find you can use -name or -iname to filter
Code: Select all
for f in *;do mv "$f" "${f// /_}";done
Code: Select all
for f in `find .`;do mv "$f" "${f// /_}";done
Last edited by technosaurus on Sat 09 Mar 2013, 04:24, edited 1 time in total.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].
@Seaside - Nice, simpler than mine. I changed to:
and it changed all files in my folder.
and it worked very well.
I like your technique. Is there an easy way to do both of the sed functions that I have in mine?
Code: Select all
cd $1
for file in *;do
newfile=${file// /_}
mv -v "$file" $newfile;done
I believe you have an extra " in there. I changed totechnosaurus wrote:in current dir?or recursively?Code: Select all
for f in *;do mv "$f" "${f// /_};done
Note: for find you can use -name or -iname to filterCode: Select all
for f in `find .`;do mv "$f" "${f// /_};done
Code: Select all
for f in *; do mv "$f" ${f// /_}; done
I like your technique. Is there an easy way to do both of the sed functions that I have in mine?
Code: Select all
sed -e 's/ */_/g' -e 's/_-_/-/g'
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
nope, it was missing one, fixed.
new=`echo $old` #squeeze all tabs and spaces to a single space
new="${new// /_} #you have to do these 1by1, but I think "," and [] work
new="${new//_-_/-} #technically this could be in the mv step
mv "$old" "$new"
new=`echo $old` #squeeze all tabs and spaces to a single space
new="${new// /_} #you have to do these 1by1, but I think "," and [] work
new="${new//_-_/-} #technically this could be in the mv step
mv "$old" "$new"
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
I found this script (/function) while looking for something else:
it recurses into subdirectories without needing find, but is missing logic to not follow symlinks into a circular loop if ... ya know, there's a circular loop
Code: Select all
#!/bin/ash
sr(){
[ "$1" ] && [ -d "$1" ] && cd $1
for x in * ; do
y=${x// /_}
[ "$x" != "$y" ] && mv "${x}" "${y}"
[ -d $y ] && sr $y &
done
}
sr $@
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].