What's the difference between 'rename' and 'mv'?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
It's not completely clear to me, but what is the difference between mv
and rename
(from util-linux-ng 2.17.2
as /usr/bin/rename
)? Are there advantages of one over the other beyond rename
accepting regular expressions and mv
doesn't? I believe rename
can also handle multiple file renames at once, whereas mv
does not do this.
I couldn't find a clear indication in their man
pages what else sets them apart or through some investigation on my own.
rename mv
|
show 1 more comment
It's not completely clear to me, but what is the difference between mv
and rename
(from util-linux-ng 2.17.2
as /usr/bin/rename
)? Are there advantages of one over the other beyond rename
accepting regular expressions and mv
doesn't? I believe rename
can also handle multiple file renames at once, whereas mv
does not do this.
I couldn't find a clear indication in their man
pages what else sets them apart or through some investigation on my own.
rename mv
2
Let's make sure we're talking about the same things; what are the outputs fromtype -a rename
andrename --version
?
– Jeff Schaller♦
Apr 4 at 17:57
@JeffSchaller It's more of a curiosity on my part, but on one of my *nix machines I getrename is /usr/bin/rename
andrename (util-linux-ng 2.17.2)
respectively.
– Urda
Apr 4 at 17:58
6
There are a few different renames out there, and it could have been an alias/function/script, so I just wanted to make sure we knew what it was. Thank you! Please feel free to edit those outputs into your question.
– Jeff Schaller♦
Apr 4 at 18:03
@JeffSchaller no worries, actually this link was one I just found that showed merename
can mean different things in different distros.
– Urda
Apr 4 at 18:05
5
It seems like your question contains its answer. “What’s the difference between a jet and a tricycle, other than the fact that a jet can fly, and can go several thousand times faster?” :-)
– G-Man
Apr 4 at 18:10
|
show 1 more comment
It's not completely clear to me, but what is the difference between mv
and rename
(from util-linux-ng 2.17.2
as /usr/bin/rename
)? Are there advantages of one over the other beyond rename
accepting regular expressions and mv
doesn't? I believe rename
can also handle multiple file renames at once, whereas mv
does not do this.
I couldn't find a clear indication in their man
pages what else sets them apart or through some investigation on my own.
rename mv
It's not completely clear to me, but what is the difference between mv
and rename
(from util-linux-ng 2.17.2
as /usr/bin/rename
)? Are there advantages of one over the other beyond rename
accepting regular expressions and mv
doesn't? I believe rename
can also handle multiple file renames at once, whereas mv
does not do this.
I couldn't find a clear indication in their man
pages what else sets them apart or through some investigation on my own.
rename mv
rename mv
edited Apr 4 at 19:49
Kusalananda♦
141k17263439
141k17263439
asked Apr 4 at 17:53
UrdaUrda
1577
1577
2
Let's make sure we're talking about the same things; what are the outputs fromtype -a rename
andrename --version
?
– Jeff Schaller♦
Apr 4 at 17:57
@JeffSchaller It's more of a curiosity on my part, but on one of my *nix machines I getrename is /usr/bin/rename
andrename (util-linux-ng 2.17.2)
respectively.
– Urda
Apr 4 at 17:58
6
There are a few different renames out there, and it could have been an alias/function/script, so I just wanted to make sure we knew what it was. Thank you! Please feel free to edit those outputs into your question.
– Jeff Schaller♦
Apr 4 at 18:03
@JeffSchaller no worries, actually this link was one I just found that showed merename
can mean different things in different distros.
– Urda
Apr 4 at 18:05
5
It seems like your question contains its answer. “What’s the difference between a jet and a tricycle, other than the fact that a jet can fly, and can go several thousand times faster?” :-)
– G-Man
Apr 4 at 18:10
|
show 1 more comment
2
Let's make sure we're talking about the same things; what are the outputs fromtype -a rename
andrename --version
?
– Jeff Schaller♦
Apr 4 at 17:57
@JeffSchaller It's more of a curiosity on my part, but on one of my *nix machines I getrename is /usr/bin/rename
andrename (util-linux-ng 2.17.2)
respectively.
– Urda
Apr 4 at 17:58
6
There are a few different renames out there, and it could have been an alias/function/script, so I just wanted to make sure we knew what it was. Thank you! Please feel free to edit those outputs into your question.
– Jeff Schaller♦
Apr 4 at 18:03
@JeffSchaller no worries, actually this link was one I just found that showed merename
can mean different things in different distros.
– Urda
Apr 4 at 18:05
5
It seems like your question contains its answer. “What’s the difference between a jet and a tricycle, other than the fact that a jet can fly, and can go several thousand times faster?” :-)
– G-Man
Apr 4 at 18:10
2
2
Let's make sure we're talking about the same things; what are the outputs from
type -a rename
and rename --version
?– Jeff Schaller♦
Apr 4 at 17:57
Let's make sure we're talking about the same things; what are the outputs from
type -a rename
and rename --version
?– Jeff Schaller♦
Apr 4 at 17:57
@JeffSchaller It's more of a curiosity on my part, but on one of my *nix machines I get
rename is /usr/bin/rename
and rename (util-linux-ng 2.17.2)
respectively.– Urda
Apr 4 at 17:58
@JeffSchaller It's more of a curiosity on my part, but on one of my *nix machines I get
rename is /usr/bin/rename
and rename (util-linux-ng 2.17.2)
respectively.– Urda
Apr 4 at 17:58
6
6
There are a few different renames out there, and it could have been an alias/function/script, so I just wanted to make sure we knew what it was. Thank you! Please feel free to edit those outputs into your question.
– Jeff Schaller♦
Apr 4 at 18:03
There are a few different renames out there, and it could have been an alias/function/script, so I just wanted to make sure we knew what it was. Thank you! Please feel free to edit those outputs into your question.
– Jeff Schaller♦
Apr 4 at 18:03
@JeffSchaller no worries, actually this link was one I just found that showed me
rename
can mean different things in different distros.– Urda
Apr 4 at 18:05
@JeffSchaller no worries, actually this link was one I just found that showed me
rename
can mean different things in different distros.– Urda
Apr 4 at 18:05
5
5
It seems like your question contains its answer. “What’s the difference between a jet and a tricycle, other than the fact that a jet can fly, and can go several thousand times faster?” :-)
– G-Man
Apr 4 at 18:10
It seems like your question contains its answer. “What’s the difference between a jet and a tricycle, other than the fact that a jet can fly, and can go several thousand times faster?” :-)
– G-Man
Apr 4 at 18:10
|
show 1 more comment
4 Answers
4
active
oldest
votes
It's basically what it says on the lid, for both.
mv
is a standard utility to move one or more files to a given target. It can be used to rename a file, if there's only one file to move. If there are several, mv
only works if the target is directory, and moves the files there.
So mv foo bar
will either move the file foo
to the directory bar
(if it exists), or rename foo
to bar
(if bar
doesn't exist or isn't a directory). mv foo1 foo2 bar
will just move both files to directory bar
, or complain if bar
isn't a directory.
mv
will call the rename()
C library function to move the files, and if that doesn't work (they're being moved to another filesystem), it will copy the files and remove the originals.
If all you have is mv
and you want to rename multiple files, you'll have to use a shell loop. There are a number of questions on that here on the site, see e.g. this, this, and others.
On the other hand, the various rename
utilities rename files, individually.
The rename
from util-linux which you mention makes a simple string substitution, e.g. rename foo bar *
would change foobar
to barbar
, and asdffoo
to asdfbar
. It does not, however, take a regular expression!
The Perl rename utility (or various instances of it) takes a Perl expression to transform the filenames. One will most likely use an s/pattern/replacement/
command, where the pattern is a regular expression.
Both the util-linux rename and the Perl rename can be used to move files to another directory at the same time, by making appropriate changes to the file name, but it's a bit awkward. Neither does more than call rename()
on the files, so moving from one filesystem to another does not work.
As for which rename
you have, it may depend on your distribution, and/or what you have installed. Most of them support rename --version
, so use that to identify which one you have.
Not only 'regular expression' but function if need it
– Gilles Quenot
Apr 4 at 22:38
@GillesQuenot, well, yeah, you could do e.g.rename '$_=lc' *
to lowercase filenames. But I think the most common case is to uses///
(which I mentioned) and the pattern part of it is a regex; I don't think you can use a function there. You could do it in the replacement part, though.
– ilkkachu
Apr 5 at 8:35
add a comment |
mv
It's a basic command line designed to do one thing and do it well (Unix philosophy) : move file(s) or directorie(s).
You can hack STDOUT
& STDIN
¹ to modify on the fly the destination string, but it's just not a smart hack
rename (Perl's one)
There are other tools with the same name which may or may not be able to do this, so be careful.
When people talk about rename
, we think about this one, not the ELF
one, less powerful (magic?).
It's not basic, it's Perl. You can pass some Perl's functions inside, and it's extremely powerful.
Consider this example :
You want to rename a bunch of files like
foobar_1.txt
foobar_2.txt
foobar_3.txt
You can prepend zeros to the digits with sprintf()
like this (using regex, heh, it's Perl :D ) :
rename 's/(d+)/sprintf("%04d", $1)/e' foobar_*.txt
Now you have :
foobar_0001.txt
foobar_0002.txt
foobar_0003.txt
Not really a basic command, isn't it ?
rename is not really designed to move dir(s), but it can do it :
$ mkdir -p /tmp/foo/bar/base
$ touch /tmp/foo/bar/base/file
$ rename 's!/tmp/foo/bar/base/file!/tmp/file!' /tmp/foo/bar/base/file
The moved file
/tmp/file
¹ some code we see on *.stackexchange.*
websites
for FILE in `ls *.txt`
do
mv ${FILE} `echo ${FILE} | sed 's/anything_ugly/anything_still_ugly/'`
done
It's not the way to go, it's plain buggy, just to explain why to use the right tool at the right moment
+1 forsprintf
ing with rename
– Archemar
Apr 4 at 19:30
add a comment |
mv
simply changes the name of the file (it can also move it to another filesystem or path). You give it an old name and a new name, and it changes the file to the new name or location. rename
is used to make bulk naming changes. Say you had a thousand files, foo000.log
through foo999.log
and you wanted to change them to bar000.log
through bar999.log
. With mv
you'd have to do mv foo000.log bar000.log
, mv foo001 bar001.log
, etc. or else write a script. With rename
you simply do rename foo bar foo*.log
, and voila, a thousand files are changed in an instant! Pretty cool. Check out the man rename
page again for details.
add a comment |
mv moves or renames files and directories and will back them up; rename just renames files.
mv has more capabilities and options. Look at the switches in the man pages for each to see the differences in capabilities. Let's take a look using man in Ubuntu 18.04LTS (your mileage may vary depending on the version of each package):
mv options (omitting help and version)
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force
do not prompt before overwriting
-i, --interactive
prompt before overwrite
-n, --no-clobber
do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
move all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-u, --update
move only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose
explain what is being done
-Z, --context
set SELinux security context of destination file to default type
rename options (omitting help and version)
-s, --symlink
Do not rename a symlink but its target.
-v, --verbose
Show which files where renamed, if any.
-n, --no-act
Do not make any changes.
-o, --no-overwrite
Do not overwrite existing files.
3
Define 'more powerful'. Does mv support regex like rename ?
– Gilles Quenot
Apr 4 at 18:24
As the questioner stated, no.
– K7AAY
Apr 4 at 18:38
2
Wow, what an argument
– Gilles Quenot
Apr 4 at 18:46
1
Giving credit where credit is due.
– K7AAY
Apr 4 at 18:54
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%2f510553%2fwhats-the-difference-between-rename-and-mv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's basically what it says on the lid, for both.
mv
is a standard utility to move one or more files to a given target. It can be used to rename a file, if there's only one file to move. If there are several, mv
only works if the target is directory, and moves the files there.
So mv foo bar
will either move the file foo
to the directory bar
(if it exists), or rename foo
to bar
(if bar
doesn't exist or isn't a directory). mv foo1 foo2 bar
will just move both files to directory bar
, or complain if bar
isn't a directory.
mv
will call the rename()
C library function to move the files, and if that doesn't work (they're being moved to another filesystem), it will copy the files and remove the originals.
If all you have is mv
and you want to rename multiple files, you'll have to use a shell loop. There are a number of questions on that here on the site, see e.g. this, this, and others.
On the other hand, the various rename
utilities rename files, individually.
The rename
from util-linux which you mention makes a simple string substitution, e.g. rename foo bar *
would change foobar
to barbar
, and asdffoo
to asdfbar
. It does not, however, take a regular expression!
The Perl rename utility (or various instances of it) takes a Perl expression to transform the filenames. One will most likely use an s/pattern/replacement/
command, where the pattern is a regular expression.
Both the util-linux rename and the Perl rename can be used to move files to another directory at the same time, by making appropriate changes to the file name, but it's a bit awkward. Neither does more than call rename()
on the files, so moving from one filesystem to another does not work.
As for which rename
you have, it may depend on your distribution, and/or what you have installed. Most of them support rename --version
, so use that to identify which one you have.
Not only 'regular expression' but function if need it
– Gilles Quenot
Apr 4 at 22:38
@GillesQuenot, well, yeah, you could do e.g.rename '$_=lc' *
to lowercase filenames. But I think the most common case is to uses///
(which I mentioned) and the pattern part of it is a regex; I don't think you can use a function there. You could do it in the replacement part, though.
– ilkkachu
Apr 5 at 8:35
add a comment |
It's basically what it says on the lid, for both.
mv
is a standard utility to move one or more files to a given target. It can be used to rename a file, if there's only one file to move. If there are several, mv
only works if the target is directory, and moves the files there.
So mv foo bar
will either move the file foo
to the directory bar
(if it exists), or rename foo
to bar
(if bar
doesn't exist or isn't a directory). mv foo1 foo2 bar
will just move both files to directory bar
, or complain if bar
isn't a directory.
mv
will call the rename()
C library function to move the files, and if that doesn't work (they're being moved to another filesystem), it will copy the files and remove the originals.
If all you have is mv
and you want to rename multiple files, you'll have to use a shell loop. There are a number of questions on that here on the site, see e.g. this, this, and others.
On the other hand, the various rename
utilities rename files, individually.
The rename
from util-linux which you mention makes a simple string substitution, e.g. rename foo bar *
would change foobar
to barbar
, and asdffoo
to asdfbar
. It does not, however, take a regular expression!
The Perl rename utility (or various instances of it) takes a Perl expression to transform the filenames. One will most likely use an s/pattern/replacement/
command, where the pattern is a regular expression.
Both the util-linux rename and the Perl rename can be used to move files to another directory at the same time, by making appropriate changes to the file name, but it's a bit awkward. Neither does more than call rename()
on the files, so moving from one filesystem to another does not work.
As for which rename
you have, it may depend on your distribution, and/or what you have installed. Most of them support rename --version
, so use that to identify which one you have.
Not only 'regular expression' but function if need it
– Gilles Quenot
Apr 4 at 22:38
@GillesQuenot, well, yeah, you could do e.g.rename '$_=lc' *
to lowercase filenames. But I think the most common case is to uses///
(which I mentioned) and the pattern part of it is a regex; I don't think you can use a function there. You could do it in the replacement part, though.
– ilkkachu
Apr 5 at 8:35
add a comment |
It's basically what it says on the lid, for both.
mv
is a standard utility to move one or more files to a given target. It can be used to rename a file, if there's only one file to move. If there are several, mv
only works if the target is directory, and moves the files there.
So mv foo bar
will either move the file foo
to the directory bar
(if it exists), or rename foo
to bar
(if bar
doesn't exist or isn't a directory). mv foo1 foo2 bar
will just move both files to directory bar
, or complain if bar
isn't a directory.
mv
will call the rename()
C library function to move the files, and if that doesn't work (they're being moved to another filesystem), it will copy the files and remove the originals.
If all you have is mv
and you want to rename multiple files, you'll have to use a shell loop. There are a number of questions on that here on the site, see e.g. this, this, and others.
On the other hand, the various rename
utilities rename files, individually.
The rename
from util-linux which you mention makes a simple string substitution, e.g. rename foo bar *
would change foobar
to barbar
, and asdffoo
to asdfbar
. It does not, however, take a regular expression!
The Perl rename utility (or various instances of it) takes a Perl expression to transform the filenames. One will most likely use an s/pattern/replacement/
command, where the pattern is a regular expression.
Both the util-linux rename and the Perl rename can be used to move files to another directory at the same time, by making appropriate changes to the file name, but it's a bit awkward. Neither does more than call rename()
on the files, so moving from one filesystem to another does not work.
As for which rename
you have, it may depend on your distribution, and/or what you have installed. Most of them support rename --version
, so use that to identify which one you have.
It's basically what it says on the lid, for both.
mv
is a standard utility to move one or more files to a given target. It can be used to rename a file, if there's only one file to move. If there are several, mv
only works if the target is directory, and moves the files there.
So mv foo bar
will either move the file foo
to the directory bar
(if it exists), or rename foo
to bar
(if bar
doesn't exist or isn't a directory). mv foo1 foo2 bar
will just move both files to directory bar
, or complain if bar
isn't a directory.
mv
will call the rename()
C library function to move the files, and if that doesn't work (they're being moved to another filesystem), it will copy the files and remove the originals.
If all you have is mv
and you want to rename multiple files, you'll have to use a shell loop. There are a number of questions on that here on the site, see e.g. this, this, and others.
On the other hand, the various rename
utilities rename files, individually.
The rename
from util-linux which you mention makes a simple string substitution, e.g. rename foo bar *
would change foobar
to barbar
, and asdffoo
to asdfbar
. It does not, however, take a regular expression!
The Perl rename utility (or various instances of it) takes a Perl expression to transform the filenames. One will most likely use an s/pattern/replacement/
command, where the pattern is a regular expression.
Both the util-linux rename and the Perl rename can be used to move files to another directory at the same time, by making appropriate changes to the file name, but it's a bit awkward. Neither does more than call rename()
on the files, so moving from one filesystem to another does not work.
As for which rename
you have, it may depend on your distribution, and/or what you have installed. Most of them support rename --version
, so use that to identify which one you have.
edited Apr 5 at 14:05
answered Apr 4 at 20:36
ilkkachuilkkachu
63.4k10104181
63.4k10104181
Not only 'regular expression' but function if need it
– Gilles Quenot
Apr 4 at 22:38
@GillesQuenot, well, yeah, you could do e.g.rename '$_=lc' *
to lowercase filenames. But I think the most common case is to uses///
(which I mentioned) and the pattern part of it is a regex; I don't think you can use a function there. You could do it in the replacement part, though.
– ilkkachu
Apr 5 at 8:35
add a comment |
Not only 'regular expression' but function if need it
– Gilles Quenot
Apr 4 at 22:38
@GillesQuenot, well, yeah, you could do e.g.rename '$_=lc' *
to lowercase filenames. But I think the most common case is to uses///
(which I mentioned) and the pattern part of it is a regex; I don't think you can use a function there. You could do it in the replacement part, though.
– ilkkachu
Apr 5 at 8:35
Not only 'regular expression' but function if need it
– Gilles Quenot
Apr 4 at 22:38
Not only 'regular expression' but function if need it
– Gilles Quenot
Apr 4 at 22:38
@GillesQuenot, well, yeah, you could do e.g.
rename '$_=lc' *
to lowercase filenames. But I think the most common case is to use s///
(which I mentioned) and the pattern part of it is a regex; I don't think you can use a function there. You could do it in the replacement part, though.– ilkkachu
Apr 5 at 8:35
@GillesQuenot, well, yeah, you could do e.g.
rename '$_=lc' *
to lowercase filenames. But I think the most common case is to use s///
(which I mentioned) and the pattern part of it is a regex; I don't think you can use a function there. You could do it in the replacement part, though.– ilkkachu
Apr 5 at 8:35
add a comment |
mv
It's a basic command line designed to do one thing and do it well (Unix philosophy) : move file(s) or directorie(s).
You can hack STDOUT
& STDIN
¹ to modify on the fly the destination string, but it's just not a smart hack
rename (Perl's one)
There are other tools with the same name which may or may not be able to do this, so be careful.
When people talk about rename
, we think about this one, not the ELF
one, less powerful (magic?).
It's not basic, it's Perl. You can pass some Perl's functions inside, and it's extremely powerful.
Consider this example :
You want to rename a bunch of files like
foobar_1.txt
foobar_2.txt
foobar_3.txt
You can prepend zeros to the digits with sprintf()
like this (using regex, heh, it's Perl :D ) :
rename 's/(d+)/sprintf("%04d", $1)/e' foobar_*.txt
Now you have :
foobar_0001.txt
foobar_0002.txt
foobar_0003.txt
Not really a basic command, isn't it ?
rename is not really designed to move dir(s), but it can do it :
$ mkdir -p /tmp/foo/bar/base
$ touch /tmp/foo/bar/base/file
$ rename 's!/tmp/foo/bar/base/file!/tmp/file!' /tmp/foo/bar/base/file
The moved file
/tmp/file
¹ some code we see on *.stackexchange.*
websites
for FILE in `ls *.txt`
do
mv ${FILE} `echo ${FILE} | sed 's/anything_ugly/anything_still_ugly/'`
done
It's not the way to go, it's plain buggy, just to explain why to use the right tool at the right moment
+1 forsprintf
ing with rename
– Archemar
Apr 4 at 19:30
add a comment |
mv
It's a basic command line designed to do one thing and do it well (Unix philosophy) : move file(s) or directorie(s).
You can hack STDOUT
& STDIN
¹ to modify on the fly the destination string, but it's just not a smart hack
rename (Perl's one)
There are other tools with the same name which may or may not be able to do this, so be careful.
When people talk about rename
, we think about this one, not the ELF
one, less powerful (magic?).
It's not basic, it's Perl. You can pass some Perl's functions inside, and it's extremely powerful.
Consider this example :
You want to rename a bunch of files like
foobar_1.txt
foobar_2.txt
foobar_3.txt
You can prepend zeros to the digits with sprintf()
like this (using regex, heh, it's Perl :D ) :
rename 's/(d+)/sprintf("%04d", $1)/e' foobar_*.txt
Now you have :
foobar_0001.txt
foobar_0002.txt
foobar_0003.txt
Not really a basic command, isn't it ?
rename is not really designed to move dir(s), but it can do it :
$ mkdir -p /tmp/foo/bar/base
$ touch /tmp/foo/bar/base/file
$ rename 's!/tmp/foo/bar/base/file!/tmp/file!' /tmp/foo/bar/base/file
The moved file
/tmp/file
¹ some code we see on *.stackexchange.*
websites
for FILE in `ls *.txt`
do
mv ${FILE} `echo ${FILE} | sed 's/anything_ugly/anything_still_ugly/'`
done
It's not the way to go, it's plain buggy, just to explain why to use the right tool at the right moment
+1 forsprintf
ing with rename
– Archemar
Apr 4 at 19:30
add a comment |
mv
It's a basic command line designed to do one thing and do it well (Unix philosophy) : move file(s) or directorie(s).
You can hack STDOUT
& STDIN
¹ to modify on the fly the destination string, but it's just not a smart hack
rename (Perl's one)
There are other tools with the same name which may or may not be able to do this, so be careful.
When people talk about rename
, we think about this one, not the ELF
one, less powerful (magic?).
It's not basic, it's Perl. You can pass some Perl's functions inside, and it's extremely powerful.
Consider this example :
You want to rename a bunch of files like
foobar_1.txt
foobar_2.txt
foobar_3.txt
You can prepend zeros to the digits with sprintf()
like this (using regex, heh, it's Perl :D ) :
rename 's/(d+)/sprintf("%04d", $1)/e' foobar_*.txt
Now you have :
foobar_0001.txt
foobar_0002.txt
foobar_0003.txt
Not really a basic command, isn't it ?
rename is not really designed to move dir(s), but it can do it :
$ mkdir -p /tmp/foo/bar/base
$ touch /tmp/foo/bar/base/file
$ rename 's!/tmp/foo/bar/base/file!/tmp/file!' /tmp/foo/bar/base/file
The moved file
/tmp/file
¹ some code we see on *.stackexchange.*
websites
for FILE in `ls *.txt`
do
mv ${FILE} `echo ${FILE} | sed 's/anything_ugly/anything_still_ugly/'`
done
It's not the way to go, it's plain buggy, just to explain why to use the right tool at the right moment
mv
It's a basic command line designed to do one thing and do it well (Unix philosophy) : move file(s) or directorie(s).
You can hack STDOUT
& STDIN
¹ to modify on the fly the destination string, but it's just not a smart hack
rename (Perl's one)
There are other tools with the same name which may or may not be able to do this, so be careful.
When people talk about rename
, we think about this one, not the ELF
one, less powerful (magic?).
It's not basic, it's Perl. You can pass some Perl's functions inside, and it's extremely powerful.
Consider this example :
You want to rename a bunch of files like
foobar_1.txt
foobar_2.txt
foobar_3.txt
You can prepend zeros to the digits with sprintf()
like this (using regex, heh, it's Perl :D ) :
rename 's/(d+)/sprintf("%04d", $1)/e' foobar_*.txt
Now you have :
foobar_0001.txt
foobar_0002.txt
foobar_0003.txt
Not really a basic command, isn't it ?
rename is not really designed to move dir(s), but it can do it :
$ mkdir -p /tmp/foo/bar/base
$ touch /tmp/foo/bar/base/file
$ rename 's!/tmp/foo/bar/base/file!/tmp/file!' /tmp/foo/bar/base/file
The moved file
/tmp/file
¹ some code we see on *.stackexchange.*
websites
for FILE in `ls *.txt`
do
mv ${FILE} `echo ${FILE} | sed 's/anything_ugly/anything_still_ugly/'`
done
It's not the way to go, it's plain buggy, just to explain why to use the right tool at the right moment
edited Apr 5 at 3:21
answered Apr 4 at 19:18
Gilles QuenotGilles Quenot
16.5k14054
16.5k14054
+1 forsprintf
ing with rename
– Archemar
Apr 4 at 19:30
add a comment |
+1 forsprintf
ing with rename
– Archemar
Apr 4 at 19:30
+1 for
sprintf
ing with rename– Archemar
Apr 4 at 19:30
+1 for
sprintf
ing with rename– Archemar
Apr 4 at 19:30
add a comment |
mv
simply changes the name of the file (it can also move it to another filesystem or path). You give it an old name and a new name, and it changes the file to the new name or location. rename
is used to make bulk naming changes. Say you had a thousand files, foo000.log
through foo999.log
and you wanted to change them to bar000.log
through bar999.log
. With mv
you'd have to do mv foo000.log bar000.log
, mv foo001 bar001.log
, etc. or else write a script. With rename
you simply do rename foo bar foo*.log
, and voila, a thousand files are changed in an instant! Pretty cool. Check out the man rename
page again for details.
add a comment |
mv
simply changes the name of the file (it can also move it to another filesystem or path). You give it an old name and a new name, and it changes the file to the new name or location. rename
is used to make bulk naming changes. Say you had a thousand files, foo000.log
through foo999.log
and you wanted to change them to bar000.log
through bar999.log
. With mv
you'd have to do mv foo000.log bar000.log
, mv foo001 bar001.log
, etc. or else write a script. With rename
you simply do rename foo bar foo*.log
, and voila, a thousand files are changed in an instant! Pretty cool. Check out the man rename
page again for details.
add a comment |
mv
simply changes the name of the file (it can also move it to another filesystem or path). You give it an old name and a new name, and it changes the file to the new name or location. rename
is used to make bulk naming changes. Say you had a thousand files, foo000.log
through foo999.log
and you wanted to change them to bar000.log
through bar999.log
. With mv
you'd have to do mv foo000.log bar000.log
, mv foo001 bar001.log
, etc. or else write a script. With rename
you simply do rename foo bar foo*.log
, and voila, a thousand files are changed in an instant! Pretty cool. Check out the man rename
page again for details.
mv
simply changes the name of the file (it can also move it to another filesystem or path). You give it an old name and a new name, and it changes the file to the new name or location. rename
is used to make bulk naming changes. Say you had a thousand files, foo000.log
through foo999.log
and you wanted to change them to bar000.log
through bar999.log
. With mv
you'd have to do mv foo000.log bar000.log
, mv foo001 bar001.log
, etc. or else write a script. With rename
you simply do rename foo bar foo*.log
, and voila, a thousand files are changed in an instant! Pretty cool. Check out the man rename
page again for details.
answered Apr 4 at 20:38
eewancoeewanco
310212
310212
add a comment |
add a comment |
mv moves or renames files and directories and will back them up; rename just renames files.
mv has more capabilities and options. Look at the switches in the man pages for each to see the differences in capabilities. Let's take a look using man in Ubuntu 18.04LTS (your mileage may vary depending on the version of each package):
mv options (omitting help and version)
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force
do not prompt before overwriting
-i, --interactive
prompt before overwrite
-n, --no-clobber
do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
move all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-u, --update
move only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose
explain what is being done
-Z, --context
set SELinux security context of destination file to default type
rename options (omitting help and version)
-s, --symlink
Do not rename a symlink but its target.
-v, --verbose
Show which files where renamed, if any.
-n, --no-act
Do not make any changes.
-o, --no-overwrite
Do not overwrite existing files.
3
Define 'more powerful'. Does mv support regex like rename ?
– Gilles Quenot
Apr 4 at 18:24
As the questioner stated, no.
– K7AAY
Apr 4 at 18:38
2
Wow, what an argument
– Gilles Quenot
Apr 4 at 18:46
1
Giving credit where credit is due.
– K7AAY
Apr 4 at 18:54
add a comment |
mv moves or renames files and directories and will back them up; rename just renames files.
mv has more capabilities and options. Look at the switches in the man pages for each to see the differences in capabilities. Let's take a look using man in Ubuntu 18.04LTS (your mileage may vary depending on the version of each package):
mv options (omitting help and version)
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force
do not prompt before overwriting
-i, --interactive
prompt before overwrite
-n, --no-clobber
do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
move all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-u, --update
move only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose
explain what is being done
-Z, --context
set SELinux security context of destination file to default type
rename options (omitting help and version)
-s, --symlink
Do not rename a symlink but its target.
-v, --verbose
Show which files where renamed, if any.
-n, --no-act
Do not make any changes.
-o, --no-overwrite
Do not overwrite existing files.
3
Define 'more powerful'. Does mv support regex like rename ?
– Gilles Quenot
Apr 4 at 18:24
As the questioner stated, no.
– K7AAY
Apr 4 at 18:38
2
Wow, what an argument
– Gilles Quenot
Apr 4 at 18:46
1
Giving credit where credit is due.
– K7AAY
Apr 4 at 18:54
add a comment |
mv moves or renames files and directories and will back them up; rename just renames files.
mv has more capabilities and options. Look at the switches in the man pages for each to see the differences in capabilities. Let's take a look using man in Ubuntu 18.04LTS (your mileage may vary depending on the version of each package):
mv options (omitting help and version)
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force
do not prompt before overwriting
-i, --interactive
prompt before overwrite
-n, --no-clobber
do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
move all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-u, --update
move only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose
explain what is being done
-Z, --context
set SELinux security context of destination file to default type
rename options (omitting help and version)
-s, --symlink
Do not rename a symlink but its target.
-v, --verbose
Show which files where renamed, if any.
-n, --no-act
Do not make any changes.
-o, --no-overwrite
Do not overwrite existing files.
mv moves or renames files and directories and will back them up; rename just renames files.
mv has more capabilities and options. Look at the switches in the man pages for each to see the differences in capabilities. Let's take a look using man in Ubuntu 18.04LTS (your mileage may vary depending on the version of each package):
mv options (omitting help and version)
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force
do not prompt before overwriting
-i, --interactive
prompt before overwrite
-n, --no-clobber
do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
move all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-u, --update
move only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose
explain what is being done
-Z, --context
set SELinux security context of destination file to default type
rename options (omitting help and version)
-s, --symlink
Do not rename a symlink but its target.
-v, --verbose
Show which files where renamed, if any.
-n, --no-act
Do not make any changes.
-o, --no-overwrite
Do not overwrite existing files.
edited Apr 4 at 18:24
answered Apr 4 at 18:04
K7AAYK7AAY
1,1181028
1,1181028
3
Define 'more powerful'. Does mv support regex like rename ?
– Gilles Quenot
Apr 4 at 18:24
As the questioner stated, no.
– K7AAY
Apr 4 at 18:38
2
Wow, what an argument
– Gilles Quenot
Apr 4 at 18:46
1
Giving credit where credit is due.
– K7AAY
Apr 4 at 18:54
add a comment |
3
Define 'more powerful'. Does mv support regex like rename ?
– Gilles Quenot
Apr 4 at 18:24
As the questioner stated, no.
– K7AAY
Apr 4 at 18:38
2
Wow, what an argument
– Gilles Quenot
Apr 4 at 18:46
1
Giving credit where credit is due.
– K7AAY
Apr 4 at 18:54
3
3
Define 'more powerful'. Does mv support regex like rename ?
– Gilles Quenot
Apr 4 at 18:24
Define 'more powerful'. Does mv support regex like rename ?
– Gilles Quenot
Apr 4 at 18:24
As the questioner stated, no.
– K7AAY
Apr 4 at 18:38
As the questioner stated, no.
– K7AAY
Apr 4 at 18:38
2
2
Wow, what an argument
– Gilles Quenot
Apr 4 at 18:46
Wow, what an argument
– Gilles Quenot
Apr 4 at 18:46
1
1
Giving credit where credit is due.
– K7AAY
Apr 4 at 18:54
Giving credit where credit is due.
– K7AAY
Apr 4 at 18:54
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%2f510553%2fwhats-the-difference-between-rename-and-mv%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
2
Let's make sure we're talking about the same things; what are the outputs from
type -a rename
andrename --version
?– Jeff Schaller♦
Apr 4 at 17:57
@JeffSchaller It's more of a curiosity on my part, but on one of my *nix machines I get
rename is /usr/bin/rename
andrename (util-linux-ng 2.17.2)
respectively.– Urda
Apr 4 at 17:58
6
There are a few different renames out there, and it could have been an alias/function/script, so I just wanted to make sure we knew what it was. Thank you! Please feel free to edit those outputs into your question.
– Jeff Schaller♦
Apr 4 at 18:03
@JeffSchaller no worries, actually this link was one I just found that showed me
rename
can mean different things in different distros.– Urda
Apr 4 at 18:05
5
It seems like your question contains its answer. “What’s the difference between a jet and a tricycle, other than the fact that a jet can fly, and can go several thousand times faster?” :-)
– G-Man
Apr 4 at 18:10