Linux - Replacing spaces in the file names
up vote
73
down vote
favorite
I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?
linux replace command filenames spaces
add a comment |
up vote
73
down vote
favorite
I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?
linux replace command filenames spaces
add a comment |
up vote
73
down vote
favorite
up vote
73
down vote
favorite
I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?
linux replace command filenames spaces
I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?
linux replace command filenames spaces
linux replace command filenames spaces
edited Mar 30 '15 at 12:16
Peter Mortensen
13.4k1983111
13.4k1983111
asked Nov 27 '09 at 5:08
Mithun Sreedharan
21.6k65160223
21.6k65160223
add a comment |
add a comment |
11 Answers
11
active
oldest
votes
up vote
129
down vote
accepted
This should do it:
for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename1 - foo.jpgand my folder already had1.jpgin it.
– byxor
Oct 18 '17 at 14:05
1
I find backticks bit hard to read when they are near quotes. The same but more readable would befor file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
– Kamil S Jaron
Jul 3 at 8:58
add a comment |
up vote
50
down vote
I prefer to use the command 'rename', which takes Perl-style regexes:
rename "s/ /_/g" *
You can do a dry run with the -n flag:
rename -n "s/ /_/g" *
1
this will work if you have the perl-style rename and not the simpler redhat/fedora one
– David Dean
Nov 27 '09 at 5:56
5
the fedora version would berename " " "_" *
– David Dean
Nov 27 '09 at 5:57
4
renamenot available in OSX it appears.
– Etienne Low-Décarie
Feb 12 '14 at 16:53
On macOS 10.12.3,renameis available.
– Sung Won Cho
Mar 14 '17 at 6:09
For some reasonrenamedoes not do a single thing on my CentOS box. Either with regex or not.
– Buttle Butkus
Jun 28 '17 at 6:32
|
show 4 more comments
up vote
9
down vote
Use sh...
for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done
If you want to try this out before pulling the trigger just change mv to echo mv.
This one also worked fine
– Mithun Sreedharan
Nov 27 '09 at 7:03
add a comment |
up vote
4
down vote
What if you want to apply the replace task recursively? How would you do that?
Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)
#!/bin/bash
find . -type d | while read N
do
(
cd "$N"
if test "$?" = "0"
then
for file in *; do mv "$file" $file// /%20; done
fi
)
done
add a comment |
up vote
3
down vote
If you use bash:
for file in *; do mv "$file" $file// /_; done
when i tried, i got mv: when moving multiple files, last argument must be a directory Trymv --help' for more information. mv: when moving multiple files, last argument must be a directory Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:32
Should be$FILE/ /_
– soulmerge
Nov 27 '09 at 5:49
that only replace 1 space
– ghostdog74
Nov 27 '09 at 5:56
Again error mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 7:07
@levislevis85: Thx, didn't know that.
– soulmerge
Nov 27 '09 at 7:13
add a comment |
up vote
1
down vote
Quote your variables:
for file in *; do echo mv "'$file'" "$file// /_"; done
Remove the "echo" to do the actual rename.
It is echoing the mv commands prperly, but not really renaming the file!
– Mithun Sreedharan
Nov 27 '09 at 7:05
removing echo produces error like mv: cannot stat'1130 lake micigan view.jpg'': No such file or directory mv: cannot stat'1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat'1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat'1130_diningroom_table.jpg'': No such file or directory
– Mithun Sreedharan
Nov 27 '09 at 7:08
what is your OS?
– ghostdog74
Nov 27 '09 at 7:26
what shell are you using?
– ghostdog74
Nov 27 '09 at 7:27
Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
– Mithun Sreedharan
Nov 27 '09 at 11:54
add a comment |
up vote
1
down vote
Try something like this, assuming all of your files were .txt's:
for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done
2
got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:31
2
Agin error tr: too many arguments Trytr --help' for more information. mv: missing file argument Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 7:06
add a comment |
up vote
1
down vote
Here is another solution:
ls | awk 'printf(""%s"n", $0)' | sed 'p; s/ /_/g' | xargs -n2 mv
- uses awk to add quotes around the name of the file
- uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name
- xargs takes 2 lines at a time and passes it to mv
add a comment |
up vote
0
down vote
I believe your answer is in Replace spaces in filenames with underscores.
1
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– Bill the Lizard
Oct 27 '11 at 14:58
add a comment |
up vote
0
down vote
The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows
sed -i 's/s/_/g' *
Hope this helps.
add a comment |
up vote
0
down vote
To rename all the files with a .py extension use,
find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
Sample output,
$ find . -iname "*.py" -type f
./Sample File.py
./Sample/Sample File.py
$ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
$ find . -iname "*.py" -type f
./Sample/Sample_File.py
./Sample_File.py
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1806868%2flinux-replacing-spaces-in-the-file-names%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
129
down vote
accepted
This should do it:
for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename1 - foo.jpgand my folder already had1.jpgin it.
– byxor
Oct 18 '17 at 14:05
1
I find backticks bit hard to read when they are near quotes. The same but more readable would befor file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
– Kamil S Jaron
Jul 3 at 8:58
add a comment |
up vote
129
down vote
accepted
This should do it:
for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename1 - foo.jpgand my folder already had1.jpgin it.
– byxor
Oct 18 '17 at 14:05
1
I find backticks bit hard to read when they are near quotes. The same but more readable would befor file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
– Kamil S Jaron
Jul 3 at 8:58
add a comment |
up vote
129
down vote
accepted
up vote
129
down vote
accepted
This should do it:
for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
This should do it:
for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
edited Mar 30 '15 at 12:17
Peter Mortensen
13.4k1983111
13.4k1983111
answered Nov 27 '09 at 5:42
neesh
3,63252528
3,63252528
This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename1 - foo.jpgand my folder already had1.jpgin it.
– byxor
Oct 18 '17 at 14:05
1
I find backticks bit hard to read when they are near quotes. The same but more readable would befor file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
– Kamil S Jaron
Jul 3 at 8:58
add a comment |
This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename1 - foo.jpgand my folder already had1.jpgin it.
– byxor
Oct 18 '17 at 14:05
1
I find backticks bit hard to read when they are near quotes. The same but more readable would befor file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
– Kamil S Jaron
Jul 3 at 8:58
This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename
1 - foo.jpg and my folder already had 1.jpg in it.– byxor
Oct 18 '17 at 14:05
This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename
1 - foo.jpg and my folder already had 1.jpg in it.– byxor
Oct 18 '17 at 14:05
1
1
I find backticks bit hard to read when they are near quotes. The same but more readable would be
for file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done– Kamil S Jaron
Jul 3 at 8:58
I find backticks bit hard to read when they are near quotes. The same but more readable would be
for file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done– Kamil S Jaron
Jul 3 at 8:58
add a comment |
up vote
50
down vote
I prefer to use the command 'rename', which takes Perl-style regexes:
rename "s/ /_/g" *
You can do a dry run with the -n flag:
rename -n "s/ /_/g" *
1
this will work if you have the perl-style rename and not the simpler redhat/fedora one
– David Dean
Nov 27 '09 at 5:56
5
the fedora version would berename " " "_" *
– David Dean
Nov 27 '09 at 5:57
4
renamenot available in OSX it appears.
– Etienne Low-Décarie
Feb 12 '14 at 16:53
On macOS 10.12.3,renameis available.
– Sung Won Cho
Mar 14 '17 at 6:09
For some reasonrenamedoes not do a single thing on my CentOS box. Either with regex or not.
– Buttle Butkus
Jun 28 '17 at 6:32
|
show 4 more comments
up vote
50
down vote
I prefer to use the command 'rename', which takes Perl-style regexes:
rename "s/ /_/g" *
You can do a dry run with the -n flag:
rename -n "s/ /_/g" *
1
this will work if you have the perl-style rename and not the simpler redhat/fedora one
– David Dean
Nov 27 '09 at 5:56
5
the fedora version would berename " " "_" *
– David Dean
Nov 27 '09 at 5:57
4
renamenot available in OSX it appears.
– Etienne Low-Décarie
Feb 12 '14 at 16:53
On macOS 10.12.3,renameis available.
– Sung Won Cho
Mar 14 '17 at 6:09
For some reasonrenamedoes not do a single thing on my CentOS box. Either with regex or not.
– Buttle Butkus
Jun 28 '17 at 6:32
|
show 4 more comments
up vote
50
down vote
up vote
50
down vote
I prefer to use the command 'rename', which takes Perl-style regexes:
rename "s/ /_/g" *
You can do a dry run with the -n flag:
rename -n "s/ /_/g" *
I prefer to use the command 'rename', which takes Perl-style regexes:
rename "s/ /_/g" *
You can do a dry run with the -n flag:
rename -n "s/ /_/g" *
answered Nov 27 '09 at 5:45
DF.
53133
53133
1
this will work if you have the perl-style rename and not the simpler redhat/fedora one
– David Dean
Nov 27 '09 at 5:56
5
the fedora version would berename " " "_" *
– David Dean
Nov 27 '09 at 5:57
4
renamenot available in OSX it appears.
– Etienne Low-Décarie
Feb 12 '14 at 16:53
On macOS 10.12.3,renameis available.
– Sung Won Cho
Mar 14 '17 at 6:09
For some reasonrenamedoes not do a single thing on my CentOS box. Either with regex or not.
– Buttle Butkus
Jun 28 '17 at 6:32
|
show 4 more comments
1
this will work if you have the perl-style rename and not the simpler redhat/fedora one
– David Dean
Nov 27 '09 at 5:56
5
the fedora version would berename " " "_" *
– David Dean
Nov 27 '09 at 5:57
4
renamenot available in OSX it appears.
– Etienne Low-Décarie
Feb 12 '14 at 16:53
On macOS 10.12.3,renameis available.
– Sung Won Cho
Mar 14 '17 at 6:09
For some reasonrenamedoes not do a single thing on my CentOS box. Either with regex or not.
– Buttle Butkus
Jun 28 '17 at 6:32
1
1
this will work if you have the perl-style rename and not the simpler redhat/fedora one
– David Dean
Nov 27 '09 at 5:56
this will work if you have the perl-style rename and not the simpler redhat/fedora one
– David Dean
Nov 27 '09 at 5:56
5
5
the fedora version would be
rename " " "_" *– David Dean
Nov 27 '09 at 5:57
the fedora version would be
rename " " "_" *– David Dean
Nov 27 '09 at 5:57
4
4
rename not available in OSX it appears.– Etienne Low-Décarie
Feb 12 '14 at 16:53
rename not available in OSX it appears.– Etienne Low-Décarie
Feb 12 '14 at 16:53
On macOS 10.12.3,
rename is available.– Sung Won Cho
Mar 14 '17 at 6:09
On macOS 10.12.3,
rename is available.– Sung Won Cho
Mar 14 '17 at 6:09
For some reason
rename does not do a single thing on my CentOS box. Either with regex or not.– Buttle Butkus
Jun 28 '17 at 6:32
For some reason
rename does not do a single thing on my CentOS box. Either with regex or not.– Buttle Butkus
Jun 28 '17 at 6:32
|
show 4 more comments
up vote
9
down vote
Use sh...
for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done
If you want to try this out before pulling the trigger just change mv to echo mv.
This one also worked fine
– Mithun Sreedharan
Nov 27 '09 at 7:03
add a comment |
up vote
9
down vote
Use sh...
for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done
If you want to try this out before pulling the trigger just change mv to echo mv.
This one also worked fine
– Mithun Sreedharan
Nov 27 '09 at 7:03
add a comment |
up vote
9
down vote
up vote
9
down vote
Use sh...
for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done
If you want to try this out before pulling the trigger just change mv to echo mv.
Use sh...
for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done
If you want to try this out before pulling the trigger just change mv to echo mv.
answered Nov 27 '09 at 5:39
DigitalRoss
122k18206296
122k18206296
This one also worked fine
– Mithun Sreedharan
Nov 27 '09 at 7:03
add a comment |
This one also worked fine
– Mithun Sreedharan
Nov 27 '09 at 7:03
This one also worked fine
– Mithun Sreedharan
Nov 27 '09 at 7:03
This one also worked fine
– Mithun Sreedharan
Nov 27 '09 at 7:03
add a comment |
up vote
4
down vote
What if you want to apply the replace task recursively? How would you do that?
Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)
#!/bin/bash
find . -type d | while read N
do
(
cd "$N"
if test "$?" = "0"
then
for file in *; do mv "$file" $file// /%20; done
fi
)
done
add a comment |
up vote
4
down vote
What if you want to apply the replace task recursively? How would you do that?
Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)
#!/bin/bash
find . -type d | while read N
do
(
cd "$N"
if test "$?" = "0"
then
for file in *; do mv "$file" $file// /%20; done
fi
)
done
add a comment |
up vote
4
down vote
up vote
4
down vote
What if you want to apply the replace task recursively? How would you do that?
Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)
#!/bin/bash
find . -type d | while read N
do
(
cd "$N"
if test "$?" = "0"
then
for file in *; do mv "$file" $file// /%20; done
fi
)
done
What if you want to apply the replace task recursively? How would you do that?
Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)
#!/bin/bash
find . -type d | while read N
do
(
cd "$N"
if test "$?" = "0"
then
for file in *; do mv "$file" $file// /%20; done
fi
)
done
edited Apr 2 '10 at 21:08
answered Apr 2 '10 at 20:33
javipas
2061414
2061414
add a comment |
add a comment |
up vote
3
down vote
If you use bash:
for file in *; do mv "$file" $file// /_; done
when i tried, i got mv: when moving multiple files, last argument must be a directory Trymv --help' for more information. mv: when moving multiple files, last argument must be a directory Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:32
Should be$FILE/ /_
– soulmerge
Nov 27 '09 at 5:49
that only replace 1 space
– ghostdog74
Nov 27 '09 at 5:56
Again error mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 7:07
@levislevis85: Thx, didn't know that.
– soulmerge
Nov 27 '09 at 7:13
add a comment |
up vote
3
down vote
If you use bash:
for file in *; do mv "$file" $file// /_; done
when i tried, i got mv: when moving multiple files, last argument must be a directory Trymv --help' for more information. mv: when moving multiple files, last argument must be a directory Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:32
Should be$FILE/ /_
– soulmerge
Nov 27 '09 at 5:49
that only replace 1 space
– ghostdog74
Nov 27 '09 at 5:56
Again error mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 7:07
@levislevis85: Thx, didn't know that.
– soulmerge
Nov 27 '09 at 7:13
add a comment |
up vote
3
down vote
up vote
3
down vote
If you use bash:
for file in *; do mv "$file" $file// /_; done
If you use bash:
for file in *; do mv "$file" $file// /_; done
edited Nov 27 '09 at 5:44
answered Nov 27 '09 at 5:24
Murali VP
3,84112331
3,84112331
when i tried, i got mv: when moving multiple files, last argument must be a directory Trymv --help' for more information. mv: when moving multiple files, last argument must be a directory Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:32
Should be$FILE/ /_
– soulmerge
Nov 27 '09 at 5:49
that only replace 1 space
– ghostdog74
Nov 27 '09 at 5:56
Again error mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 7:07
@levislevis85: Thx, didn't know that.
– soulmerge
Nov 27 '09 at 7:13
add a comment |
when i tried, i got mv: when moving multiple files, last argument must be a directory Trymv --help' for more information. mv: when moving multiple files, last argument must be a directory Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:32
Should be$FILE/ /_
– soulmerge
Nov 27 '09 at 5:49
that only replace 1 space
– ghostdog74
Nov 27 '09 at 5:56
Again error mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information. mv: missing file argument Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 7:07
@levislevis85: Thx, didn't know that.
– soulmerge
Nov 27 '09 at 7:13
when i tried, i got mv: when moving multiple files, last argument must be a directory Try
mv --help' for more information. mv: when moving multiple files, last argument must be a directory Try mv --help' for more information.– Mithun Sreedharan
Nov 27 '09 at 5:32
when i tried, i got mv: when moving multiple files, last argument must be a directory Try
mv --help' for more information. mv: when moving multiple files, last argument must be a directory Try mv --help' for more information.– Mithun Sreedharan
Nov 27 '09 at 5:32
Should be
$FILE/ /_– soulmerge
Nov 27 '09 at 5:49
Should be
$FILE/ /_– soulmerge
Nov 27 '09 at 5:49
that only replace 1 space
– ghostdog74
Nov 27 '09 at 5:56
that only replace 1 space
– ghostdog74
Nov 27 '09 at 5:56
Again error mv: missing file argument Try
mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information.– Mithun Sreedharan
Nov 27 '09 at 7:07
Again error mv: missing file argument Try
mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information.– Mithun Sreedharan
Nov 27 '09 at 7:07
@levislevis85: Thx, didn't know that.
– soulmerge
Nov 27 '09 at 7:13
@levislevis85: Thx, didn't know that.
– soulmerge
Nov 27 '09 at 7:13
add a comment |
up vote
1
down vote
Quote your variables:
for file in *; do echo mv "'$file'" "$file// /_"; done
Remove the "echo" to do the actual rename.
It is echoing the mv commands prperly, but not really renaming the file!
– Mithun Sreedharan
Nov 27 '09 at 7:05
removing echo produces error like mv: cannot stat'1130 lake micigan view.jpg'': No such file or directory mv: cannot stat'1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat'1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat'1130_diningroom_table.jpg'': No such file or directory
– Mithun Sreedharan
Nov 27 '09 at 7:08
what is your OS?
– ghostdog74
Nov 27 '09 at 7:26
what shell are you using?
– ghostdog74
Nov 27 '09 at 7:27
Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
– Mithun Sreedharan
Nov 27 '09 at 11:54
add a comment |
up vote
1
down vote
Quote your variables:
for file in *; do echo mv "'$file'" "$file// /_"; done
Remove the "echo" to do the actual rename.
It is echoing the mv commands prperly, but not really renaming the file!
– Mithun Sreedharan
Nov 27 '09 at 7:05
removing echo produces error like mv: cannot stat'1130 lake micigan view.jpg'': No such file or directory mv: cannot stat'1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat'1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat'1130_diningroom_table.jpg'': No such file or directory
– Mithun Sreedharan
Nov 27 '09 at 7:08
what is your OS?
– ghostdog74
Nov 27 '09 at 7:26
what shell are you using?
– ghostdog74
Nov 27 '09 at 7:27
Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
– Mithun Sreedharan
Nov 27 '09 at 11:54
add a comment |
up vote
1
down vote
up vote
1
down vote
Quote your variables:
for file in *; do echo mv "'$file'" "$file// /_"; done
Remove the "echo" to do the actual rename.
Quote your variables:
for file in *; do echo mv "'$file'" "$file// /_"; done
Remove the "echo" to do the actual rename.
edited Mar 30 '15 at 12:17
Peter Mortensen
13.4k1983111
13.4k1983111
answered Nov 27 '09 at 5:42
ghostdog74
214k39210296
214k39210296
It is echoing the mv commands prperly, but not really renaming the file!
– Mithun Sreedharan
Nov 27 '09 at 7:05
removing echo produces error like mv: cannot stat'1130 lake micigan view.jpg'': No such file or directory mv: cannot stat'1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat'1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat'1130_diningroom_table.jpg'': No such file or directory
– Mithun Sreedharan
Nov 27 '09 at 7:08
what is your OS?
– ghostdog74
Nov 27 '09 at 7:26
what shell are you using?
– ghostdog74
Nov 27 '09 at 7:27
Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
– Mithun Sreedharan
Nov 27 '09 at 11:54
add a comment |
It is echoing the mv commands prperly, but not really renaming the file!
– Mithun Sreedharan
Nov 27 '09 at 7:05
removing echo produces error like mv: cannot stat'1130 lake micigan view.jpg'': No such file or directory mv: cannot stat'1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat'1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat'1130_diningroom_table.jpg'': No such file or directory
– Mithun Sreedharan
Nov 27 '09 at 7:08
what is your OS?
– ghostdog74
Nov 27 '09 at 7:26
what shell are you using?
– ghostdog74
Nov 27 '09 at 7:27
Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
– Mithun Sreedharan
Nov 27 '09 at 11:54
It is echoing the mv commands prperly, but not really renaming the file!
– Mithun Sreedharan
Nov 27 '09 at 7:05
It is echoing the mv commands prperly, but not really renaming the file!
– Mithun Sreedharan
Nov 27 '09 at 7:05
removing echo produces error like mv: cannot stat
'1130 lake micigan view.jpg'': No such file or directory mv: cannot stat '1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat '1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat '1130_diningroom_table.jpg'': No such file or directory– Mithun Sreedharan
Nov 27 '09 at 7:08
removing echo produces error like mv: cannot stat
'1130 lake micigan view.jpg'': No such file or directory mv: cannot stat '1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat '1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat '1130_diningroom_table.jpg'': No such file or directory– Mithun Sreedharan
Nov 27 '09 at 7:08
what is your OS?
– ghostdog74
Nov 27 '09 at 7:26
what is your OS?
– ghostdog74
Nov 27 '09 at 7:26
what shell are you using?
– ghostdog74
Nov 27 '09 at 7:27
what shell are you using?
– ghostdog74
Nov 27 '09 at 7:27
Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
– Mithun Sreedharan
Nov 27 '09 at 11:54
Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
– Mithun Sreedharan
Nov 27 '09 at 11:54
add a comment |
up vote
1
down vote
Try something like this, assuming all of your files were .txt's:
for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done
2
got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:31
2
Agin error tr: too many arguments Trytr --help' for more information. mv: missing file argument Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 7:06
add a comment |
up vote
1
down vote
Try something like this, assuming all of your files were .txt's:
for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done
2
got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:31
2
Agin error tr: too many arguments Trytr --help' for more information. mv: missing file argument Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 7:06
add a comment |
up vote
1
down vote
up vote
1
down vote
Try something like this, assuming all of your files were .txt's:
for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done
Try something like this, assuming all of your files were .txt's:
for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done
edited Apr 9 '15 at 15:00
Peter Mortensen
13.4k1983111
13.4k1983111
answered Nov 27 '09 at 5:21
Amir Afghani
28.6k1568112
28.6k1568112
2
got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:31
2
Agin error tr: too many arguments Trytr --help' for more information. mv: missing file argument Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 7:06
add a comment |
2
got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:31
2
Agin error tr: too many arguments Trytr --help' for more information. mv: missing file argument Trymv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 7:06
2
2
got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:31
got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
– Mithun Sreedharan
Nov 27 '09 at 5:31
2
2
Agin error tr: too many arguments Try
tr --help' for more information. mv: missing file argument Try mv --help' for more information.– Mithun Sreedharan
Nov 27 '09 at 7:06
Agin error tr: too many arguments Try
tr --help' for more information. mv: missing file argument Try mv --help' for more information.– Mithun Sreedharan
Nov 27 '09 at 7:06
add a comment |
up vote
1
down vote
Here is another solution:
ls | awk 'printf(""%s"n", $0)' | sed 'p; s/ /_/g' | xargs -n2 mv
- uses awk to add quotes around the name of the file
- uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name
- xargs takes 2 lines at a time and passes it to mv
add a comment |
up vote
1
down vote
Here is another solution:
ls | awk 'printf(""%s"n", $0)' | sed 'p; s/ /_/g' | xargs -n2 mv
- uses awk to add quotes around the name of the file
- uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name
- xargs takes 2 lines at a time and passes it to mv
add a comment |
up vote
1
down vote
up vote
1
down vote
Here is another solution:
ls | awk 'printf(""%s"n", $0)' | sed 'p; s/ /_/g' | xargs -n2 mv
- uses awk to add quotes around the name of the file
- uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name
- xargs takes 2 lines at a time and passes it to mv
Here is another solution:
ls | awk 'printf(""%s"n", $0)' | sed 'p; s/ /_/g' | xargs -n2 mv
- uses awk to add quotes around the name of the file
- uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name
- xargs takes 2 lines at a time and passes it to mv
answered Nov 9 at 13:51
yoogottamk
111
111
add a comment |
add a comment |
up vote
0
down vote
I believe your answer is in Replace spaces in filenames with underscores.
1
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– Bill the Lizard
Oct 27 '11 at 14:58
add a comment |
up vote
0
down vote
I believe your answer is in Replace spaces in filenames with underscores.
1
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– Bill the Lizard
Oct 27 '11 at 14:58
add a comment |
up vote
0
down vote
up vote
0
down vote
I believe your answer is in Replace spaces in filenames with underscores.
I believe your answer is in Replace spaces in filenames with underscores.
edited Apr 9 '15 at 14:59
Peter Mortensen
13.4k1983111
13.4k1983111
answered Nov 27 '09 at 5:18
Arthur Frankel
3,24532950
3,24532950
1
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– Bill the Lizard
Oct 27 '11 at 14:58
add a comment |
1
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– Bill the Lizard
Oct 27 '11 at 14:58
1
1
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– Bill the Lizard
Oct 27 '11 at 14:58
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– Bill the Lizard
Oct 27 '11 at 14:58
add a comment |
up vote
0
down vote
The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows
sed -i 's/s/_/g' *
Hope this helps.
add a comment |
up vote
0
down vote
The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows
sed -i 's/s/_/g' *
Hope this helps.
add a comment |
up vote
0
down vote
up vote
0
down vote
The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows
sed -i 's/s/_/g' *
Hope this helps.
The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows
sed -i 's/s/_/g' *
Hope this helps.
answered Dec 9 '16 at 13:03
Desta Haileselassie Hagos
9,23422838
9,23422838
add a comment |
add a comment |
up vote
0
down vote
To rename all the files with a .py extension use,
find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
Sample output,
$ find . -iname "*.py" -type f
./Sample File.py
./Sample/Sample File.py
$ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
$ find . -iname "*.py" -type f
./Sample/Sample_File.py
./Sample_File.py
add a comment |
up vote
0
down vote
To rename all the files with a .py extension use,
find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
Sample output,
$ find . -iname "*.py" -type f
./Sample File.py
./Sample/Sample File.py
$ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
$ find . -iname "*.py" -type f
./Sample/Sample_File.py
./Sample_File.py
add a comment |
up vote
0
down vote
up vote
0
down vote
To rename all the files with a .py extension use,
find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
Sample output,
$ find . -iname "*.py" -type f
./Sample File.py
./Sample/Sample File.py
$ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
$ find . -iname "*.py" -type f
./Sample/Sample_File.py
./Sample_File.py
To rename all the files with a .py extension use,
find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
Sample output,
$ find . -iname "*.py" -type f
./Sample File.py
./Sample/Sample File.py
$ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
$ find . -iname "*.py" -type f
./Sample/Sample_File.py
./Sample_File.py
answered Apr 7 at 9:21
akilesh raj
1221112
1221112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1806868%2flinux-replacing-spaces-in-the-file-names%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown