how to rename multiple files by replacing string in file name? this string contains a “#”Find all...
If a player tries to persuade somebody, what should that creature roll not to be persuaded?
Manually select/unselect lines before forwarding to stdout
Is it possible to cast two 9th level spells without taking a long rest in 5e?
Why are road bikes (not time trial bikes) used in many triathlons?
Video editor for YouTube
Why hasn't the U.S. government paid war reparations to any country it attacked?
What do these three diagonal lines that cross through three measures and both staves mean, and what are they called?
Clarification on defining FFT bin sizes
Can't update Ubuntu 18.04.2
When to ask for constructive criticism?
What is this old "lemon-squeezer" shaped pan
In Adventurers League, is there any way for an 5th-level wizard to gain heavy armor proficiency?
Why use null function instead of == [] to check for empty list in Haskell?
Should I be able to keep my company purchased standing desk when I leave my job?
Extension of trace on von Neumann subalgebra
Why does FFmpeg choose 10+20+20 ms instead of an even 16 ms for 60 fps GIF images?
Why do legislative committees exist?
I have accepted an internship offer. Should I inform companies I have applied to that have not gotten back to me yet?
Can both line and load go to same screw on a GFCI outlet?
Is it rude to refer to janitors as 'floor people'?
MQTT subscription topic match
Source of story about the Vilna Gaon and immigration policy
Can a pizza stone be fixed after soap has been used to clean it?
Was Willow's first magic display (blazing arrow through arm) actual magic, and if not, what's the trick?
how to rename multiple files by replacing string in file name? this string contains a “#”
Find all [filename].mp4 and rename [filename].audioHow do I get this find and rename command to work with subdirectories?How can I replace a string in a file(s)?how to rename files while copying?How to rename thousands of files efficiently?Rename multiple files in multiple directories to the name of the directory plus 1rename multiple files in multiple directories using Bash scriptingRename certain files with part of parent directory nameHow to rename multiple files by replacing string in file name?Using sed to replace the hexadecimal code for URL and to insert new SVG codes after SVG tag in all SVG filesComplex string replace - multiple files, multiple different strings, must include certain text
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
https://serverfault.com/questions/70939/how-to-replace-a-text-string-in-multiple-files-in-linux
https://serverfault.com/questions/228733/how-to-rename-multiple-files-by-replacing-word-in-file-name
https://serverfault.com/questions/212153/replace-string-in-files-with-certain-file-extension
https://serverfault.com/questions/33158/searching-a-number-of-files-for-a-string-in-linux
These mentioned articles have all answered my question. However none of them work for me. I suspect it is because the string I am trying to replace has a # in it. Is there a special way to address this?
I have image file that had an é replaced by #U00a9 during a site migration. These look like this:
Lucky-#U00a9NBC-80x60.jpg
Lucky-#U00a9NBC-125x125.jpg
Lucky-#U00a9NBC-150x150.jpg
Lucky-#U00a9NBC-250x250.jpg
Lucky-#U00a9NBC-282x232.jpg
Lucky-#U00a9NBC-300x150.jpg
Lucky-#U00a9NBC-300x200.jpg
Lucky-#U00a9NBC-300x250.jpg
Lucky-#U00a9NBC-360x240.jpg
Lucky-#U00a9NBC-400x250.jpg
Lucky-#U00a9NBC-430x270.jpg
Lucky-#U00a9NBC-480x240.jpg
Lucky-#U00a9NBC-600x240.jpg
Lucky-#U00a9NBC-600x250.jpg
Lucky-#U00a9NBC.jpg
and I want to change it to something like this:
Lucky-safeNBC-80x60.jpg
Lucky-safeNBC-125x125.jpg
Lucky-safeNBC-150x150.jpg
Lucky-safeNBC-250x250.jpg
Lucky-safeNBC-282x232.jpg
Lucky-safeNBC-300x150.jpg
Lucky-safeNBC-300x200.jpg
Lucky-safeNBC-300x250.jpg
Lucky-safeNBC-360x240.jpg
Lucky-safeNBC-400x250.jpg
Lucky-safeNBC-430x270.jpg
Lucky-safeNBC-480x240.jpg
Lucky-safeNBC-600x240.jpg
Lucky-safeNBC-600x250.jpg
Lucky-safeNBC.jpg
UPDATE:
These examples all start with "LU00a9ucky but here are many images with different names. I am simply targeting the "#U00a9" portion of the string to replace with "safe".
sed rename mv
migrated from serverfault.com Dec 20 '14 at 10:18
This question came from our site for system and network administrators.
add a comment |
https://serverfault.com/questions/70939/how-to-replace-a-text-string-in-multiple-files-in-linux
https://serverfault.com/questions/228733/how-to-rename-multiple-files-by-replacing-word-in-file-name
https://serverfault.com/questions/212153/replace-string-in-files-with-certain-file-extension
https://serverfault.com/questions/33158/searching-a-number-of-files-for-a-string-in-linux
These mentioned articles have all answered my question. However none of them work for me. I suspect it is because the string I am trying to replace has a # in it. Is there a special way to address this?
I have image file that had an é replaced by #U00a9 during a site migration. These look like this:
Lucky-#U00a9NBC-80x60.jpg
Lucky-#U00a9NBC-125x125.jpg
Lucky-#U00a9NBC-150x150.jpg
Lucky-#U00a9NBC-250x250.jpg
Lucky-#U00a9NBC-282x232.jpg
Lucky-#U00a9NBC-300x150.jpg
Lucky-#U00a9NBC-300x200.jpg
Lucky-#U00a9NBC-300x250.jpg
Lucky-#U00a9NBC-360x240.jpg
Lucky-#U00a9NBC-400x250.jpg
Lucky-#U00a9NBC-430x270.jpg
Lucky-#U00a9NBC-480x240.jpg
Lucky-#U00a9NBC-600x240.jpg
Lucky-#U00a9NBC-600x250.jpg
Lucky-#U00a9NBC.jpg
and I want to change it to something like this:
Lucky-safeNBC-80x60.jpg
Lucky-safeNBC-125x125.jpg
Lucky-safeNBC-150x150.jpg
Lucky-safeNBC-250x250.jpg
Lucky-safeNBC-282x232.jpg
Lucky-safeNBC-300x150.jpg
Lucky-safeNBC-300x200.jpg
Lucky-safeNBC-300x250.jpg
Lucky-safeNBC-360x240.jpg
Lucky-safeNBC-400x250.jpg
Lucky-safeNBC-430x270.jpg
Lucky-safeNBC-480x240.jpg
Lucky-safeNBC-600x240.jpg
Lucky-safeNBC-600x250.jpg
Lucky-safeNBC.jpg
UPDATE:
These examples all start with "LU00a9ucky but here are many images with different names. I am simply targeting the "#U00a9" portion of the string to replace with "safe".
sed rename mv
migrated from serverfault.com Dec 20 '14 at 10:18
This question came from our site for system and network administrators.
3
So what have you actually tried? I see that you have linked to a few questions and say they failed, but how did they fail? IMO the best example uses therename
command. I suspect your rename would be as simple asrename -n 's/#/safeNBC/' *.jpg
.
– Zoredache
Dec 19 '14 at 21:26
I triedrename -n 's/#U00a9/safe/' *.jpg
and the command was accepted but no changes occurred.
– Leon Francis Shelhamer
Dec 19 '14 at 23:36
Sure, as you would have seen from the documentation you surely reviewed, the-n
is theno act
option. Which lets you see if it works before you actually use it. Did the output on the screen show the potential new names correctly?
– Zoredache
Dec 19 '14 at 23:55
I apologize I copied and pasted your example without paying full attention, I did the rename command without the -n. I believe @DTK address the problem, I was not escaping the #.
– Leon Francis Shelhamer
Dec 20 '14 at 8:17
Replacing strings in filenames on MacOS: superuser.com/questions/152627/…
– Anton Tarasenko
Nov 26 '18 at 10:07
add a comment |
https://serverfault.com/questions/70939/how-to-replace-a-text-string-in-multiple-files-in-linux
https://serverfault.com/questions/228733/how-to-rename-multiple-files-by-replacing-word-in-file-name
https://serverfault.com/questions/212153/replace-string-in-files-with-certain-file-extension
https://serverfault.com/questions/33158/searching-a-number-of-files-for-a-string-in-linux
These mentioned articles have all answered my question. However none of them work for me. I suspect it is because the string I am trying to replace has a # in it. Is there a special way to address this?
I have image file that had an é replaced by #U00a9 during a site migration. These look like this:
Lucky-#U00a9NBC-80x60.jpg
Lucky-#U00a9NBC-125x125.jpg
Lucky-#U00a9NBC-150x150.jpg
Lucky-#U00a9NBC-250x250.jpg
Lucky-#U00a9NBC-282x232.jpg
Lucky-#U00a9NBC-300x150.jpg
Lucky-#U00a9NBC-300x200.jpg
Lucky-#U00a9NBC-300x250.jpg
Lucky-#U00a9NBC-360x240.jpg
Lucky-#U00a9NBC-400x250.jpg
Lucky-#U00a9NBC-430x270.jpg
Lucky-#U00a9NBC-480x240.jpg
Lucky-#U00a9NBC-600x240.jpg
Lucky-#U00a9NBC-600x250.jpg
Lucky-#U00a9NBC.jpg
and I want to change it to something like this:
Lucky-safeNBC-80x60.jpg
Lucky-safeNBC-125x125.jpg
Lucky-safeNBC-150x150.jpg
Lucky-safeNBC-250x250.jpg
Lucky-safeNBC-282x232.jpg
Lucky-safeNBC-300x150.jpg
Lucky-safeNBC-300x200.jpg
Lucky-safeNBC-300x250.jpg
Lucky-safeNBC-360x240.jpg
Lucky-safeNBC-400x250.jpg
Lucky-safeNBC-430x270.jpg
Lucky-safeNBC-480x240.jpg
Lucky-safeNBC-600x240.jpg
Lucky-safeNBC-600x250.jpg
Lucky-safeNBC.jpg
UPDATE:
These examples all start with "LU00a9ucky but here are many images with different names. I am simply targeting the "#U00a9" portion of the string to replace with "safe".
sed rename mv
https://serverfault.com/questions/70939/how-to-replace-a-text-string-in-multiple-files-in-linux
https://serverfault.com/questions/228733/how-to-rename-multiple-files-by-replacing-word-in-file-name
https://serverfault.com/questions/212153/replace-string-in-files-with-certain-file-extension
https://serverfault.com/questions/33158/searching-a-number-of-files-for-a-string-in-linux
These mentioned articles have all answered my question. However none of them work for me. I suspect it is because the string I am trying to replace has a # in it. Is there a special way to address this?
I have image file that had an é replaced by #U00a9 during a site migration. These look like this:
Lucky-#U00a9NBC-80x60.jpg
Lucky-#U00a9NBC-125x125.jpg
Lucky-#U00a9NBC-150x150.jpg
Lucky-#U00a9NBC-250x250.jpg
Lucky-#U00a9NBC-282x232.jpg
Lucky-#U00a9NBC-300x150.jpg
Lucky-#U00a9NBC-300x200.jpg
Lucky-#U00a9NBC-300x250.jpg
Lucky-#U00a9NBC-360x240.jpg
Lucky-#U00a9NBC-400x250.jpg
Lucky-#U00a9NBC-430x270.jpg
Lucky-#U00a9NBC-480x240.jpg
Lucky-#U00a9NBC-600x240.jpg
Lucky-#U00a9NBC-600x250.jpg
Lucky-#U00a9NBC.jpg
and I want to change it to something like this:
Lucky-safeNBC-80x60.jpg
Lucky-safeNBC-125x125.jpg
Lucky-safeNBC-150x150.jpg
Lucky-safeNBC-250x250.jpg
Lucky-safeNBC-282x232.jpg
Lucky-safeNBC-300x150.jpg
Lucky-safeNBC-300x200.jpg
Lucky-safeNBC-300x250.jpg
Lucky-safeNBC-360x240.jpg
Lucky-safeNBC-400x250.jpg
Lucky-safeNBC-430x270.jpg
Lucky-safeNBC-480x240.jpg
Lucky-safeNBC-600x240.jpg
Lucky-safeNBC-600x250.jpg
Lucky-safeNBC.jpg
UPDATE:
These examples all start with "LU00a9ucky but here are many images with different names. I am simply targeting the "#U00a9" portion of the string to replace with "safe".
sed rename mv
sed rename mv
edited Apr 13 '17 at 12:13
Community♦
1
1
asked Dec 19 '14 at 21:15
Leon Francis Shelhamer
migrated from serverfault.com Dec 20 '14 at 10:18
This question came from our site for system and network administrators.
migrated from serverfault.com Dec 20 '14 at 10:18
This question came from our site for system and network administrators.
3
So what have you actually tried? I see that you have linked to a few questions and say they failed, but how did they fail? IMO the best example uses therename
command. I suspect your rename would be as simple asrename -n 's/#/safeNBC/' *.jpg
.
– Zoredache
Dec 19 '14 at 21:26
I triedrename -n 's/#U00a9/safe/' *.jpg
and the command was accepted but no changes occurred.
– Leon Francis Shelhamer
Dec 19 '14 at 23:36
Sure, as you would have seen from the documentation you surely reviewed, the-n
is theno act
option. Which lets you see if it works before you actually use it. Did the output on the screen show the potential new names correctly?
– Zoredache
Dec 19 '14 at 23:55
I apologize I copied and pasted your example without paying full attention, I did the rename command without the -n. I believe @DTK address the problem, I was not escaping the #.
– Leon Francis Shelhamer
Dec 20 '14 at 8:17
Replacing strings in filenames on MacOS: superuser.com/questions/152627/…
– Anton Tarasenko
Nov 26 '18 at 10:07
add a comment |
3
So what have you actually tried? I see that you have linked to a few questions and say they failed, but how did they fail? IMO the best example uses therename
command. I suspect your rename would be as simple asrename -n 's/#/safeNBC/' *.jpg
.
– Zoredache
Dec 19 '14 at 21:26
I triedrename -n 's/#U00a9/safe/' *.jpg
and the command was accepted but no changes occurred.
– Leon Francis Shelhamer
Dec 19 '14 at 23:36
Sure, as you would have seen from the documentation you surely reviewed, the-n
is theno act
option. Which lets you see if it works before you actually use it. Did the output on the screen show the potential new names correctly?
– Zoredache
Dec 19 '14 at 23:55
I apologize I copied and pasted your example without paying full attention, I did the rename command without the -n. I believe @DTK address the problem, I was not escaping the #.
– Leon Francis Shelhamer
Dec 20 '14 at 8:17
Replacing strings in filenames on MacOS: superuser.com/questions/152627/…
– Anton Tarasenko
Nov 26 '18 at 10:07
3
3
So what have you actually tried? I see that you have linked to a few questions and say they failed, but how did they fail? IMO the best example uses the
rename
command. I suspect your rename would be as simple as rename -n 's/#/safeNBC/' *.jpg
.– Zoredache
Dec 19 '14 at 21:26
So what have you actually tried? I see that you have linked to a few questions and say they failed, but how did they fail? IMO the best example uses the
rename
command. I suspect your rename would be as simple as rename -n 's/#/safeNBC/' *.jpg
.– Zoredache
Dec 19 '14 at 21:26
I tried
rename -n 's/#U00a9/safe/' *.jpg
and the command was accepted but no changes occurred.– Leon Francis Shelhamer
Dec 19 '14 at 23:36
I tried
rename -n 's/#U00a9/safe/' *.jpg
and the command was accepted but no changes occurred.– Leon Francis Shelhamer
Dec 19 '14 at 23:36
Sure, as you would have seen from the documentation you surely reviewed, the
-n
is the no act
option. Which lets you see if it works before you actually use it. Did the output on the screen show the potential new names correctly?– Zoredache
Dec 19 '14 at 23:55
Sure, as you would have seen from the documentation you surely reviewed, the
-n
is the no act
option. Which lets you see if it works before you actually use it. Did the output on the screen show the potential new names correctly?– Zoredache
Dec 19 '14 at 23:55
I apologize I copied and pasted your example without paying full attention, I did the rename command without the -n. I believe @DTK address the problem, I was not escaping the #.
– Leon Francis Shelhamer
Dec 20 '14 at 8:17
I apologize I copied and pasted your example without paying full attention, I did the rename command without the -n. I believe @DTK address the problem, I was not escaping the #.
– Leon Francis Shelhamer
Dec 20 '14 at 8:17
Replacing strings in filenames on MacOS: superuser.com/questions/152627/…
– Anton Tarasenko
Nov 26 '18 at 10:07
Replacing strings in filenames on MacOS: superuser.com/questions/152627/…
– Anton Tarasenko
Nov 26 '18 at 10:07
add a comment |
9 Answers
9
active
oldest
votes
To replace #
by somethingelse
for filenames in the current directory (not recursive) you can use the GNU rename utility:
rename 's/#/somethingelse/' *
Characters like -
must be escaped with a .
For your case, you would want to use
rename 's/#U00a9/safe/g' *
Note that if you only want to operate on a certain selection of files, e.g., only *.jpg
, adjust the final input to match that selection:
rename 's/#U00a9/safe/g' *.jpg
To perform a test before actually changing filenames, use the -n
flag:
demo/> ls
Lucky-#U00a9NBC-125x125.jpg
Lucky-#U00a9NBC-150x150.jpg
demo/> rename -n 's/#U00a9/safe/g' *.jpg
rename(Lucky-#U00a9NBC-125x125.jpg, Lucky-safeNBC-125x125.jpg)
rename(Lucky-#U00a9NBC-150x150.jpg, Lucky-safeNBC-150x150.jpg)
For OS X, GNU rename can be installed using homebrew: brew install rename
.
This did not work on my machine (Arch Linux), but mik's answer did.
– marcvangend
Apr 21 '17 at 8:14
@marcvangend Which version ofrename
are you using?# sudo pacman -S perl-rename
will install the perl version, which is more powerful and might be enough to make this answer work for you.
– John Gowers
May 20 '17 at 19:40
@JohnGowers Thanks! I didn't realize there are two versions. On my system,rename --version
returnsrename from util-linux 2.29.2
so that is indeed not the perl version.
– marcvangend
May 20 '17 at 21:53
4
There are two commonrename
utilities but neither of them are developed by GNU: Debian-based distributions include arename
utility with their Perl package while Red Hat-based distributions use therename
utility from theutil-linux
from the Linux Kernel Organization. Your link is to therename
C function from the GNU standard library.
– Anthony Geoghegan
Jul 7 '17 at 9:16
In this answer, why do we have to write/g
inrename 's/#U00a9/safe/g' *
– Nikhil
Jul 9 '18 at 21:25
|
show 1 more comment
This is not hard, simply make sure to escape the octothorpe (#) in the name by prepending a reverse-slash ().
find . -type f -name 'Lucky-*' | while read FILE ; do
newfile="$(echo ${FILE} |sed -e 's/\#U00a9/safe/')" ;
mv "${FILE}" "${newfile}" ;
done
Your explanation makes sense, escaping the#
sounds like what I need. I do not see a backslash in your example. Should it look like this:s/#U00a9/safe/
– Leon Francis Shelhamer
Dec 19 '14 at 23:45
@LeonFrancisShelhamer good catch. It swallowed the backslash. I'll modify.
– DTK
Dec 20 '14 at 1:06
add a comment |
To escape #
from the shell, just use single quotes ('#'
), double quotes ("#"
), or backslash (#
).
The simplest in your case would be to use the rename
command (if it is available):
rename '#U00a9' safe *.jpg
2
Thanks. While the highest voted answer (rename 's/#/somethingelse/' *
) didn't work on my machine (Arch Linux), this one did.
– marcvangend
Apr 21 '17 at 8:13
1
This works using the rename command from util-linux.
– imclean
May 17 at 4:14
add a comment |
find the list of files and then replace keyword.
below is example
find . -name '*jpg' -exec bash -c ' mv $0 ${0/#U00a9NBC/safeNBC}' {} ;
1
You need some double-quotes around yourmv
arguments, in case there are spaces in the name
– OrangeDog
Oct 3 '18 at 12:52
add a comment |
not sure how to in sed, but you can try this in a bash shell:
for f in Lucky-#U00a9NBC-*.jpg; do mv -v "$f" "${f/#U00a9/safe}"; done;
explanation:
- loops through all file names matching the glob (Lucky-#U00a9NBC-*.jpg)
- renames file using the move command(mv)
- uses native bash parameter substitution ${var/Pattern/Replacement} to craft the new name ("${f/#U00a9/safe}")
More on parameter substitution (which is highly underutilized IMO): http://www.tldp.org/LDP/abs/html/parameter-substitution.html
While this code may answer the question, it isn’t very useful by itself. Explaining how it solves the problem would improve the usefulness and long-term value of the answer.
– Anthony Geoghegan
Sep 2 '16 at 10:16
added walkthrough explanation and further reading as per Anthony's comment.
– Gregory Patmore
Oct 5 '16 at 16:11
add a comment |
The above examples were not working on my system (CentOS 5.6) so I found a (possibly more system-specific) command that works (note: need to escape '#' with on command line):
rename #U00a9 safe *.jpg
[Also: I don't have enough reputation yet to comment, so in response to Nikhil's question regarding the use of /g
in rename 's/old_string/new_string/g'
(posed in the comments for another answer above):
Use the g
modifier to perform a 'global' substitution (that is, to substitute new_string for old_string as many times as old_string occurs). This shouldn't be necessary in my answer because the rename
will be applied to all files specified with *
. See https://www.computerhope.com/unix/rename.htm for a concise explanation of this and other modifiers.]
Correct; Fedora-derived OSes like CentOS have a different version ofrename
than Debian-derived OSes. See unix.stackexchange.com/a/238862/135943. And by the way, CentOS 5.6 is quite old and I recommend you upgrade.
– Wildcard
Aug 15 '18 at 19:23
Ah, thanks for the explanation + link. The outdated OS is at work and so it isn't up to me to upgrade :) but a new cluster is being built out and presumably we'll upgrade when we move over...
– Ezra Citron
Aug 17 '18 at 17:02
add a comment |
Here's DTK's solution wrapped in reusable bash function:
function renameFilesRecursively () {
SEARCH_PATH="$1"
SEARCH="$2"
REPLACE="$3"
find ${SEARCH_PATH} -type f -name "*${SEARCH}*" | while read FILENAME ; do
NEW_FILENAME="$(echo ${FILENAME} | sed -e "s/${SEARCH}/${REPLACE}/g")";
mv "${FILENAME}" "${NEW_FILENAME}";
done
}
Here's how you can use it:
renameFilesRecursively /home/user/my-files apple orange
add a comment |
Actually rename
has an option exactly for that called --subst
or -s
in short. No need to use the regex syntax.
rename -s '#U00a9' 'safe' *
If you want to replace/substitute multi occurrences, use --subst-all
or -S
.
BTW, I only wanted to replace a string by nothing (remove it from file name)... well we also have an option for it -d/--delete
and -D/--delete-all
:
rename -d '#U00a9' *
add a comment |
Another option is to use pyRenamer
, an application made specifically for batch renaming.
It can be installed with sudo apt-get install pyrenamer
For usage details, refer to its README file on GitHub.
1
If you really think this could answer the question please add some explanation to your answer. Of itself, it only installs a package. You should explain what the command does and provide an example that handles the OP's specific requirement.
– roaima
Sep 2 '16 at 10:02
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f175135%2fhow-to-rename-multiple-files-by-replacing-string-in-file-name-this-string-conta%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
To replace #
by somethingelse
for filenames in the current directory (not recursive) you can use the GNU rename utility:
rename 's/#/somethingelse/' *
Characters like -
must be escaped with a .
For your case, you would want to use
rename 's/#U00a9/safe/g' *
Note that if you only want to operate on a certain selection of files, e.g., only *.jpg
, adjust the final input to match that selection:
rename 's/#U00a9/safe/g' *.jpg
To perform a test before actually changing filenames, use the -n
flag:
demo/> ls
Lucky-#U00a9NBC-125x125.jpg
Lucky-#U00a9NBC-150x150.jpg
demo/> rename -n 's/#U00a9/safe/g' *.jpg
rename(Lucky-#U00a9NBC-125x125.jpg, Lucky-safeNBC-125x125.jpg)
rename(Lucky-#U00a9NBC-150x150.jpg, Lucky-safeNBC-150x150.jpg)
For OS X, GNU rename can be installed using homebrew: brew install rename
.
This did not work on my machine (Arch Linux), but mik's answer did.
– marcvangend
Apr 21 '17 at 8:14
@marcvangend Which version ofrename
are you using?# sudo pacman -S perl-rename
will install the perl version, which is more powerful and might be enough to make this answer work for you.
– John Gowers
May 20 '17 at 19:40
@JohnGowers Thanks! I didn't realize there are two versions. On my system,rename --version
returnsrename from util-linux 2.29.2
so that is indeed not the perl version.
– marcvangend
May 20 '17 at 21:53
4
There are two commonrename
utilities but neither of them are developed by GNU: Debian-based distributions include arename
utility with their Perl package while Red Hat-based distributions use therename
utility from theutil-linux
from the Linux Kernel Organization. Your link is to therename
C function from the GNU standard library.
– Anthony Geoghegan
Jul 7 '17 at 9:16
In this answer, why do we have to write/g
inrename 's/#U00a9/safe/g' *
– Nikhil
Jul 9 '18 at 21:25
|
show 1 more comment
To replace #
by somethingelse
for filenames in the current directory (not recursive) you can use the GNU rename utility:
rename 's/#/somethingelse/' *
Characters like -
must be escaped with a .
For your case, you would want to use
rename 's/#U00a9/safe/g' *
Note that if you only want to operate on a certain selection of files, e.g., only *.jpg
, adjust the final input to match that selection:
rename 's/#U00a9/safe/g' *.jpg
To perform a test before actually changing filenames, use the -n
flag:
demo/> ls
Lucky-#U00a9NBC-125x125.jpg
Lucky-#U00a9NBC-150x150.jpg
demo/> rename -n 's/#U00a9/safe/g' *.jpg
rename(Lucky-#U00a9NBC-125x125.jpg, Lucky-safeNBC-125x125.jpg)
rename(Lucky-#U00a9NBC-150x150.jpg, Lucky-safeNBC-150x150.jpg)
For OS X, GNU rename can be installed using homebrew: brew install rename
.
This did not work on my machine (Arch Linux), but mik's answer did.
– marcvangend
Apr 21 '17 at 8:14
@marcvangend Which version ofrename
are you using?# sudo pacman -S perl-rename
will install the perl version, which is more powerful and might be enough to make this answer work for you.
– John Gowers
May 20 '17 at 19:40
@JohnGowers Thanks! I didn't realize there are two versions. On my system,rename --version
returnsrename from util-linux 2.29.2
so that is indeed not the perl version.
– marcvangend
May 20 '17 at 21:53
4
There are two commonrename
utilities but neither of them are developed by GNU: Debian-based distributions include arename
utility with their Perl package while Red Hat-based distributions use therename
utility from theutil-linux
from the Linux Kernel Organization. Your link is to therename
C function from the GNU standard library.
– Anthony Geoghegan
Jul 7 '17 at 9:16
In this answer, why do we have to write/g
inrename 's/#U00a9/safe/g' *
– Nikhil
Jul 9 '18 at 21:25
|
show 1 more comment
To replace #
by somethingelse
for filenames in the current directory (not recursive) you can use the GNU rename utility:
rename 's/#/somethingelse/' *
Characters like -
must be escaped with a .
For your case, you would want to use
rename 's/#U00a9/safe/g' *
Note that if you only want to operate on a certain selection of files, e.g., only *.jpg
, adjust the final input to match that selection:
rename 's/#U00a9/safe/g' *.jpg
To perform a test before actually changing filenames, use the -n
flag:
demo/> ls
Lucky-#U00a9NBC-125x125.jpg
Lucky-#U00a9NBC-150x150.jpg
demo/> rename -n 's/#U00a9/safe/g' *.jpg
rename(Lucky-#U00a9NBC-125x125.jpg, Lucky-safeNBC-125x125.jpg)
rename(Lucky-#U00a9NBC-150x150.jpg, Lucky-safeNBC-150x150.jpg)
For OS X, GNU rename can be installed using homebrew: brew install rename
.
To replace #
by somethingelse
for filenames in the current directory (not recursive) you can use the GNU rename utility:
rename 's/#/somethingelse/' *
Characters like -
must be escaped with a .
For your case, you would want to use
rename 's/#U00a9/safe/g' *
Note that if you only want to operate on a certain selection of files, e.g., only *.jpg
, adjust the final input to match that selection:
rename 's/#U00a9/safe/g' *.jpg
To perform a test before actually changing filenames, use the -n
flag:
demo/> ls
Lucky-#U00a9NBC-125x125.jpg
Lucky-#U00a9NBC-150x150.jpg
demo/> rename -n 's/#U00a9/safe/g' *.jpg
rename(Lucky-#U00a9NBC-125x125.jpg, Lucky-safeNBC-125x125.jpg)
rename(Lucky-#U00a9NBC-150x150.jpg, Lucky-safeNBC-150x150.jpg)
For OS X, GNU rename can be installed using homebrew: brew install rename
.
edited Sep 27 '16 at 15:26
Steven C. Howell
2301 gold badge2 silver badges12 bronze badges
2301 gold badge2 silver badges12 bronze badges
answered Aug 28 '16 at 17:52
KrisWebDevKrisWebDev
7131 gold badge5 silver badges8 bronze badges
7131 gold badge5 silver badges8 bronze badges
This did not work on my machine (Arch Linux), but mik's answer did.
– marcvangend
Apr 21 '17 at 8:14
@marcvangend Which version ofrename
are you using?# sudo pacman -S perl-rename
will install the perl version, which is more powerful and might be enough to make this answer work for you.
– John Gowers
May 20 '17 at 19:40
@JohnGowers Thanks! I didn't realize there are two versions. On my system,rename --version
returnsrename from util-linux 2.29.2
so that is indeed not the perl version.
– marcvangend
May 20 '17 at 21:53
4
There are two commonrename
utilities but neither of them are developed by GNU: Debian-based distributions include arename
utility with their Perl package while Red Hat-based distributions use therename
utility from theutil-linux
from the Linux Kernel Organization. Your link is to therename
C function from the GNU standard library.
– Anthony Geoghegan
Jul 7 '17 at 9:16
In this answer, why do we have to write/g
inrename 's/#U00a9/safe/g' *
– Nikhil
Jul 9 '18 at 21:25
|
show 1 more comment
This did not work on my machine (Arch Linux), but mik's answer did.
– marcvangend
Apr 21 '17 at 8:14
@marcvangend Which version ofrename
are you using?# sudo pacman -S perl-rename
will install the perl version, which is more powerful and might be enough to make this answer work for you.
– John Gowers
May 20 '17 at 19:40
@JohnGowers Thanks! I didn't realize there are two versions. On my system,rename --version
returnsrename from util-linux 2.29.2
so that is indeed not the perl version.
– marcvangend
May 20 '17 at 21:53
4
There are two commonrename
utilities but neither of them are developed by GNU: Debian-based distributions include arename
utility with their Perl package while Red Hat-based distributions use therename
utility from theutil-linux
from the Linux Kernel Organization. Your link is to therename
C function from the GNU standard library.
– Anthony Geoghegan
Jul 7 '17 at 9:16
In this answer, why do we have to write/g
inrename 's/#U00a9/safe/g' *
– Nikhil
Jul 9 '18 at 21:25
This did not work on my machine (Arch Linux), but mik's answer did.
– marcvangend
Apr 21 '17 at 8:14
This did not work on my machine (Arch Linux), but mik's answer did.
– marcvangend
Apr 21 '17 at 8:14
@marcvangend Which version of
rename
are you using? # sudo pacman -S perl-rename
will install the perl version, which is more powerful and might be enough to make this answer work for you.– John Gowers
May 20 '17 at 19:40
@marcvangend Which version of
rename
are you using? # sudo pacman -S perl-rename
will install the perl version, which is more powerful and might be enough to make this answer work for you.– John Gowers
May 20 '17 at 19:40
@JohnGowers Thanks! I didn't realize there are two versions. On my system,
rename --version
returns rename from util-linux 2.29.2
so that is indeed not the perl version.– marcvangend
May 20 '17 at 21:53
@JohnGowers Thanks! I didn't realize there are two versions. On my system,
rename --version
returns rename from util-linux 2.29.2
so that is indeed not the perl version.– marcvangend
May 20 '17 at 21:53
4
4
There are two common
rename
utilities but neither of them are developed by GNU: Debian-based distributions include a rename
utility with their Perl package while Red Hat-based distributions use the rename
utility from the util-linux
from the Linux Kernel Organization. Your link is to the rename
C function from the GNU standard library.– Anthony Geoghegan
Jul 7 '17 at 9:16
There are two common
rename
utilities but neither of them are developed by GNU: Debian-based distributions include a rename
utility with their Perl package while Red Hat-based distributions use the rename
utility from the util-linux
from the Linux Kernel Organization. Your link is to the rename
C function from the GNU standard library.– Anthony Geoghegan
Jul 7 '17 at 9:16
In this answer, why do we have to write
/g
in rename 's/#U00a9/safe/g' *
– Nikhil
Jul 9 '18 at 21:25
In this answer, why do we have to write
/g
in rename 's/#U00a9/safe/g' *
– Nikhil
Jul 9 '18 at 21:25
|
show 1 more comment
This is not hard, simply make sure to escape the octothorpe (#) in the name by prepending a reverse-slash ().
find . -type f -name 'Lucky-*' | while read FILE ; do
newfile="$(echo ${FILE} |sed -e 's/\#U00a9/safe/')" ;
mv "${FILE}" "${newfile}" ;
done
Your explanation makes sense, escaping the#
sounds like what I need. I do not see a backslash in your example. Should it look like this:s/#U00a9/safe/
– Leon Francis Shelhamer
Dec 19 '14 at 23:45
@LeonFrancisShelhamer good catch. It swallowed the backslash. I'll modify.
– DTK
Dec 20 '14 at 1:06
add a comment |
This is not hard, simply make sure to escape the octothorpe (#) in the name by prepending a reverse-slash ().
find . -type f -name 'Lucky-*' | while read FILE ; do
newfile="$(echo ${FILE} |sed -e 's/\#U00a9/safe/')" ;
mv "${FILE}" "${newfile}" ;
done
Your explanation makes sense, escaping the#
sounds like what I need. I do not see a backslash in your example. Should it look like this:s/#U00a9/safe/
– Leon Francis Shelhamer
Dec 19 '14 at 23:45
@LeonFrancisShelhamer good catch. It swallowed the backslash. I'll modify.
– DTK
Dec 20 '14 at 1:06
add a comment |
This is not hard, simply make sure to escape the octothorpe (#) in the name by prepending a reverse-slash ().
find . -type f -name 'Lucky-*' | while read FILE ; do
newfile="$(echo ${FILE} |sed -e 's/\#U00a9/safe/')" ;
mv "${FILE}" "${newfile}" ;
done
This is not hard, simply make sure to escape the octothorpe (#) in the name by prepending a reverse-slash ().
find . -type f -name 'Lucky-*' | while read FILE ; do
newfile="$(echo ${FILE} |sed -e 's/\#U00a9/safe/')" ;
mv "${FILE}" "${newfile}" ;
done
edited Jun 3 '16 at 14:46
Community♦
1
1
answered Dec 19 '14 at 22:10
DTK
Your explanation makes sense, escaping the#
sounds like what I need. I do not see a backslash in your example. Should it look like this:s/#U00a9/safe/
– Leon Francis Shelhamer
Dec 19 '14 at 23:45
@LeonFrancisShelhamer good catch. It swallowed the backslash. I'll modify.
– DTK
Dec 20 '14 at 1:06
add a comment |
Your explanation makes sense, escaping the#
sounds like what I need. I do not see a backslash in your example. Should it look like this:s/#U00a9/safe/
– Leon Francis Shelhamer
Dec 19 '14 at 23:45
@LeonFrancisShelhamer good catch. It swallowed the backslash. I'll modify.
– DTK
Dec 20 '14 at 1:06
Your explanation makes sense, escaping the
#
sounds like what I need. I do not see a backslash in your example. Should it look like this: s/#U00a9/safe/
– Leon Francis Shelhamer
Dec 19 '14 at 23:45
Your explanation makes sense, escaping the
#
sounds like what I need. I do not see a backslash in your example. Should it look like this: s/#U00a9/safe/
– Leon Francis Shelhamer
Dec 19 '14 at 23:45
@LeonFrancisShelhamer good catch. It swallowed the backslash. I'll modify.
– DTK
Dec 20 '14 at 1:06
@LeonFrancisShelhamer good catch. It swallowed the backslash. I'll modify.
– DTK
Dec 20 '14 at 1:06
add a comment |
To escape #
from the shell, just use single quotes ('#'
), double quotes ("#"
), or backslash (#
).
The simplest in your case would be to use the rename
command (if it is available):
rename '#U00a9' safe *.jpg
2
Thanks. While the highest voted answer (rename 's/#/somethingelse/' *
) didn't work on my machine (Arch Linux), this one did.
– marcvangend
Apr 21 '17 at 8:13
1
This works using the rename command from util-linux.
– imclean
May 17 at 4:14
add a comment |
To escape #
from the shell, just use single quotes ('#'
), double quotes ("#"
), or backslash (#
).
The simplest in your case would be to use the rename
command (if it is available):
rename '#U00a9' safe *.jpg
2
Thanks. While the highest voted answer (rename 's/#/somethingelse/' *
) didn't work on my machine (Arch Linux), this one did.
– marcvangend
Apr 21 '17 at 8:13
1
This works using the rename command from util-linux.
– imclean
May 17 at 4:14
add a comment |
To escape #
from the shell, just use single quotes ('#'
), double quotes ("#"
), or backslash (#
).
The simplest in your case would be to use the rename
command (if it is available):
rename '#U00a9' safe *.jpg
To escape #
from the shell, just use single quotes ('#'
), double quotes ("#"
), or backslash (#
).
The simplest in your case would be to use the rename
command (if it is available):
rename '#U00a9' safe *.jpg
answered Jun 3 '16 at 15:03
mikmik
9307 silver badges15 bronze badges
9307 silver badges15 bronze badges
2
Thanks. While the highest voted answer (rename 's/#/somethingelse/' *
) didn't work on my machine (Arch Linux), this one did.
– marcvangend
Apr 21 '17 at 8:13
1
This works using the rename command from util-linux.
– imclean
May 17 at 4:14
add a comment |
2
Thanks. While the highest voted answer (rename 's/#/somethingelse/' *
) didn't work on my machine (Arch Linux), this one did.
– marcvangend
Apr 21 '17 at 8:13
1
This works using the rename command from util-linux.
– imclean
May 17 at 4:14
2
2
Thanks. While the highest voted answer (
rename 's/#/somethingelse/' *
) didn't work on my machine (Arch Linux), this one did.– marcvangend
Apr 21 '17 at 8:13
Thanks. While the highest voted answer (
rename 's/#/somethingelse/' *
) didn't work on my machine (Arch Linux), this one did.– marcvangend
Apr 21 '17 at 8:13
1
1
This works using the rename command from util-linux.
– imclean
May 17 at 4:14
This works using the rename command from util-linux.
– imclean
May 17 at 4:14
add a comment |
find the list of files and then replace keyword.
below is example
find . -name '*jpg' -exec bash -c ' mv $0 ${0/#U00a9NBC/safeNBC}' {} ;
1
You need some double-quotes around yourmv
arguments, in case there are spaces in the name
– OrangeDog
Oct 3 '18 at 12:52
add a comment |
find the list of files and then replace keyword.
below is example
find . -name '*jpg' -exec bash -c ' mv $0 ${0/#U00a9NBC/safeNBC}' {} ;
1
You need some double-quotes around yourmv
arguments, in case there are spaces in the name
– OrangeDog
Oct 3 '18 at 12:52
add a comment |
find the list of files and then replace keyword.
below is example
find . -name '*jpg' -exec bash -c ' mv $0 ${0/#U00a9NBC/safeNBC}' {} ;
find the list of files and then replace keyword.
below is example
find . -name '*jpg' -exec bash -c ' mv $0 ${0/#U00a9NBC/safeNBC}' {} ;
answered Jul 7 '17 at 8:01
Sujit DhamaleSujit Dhamale
2312 silver badges4 bronze badges
2312 silver badges4 bronze badges
1
You need some double-quotes around yourmv
arguments, in case there are spaces in the name
– OrangeDog
Oct 3 '18 at 12:52
add a comment |
1
You need some double-quotes around yourmv
arguments, in case there are spaces in the name
– OrangeDog
Oct 3 '18 at 12:52
1
1
You need some double-quotes around your
mv
arguments, in case there are spaces in the name– OrangeDog
Oct 3 '18 at 12:52
You need some double-quotes around your
mv
arguments, in case there are spaces in the name– OrangeDog
Oct 3 '18 at 12:52
add a comment |
not sure how to in sed, but you can try this in a bash shell:
for f in Lucky-#U00a9NBC-*.jpg; do mv -v "$f" "${f/#U00a9/safe}"; done;
explanation:
- loops through all file names matching the glob (Lucky-#U00a9NBC-*.jpg)
- renames file using the move command(mv)
- uses native bash parameter substitution ${var/Pattern/Replacement} to craft the new name ("${f/#U00a9/safe}")
More on parameter substitution (which is highly underutilized IMO): http://www.tldp.org/LDP/abs/html/parameter-substitution.html
While this code may answer the question, it isn’t very useful by itself. Explaining how it solves the problem would improve the usefulness and long-term value of the answer.
– Anthony Geoghegan
Sep 2 '16 at 10:16
added walkthrough explanation and further reading as per Anthony's comment.
– Gregory Patmore
Oct 5 '16 at 16:11
add a comment |
not sure how to in sed, but you can try this in a bash shell:
for f in Lucky-#U00a9NBC-*.jpg; do mv -v "$f" "${f/#U00a9/safe}"; done;
explanation:
- loops through all file names matching the glob (Lucky-#U00a9NBC-*.jpg)
- renames file using the move command(mv)
- uses native bash parameter substitution ${var/Pattern/Replacement} to craft the new name ("${f/#U00a9/safe}")
More on parameter substitution (which is highly underutilized IMO): http://www.tldp.org/LDP/abs/html/parameter-substitution.html
While this code may answer the question, it isn’t very useful by itself. Explaining how it solves the problem would improve the usefulness and long-term value of the answer.
– Anthony Geoghegan
Sep 2 '16 at 10:16
added walkthrough explanation and further reading as per Anthony's comment.
– Gregory Patmore
Oct 5 '16 at 16:11
add a comment |
not sure how to in sed, but you can try this in a bash shell:
for f in Lucky-#U00a9NBC-*.jpg; do mv -v "$f" "${f/#U00a9/safe}"; done;
explanation:
- loops through all file names matching the glob (Lucky-#U00a9NBC-*.jpg)
- renames file using the move command(mv)
- uses native bash parameter substitution ${var/Pattern/Replacement} to craft the new name ("${f/#U00a9/safe}")
More on parameter substitution (which is highly underutilized IMO): http://www.tldp.org/LDP/abs/html/parameter-substitution.html
not sure how to in sed, but you can try this in a bash shell:
for f in Lucky-#U00a9NBC-*.jpg; do mv -v "$f" "${f/#U00a9/safe}"; done;
explanation:
- loops through all file names matching the glob (Lucky-#U00a9NBC-*.jpg)
- renames file using the move command(mv)
- uses native bash parameter substitution ${var/Pattern/Replacement} to craft the new name ("${f/#U00a9/safe}")
More on parameter substitution (which is highly underutilized IMO): http://www.tldp.org/LDP/abs/html/parameter-substitution.html
edited Oct 5 '16 at 16:10
answered Dec 19 '14 at 22:09
Gregory PatmoreGregory Patmore
1913 bronze badges
1913 bronze badges
While this code may answer the question, it isn’t very useful by itself. Explaining how it solves the problem would improve the usefulness and long-term value of the answer.
– Anthony Geoghegan
Sep 2 '16 at 10:16
added walkthrough explanation and further reading as per Anthony's comment.
– Gregory Patmore
Oct 5 '16 at 16:11
add a comment |
While this code may answer the question, it isn’t very useful by itself. Explaining how it solves the problem would improve the usefulness and long-term value of the answer.
– Anthony Geoghegan
Sep 2 '16 at 10:16
added walkthrough explanation and further reading as per Anthony's comment.
– Gregory Patmore
Oct 5 '16 at 16:11
While this code may answer the question, it isn’t very useful by itself. Explaining how it solves the problem would improve the usefulness and long-term value of the answer.
– Anthony Geoghegan
Sep 2 '16 at 10:16
While this code may answer the question, it isn’t very useful by itself. Explaining how it solves the problem would improve the usefulness and long-term value of the answer.
– Anthony Geoghegan
Sep 2 '16 at 10:16
added walkthrough explanation and further reading as per Anthony's comment.
– Gregory Patmore
Oct 5 '16 at 16:11
added walkthrough explanation and further reading as per Anthony's comment.
– Gregory Patmore
Oct 5 '16 at 16:11
add a comment |
The above examples were not working on my system (CentOS 5.6) so I found a (possibly more system-specific) command that works (note: need to escape '#' with on command line):
rename #U00a9 safe *.jpg
[Also: I don't have enough reputation yet to comment, so in response to Nikhil's question regarding the use of /g
in rename 's/old_string/new_string/g'
(posed in the comments for another answer above):
Use the g
modifier to perform a 'global' substitution (that is, to substitute new_string for old_string as many times as old_string occurs). This shouldn't be necessary in my answer because the rename
will be applied to all files specified with *
. See https://www.computerhope.com/unix/rename.htm for a concise explanation of this and other modifiers.]
Correct; Fedora-derived OSes like CentOS have a different version ofrename
than Debian-derived OSes. See unix.stackexchange.com/a/238862/135943. And by the way, CentOS 5.6 is quite old and I recommend you upgrade.
– Wildcard
Aug 15 '18 at 19:23
Ah, thanks for the explanation + link. The outdated OS is at work and so it isn't up to me to upgrade :) but a new cluster is being built out and presumably we'll upgrade when we move over...
– Ezra Citron
Aug 17 '18 at 17:02
add a comment |
The above examples were not working on my system (CentOS 5.6) so I found a (possibly more system-specific) command that works (note: need to escape '#' with on command line):
rename #U00a9 safe *.jpg
[Also: I don't have enough reputation yet to comment, so in response to Nikhil's question regarding the use of /g
in rename 's/old_string/new_string/g'
(posed in the comments for another answer above):
Use the g
modifier to perform a 'global' substitution (that is, to substitute new_string for old_string as many times as old_string occurs). This shouldn't be necessary in my answer because the rename
will be applied to all files specified with *
. See https://www.computerhope.com/unix/rename.htm for a concise explanation of this and other modifiers.]
Correct; Fedora-derived OSes like CentOS have a different version ofrename
than Debian-derived OSes. See unix.stackexchange.com/a/238862/135943. And by the way, CentOS 5.6 is quite old and I recommend you upgrade.
– Wildcard
Aug 15 '18 at 19:23
Ah, thanks for the explanation + link. The outdated OS is at work and so it isn't up to me to upgrade :) but a new cluster is being built out and presumably we'll upgrade when we move over...
– Ezra Citron
Aug 17 '18 at 17:02
add a comment |
The above examples were not working on my system (CentOS 5.6) so I found a (possibly more system-specific) command that works (note: need to escape '#' with on command line):
rename #U00a9 safe *.jpg
[Also: I don't have enough reputation yet to comment, so in response to Nikhil's question regarding the use of /g
in rename 's/old_string/new_string/g'
(posed in the comments for another answer above):
Use the g
modifier to perform a 'global' substitution (that is, to substitute new_string for old_string as many times as old_string occurs). This shouldn't be necessary in my answer because the rename
will be applied to all files specified with *
. See https://www.computerhope.com/unix/rename.htm for a concise explanation of this and other modifiers.]
The above examples were not working on my system (CentOS 5.6) so I found a (possibly more system-specific) command that works (note: need to escape '#' with on command line):
rename #U00a9 safe *.jpg
[Also: I don't have enough reputation yet to comment, so in response to Nikhil's question regarding the use of /g
in rename 's/old_string/new_string/g'
(posed in the comments for another answer above):
Use the g
modifier to perform a 'global' substitution (that is, to substitute new_string for old_string as many times as old_string occurs). This shouldn't be necessary in my answer because the rename
will be applied to all files specified with *
. See https://www.computerhope.com/unix/rename.htm for a concise explanation of this and other modifiers.]
answered Aug 15 '18 at 18:36
Ezra CitronEzra Citron
111 bronze badge
111 bronze badge
Correct; Fedora-derived OSes like CentOS have a different version ofrename
than Debian-derived OSes. See unix.stackexchange.com/a/238862/135943. And by the way, CentOS 5.6 is quite old and I recommend you upgrade.
– Wildcard
Aug 15 '18 at 19:23
Ah, thanks for the explanation + link. The outdated OS is at work and so it isn't up to me to upgrade :) but a new cluster is being built out and presumably we'll upgrade when we move over...
– Ezra Citron
Aug 17 '18 at 17:02
add a comment |
Correct; Fedora-derived OSes like CentOS have a different version ofrename
than Debian-derived OSes. See unix.stackexchange.com/a/238862/135943. And by the way, CentOS 5.6 is quite old and I recommend you upgrade.
– Wildcard
Aug 15 '18 at 19:23
Ah, thanks for the explanation + link. The outdated OS is at work and so it isn't up to me to upgrade :) but a new cluster is being built out and presumably we'll upgrade when we move over...
– Ezra Citron
Aug 17 '18 at 17:02
Correct; Fedora-derived OSes like CentOS have a different version of
rename
than Debian-derived OSes. See unix.stackexchange.com/a/238862/135943. And by the way, CentOS 5.6 is quite old and I recommend you upgrade.– Wildcard
Aug 15 '18 at 19:23
Correct; Fedora-derived OSes like CentOS have a different version of
rename
than Debian-derived OSes. See unix.stackexchange.com/a/238862/135943. And by the way, CentOS 5.6 is quite old and I recommend you upgrade.– Wildcard
Aug 15 '18 at 19:23
Ah, thanks for the explanation + link. The outdated OS is at work and so it isn't up to me to upgrade :) but a new cluster is being built out and presumably we'll upgrade when we move over...
– Ezra Citron
Aug 17 '18 at 17:02
Ah, thanks for the explanation + link. The outdated OS is at work and so it isn't up to me to upgrade :) but a new cluster is being built out and presumably we'll upgrade when we move over...
– Ezra Citron
Aug 17 '18 at 17:02
add a comment |
Here's DTK's solution wrapped in reusable bash function:
function renameFilesRecursively () {
SEARCH_PATH="$1"
SEARCH="$2"
REPLACE="$3"
find ${SEARCH_PATH} -type f -name "*${SEARCH}*" | while read FILENAME ; do
NEW_FILENAME="$(echo ${FILENAME} | sed -e "s/${SEARCH}/${REPLACE}/g")";
mv "${FILENAME}" "${NEW_FILENAME}";
done
}
Here's how you can use it:
renameFilesRecursively /home/user/my-files apple orange
add a comment |
Here's DTK's solution wrapped in reusable bash function:
function renameFilesRecursively () {
SEARCH_PATH="$1"
SEARCH="$2"
REPLACE="$3"
find ${SEARCH_PATH} -type f -name "*${SEARCH}*" | while read FILENAME ; do
NEW_FILENAME="$(echo ${FILENAME} | sed -e "s/${SEARCH}/${REPLACE}/g")";
mv "${FILENAME}" "${NEW_FILENAME}";
done
}
Here's how you can use it:
renameFilesRecursively /home/user/my-files apple orange
add a comment |
Here's DTK's solution wrapped in reusable bash function:
function renameFilesRecursively () {
SEARCH_PATH="$1"
SEARCH="$2"
REPLACE="$3"
find ${SEARCH_PATH} -type f -name "*${SEARCH}*" | while read FILENAME ; do
NEW_FILENAME="$(echo ${FILENAME} | sed -e "s/${SEARCH}/${REPLACE}/g")";
mv "${FILENAME}" "${NEW_FILENAME}";
done
}
Here's how you can use it:
renameFilesRecursively /home/user/my-files apple orange
Here's DTK's solution wrapped in reusable bash function:
function renameFilesRecursively () {
SEARCH_PATH="$1"
SEARCH="$2"
REPLACE="$3"
find ${SEARCH_PATH} -type f -name "*${SEARCH}*" | while read FILENAME ; do
NEW_FILENAME="$(echo ${FILENAME} | sed -e "s/${SEARCH}/${REPLACE}/g")";
mv "${FILENAME}" "${NEW_FILENAME}";
done
}
Here's how you can use it:
renameFilesRecursively /home/user/my-files apple orange
answered Mar 20 at 13:10
Slava Fomin IISlava Fomin II
1012 bronze badges
1012 bronze badges
add a comment |
add a comment |
Actually rename
has an option exactly for that called --subst
or -s
in short. No need to use the regex syntax.
rename -s '#U00a9' 'safe' *
If you want to replace/substitute multi occurrences, use --subst-all
or -S
.
BTW, I only wanted to replace a string by nothing (remove it from file name)... well we also have an option for it -d/--delete
and -D/--delete-all
:
rename -d '#U00a9' *
add a comment |
Actually rename
has an option exactly for that called --subst
or -s
in short. No need to use the regex syntax.
rename -s '#U00a9' 'safe' *
If you want to replace/substitute multi occurrences, use --subst-all
or -S
.
BTW, I only wanted to replace a string by nothing (remove it from file name)... well we also have an option for it -d/--delete
and -D/--delete-all
:
rename -d '#U00a9' *
add a comment |
Actually rename
has an option exactly for that called --subst
or -s
in short. No need to use the regex syntax.
rename -s '#U00a9' 'safe' *
If you want to replace/substitute multi occurrences, use --subst-all
or -S
.
BTW, I only wanted to replace a string by nothing (remove it from file name)... well we also have an option for it -d/--delete
and -D/--delete-all
:
rename -d '#U00a9' *
Actually rename
has an option exactly for that called --subst
or -s
in short. No need to use the regex syntax.
rename -s '#U00a9' 'safe' *
If you want to replace/substitute multi occurrences, use --subst-all
or -S
.
BTW, I only wanted to replace a string by nothing (remove it from file name)... well we also have an option for it -d/--delete
and -D/--delete-all
:
rename -d '#U00a9' *
answered 23 mins ago
lapinlapin
1551 silver badge6 bronze badges
1551 silver badge6 bronze badges
add a comment |
add a comment |
Another option is to use pyRenamer
, an application made specifically for batch renaming.
It can be installed with sudo apt-get install pyrenamer
For usage details, refer to its README file on GitHub.
1
If you really think this could answer the question please add some explanation to your answer. Of itself, it only installs a package. You should explain what the command does and provide an example that handles the OP's specific requirement.
– roaima
Sep 2 '16 at 10:02
add a comment |
Another option is to use pyRenamer
, an application made specifically for batch renaming.
It can be installed with sudo apt-get install pyrenamer
For usage details, refer to its README file on GitHub.
1
If you really think this could answer the question please add some explanation to your answer. Of itself, it only installs a package. You should explain what the command does and provide an example that handles the OP's specific requirement.
– roaima
Sep 2 '16 at 10:02
add a comment |
Another option is to use pyRenamer
, an application made specifically for batch renaming.
It can be installed with sudo apt-get install pyrenamer
For usage details, refer to its README file on GitHub.
Another option is to use pyRenamer
, an application made specifically for batch renaming.
It can be installed with sudo apt-get install pyrenamer
For usage details, refer to its README file on GitHub.
edited Sep 2 '16 at 15:37
Julie Pelletier
6,9851 gold badge15 silver badges40 bronze badges
6,9851 gold badge15 silver badges40 bronze badges
answered Sep 2 '16 at 9:06
Sajna TSajna T
1
1
1
If you really think this could answer the question please add some explanation to your answer. Of itself, it only installs a package. You should explain what the command does and provide an example that handles the OP's specific requirement.
– roaima
Sep 2 '16 at 10:02
add a comment |
1
If you really think this could answer the question please add some explanation to your answer. Of itself, it only installs a package. You should explain what the command does and provide an example that handles the OP's specific requirement.
– roaima
Sep 2 '16 at 10:02
1
1
If you really think this could answer the question please add some explanation to your answer. Of itself, it only installs a package. You should explain what the command does and provide an example that handles the OP's specific requirement.
– roaima
Sep 2 '16 at 10:02
If you really think this could answer the question please add some explanation to your answer. Of itself, it only installs a package. You should explain what the command does and provide an example that handles the OP's specific requirement.
– roaima
Sep 2 '16 at 10:02
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f175135%2fhow-to-rename-multiple-files-by-replacing-string-in-file-name-this-string-conta%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
3
So what have you actually tried? I see that you have linked to a few questions and say they failed, but how did they fail? IMO the best example uses the
rename
command. I suspect your rename would be as simple asrename -n 's/#/safeNBC/' *.jpg
.– Zoredache
Dec 19 '14 at 21:26
I tried
rename -n 's/#U00a9/safe/' *.jpg
and the command was accepted but no changes occurred.– Leon Francis Shelhamer
Dec 19 '14 at 23:36
Sure, as you would have seen from the documentation you surely reviewed, the
-n
is theno act
option. Which lets you see if it works before you actually use it. Did the output on the screen show the potential new names correctly?– Zoredache
Dec 19 '14 at 23:55
I apologize I copied and pasted your example without paying full attention, I did the rename command without the -n. I believe @DTK address the problem, I was not escaping the #.
– Leon Francis Shelhamer
Dec 20 '14 at 8:17
Replacing strings in filenames on MacOS: superuser.com/questions/152627/…
– Anton Tarasenko
Nov 26 '18 at 10:07