BASH file mass rename with counter
Clash Royale CLAN TAG#URR8PPP
I want to rename all the files in a folder with PREFIX+COUNTER+FILENAME
for ex.
input:
england.txt
canada.txt
france.txt
output:
CO_01_england.txt
CO_02_canada.txt
CO_03_france.txt
bash rename
add a comment |
I want to rename all the files in a folder with PREFIX+COUNTER+FILENAME
for ex.
input:
england.txt
canada.txt
france.txt
output:
CO_01_england.txt
CO_02_canada.txt
CO_03_france.txt
bash rename
How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?
– Wildcard
May 8 '16 at 6:41
no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)
– user3148655
May 8 '16 at 8:04
But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.
– Wildcard
May 8 '16 at 20:43
yes. you're correct
– user3148655
May 9 '16 at 4:14
add a comment |
I want to rename all the files in a folder with PREFIX+COUNTER+FILENAME
for ex.
input:
england.txt
canada.txt
france.txt
output:
CO_01_england.txt
CO_02_canada.txt
CO_03_france.txt
bash rename
I want to rename all the files in a folder with PREFIX+COUNTER+FILENAME
for ex.
input:
england.txt
canada.txt
france.txt
output:
CO_01_england.txt
CO_02_canada.txt
CO_03_france.txt
bash rename
bash rename
edited Jan 10 '17 at 23:00
рüффп
78331529
78331529
asked May 8 '16 at 6:00
user3148655user3148655
7728
7728
How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?
– Wildcard
May 8 '16 at 6:41
no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)
– user3148655
May 8 '16 at 8:04
But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.
– Wildcard
May 8 '16 at 20:43
yes. you're correct
– user3148655
May 9 '16 at 4:14
add a comment |
How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?
– Wildcard
May 8 '16 at 6:41
no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)
– user3148655
May 8 '16 at 8:04
But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.
– Wildcard
May 8 '16 at 20:43
yes. you're correct
– user3148655
May 9 '16 at 4:14
How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?
– Wildcard
May 8 '16 at 6:41
How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?
– Wildcard
May 8 '16 at 6:41
no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)
– user3148655
May 8 '16 at 8:04
no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)
– user3148655
May 8 '16 at 8:04
But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.
– Wildcard
May 8 '16 at 20:43
But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.
– Wildcard
May 8 '16 at 20:43
yes. you're correct
– user3148655
May 9 '16 at 4:14
yes. you're correct
– user3148655
May 9 '16 at 4:14
add a comment |
2 Answers
2
active
oldest
votes
This does what you ask:
n=1; for f in *.txt; do mv "$f" "CO_$((n++))_$f"; done
How it works
n=1
This initializes the variable
n
to 1.for f in *.txt; do
This starts a loop over all files in the current directory whose names end with
.txt
.mv "$f" "CO_$((n++))_$f"
This renames the files to have the
CO_
prefix withn
as the counter. The++
symbol tells bash to increment the variablen
.done
This signals the end of the loop.
Improvement
This version uses printf
which allows greater control over how the number will be formatted:
n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done
In particular, the %02i
format will put a leading zero before the number when n
is still in single digits.
Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt
– user3148655
May 8 '16 at 6:58
@user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.
– Gilles
May 8 '16 at 20:06
add a comment |
With the prename
utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle
:
prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt
Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_
followed by the counter value $n
(incremented before use, starting at 0 before the first incrementation and use) formatted to two decimal digits, followed by _
, followed by the original name. To rename all files with the .txt
extension, with the numbering in lexicographic order:
prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' *.txt
With zsh, use the zmv
function, and the parameter expansion flag l
to pad the number:
autoload -U zmv
zmv '*.txt' '$f:h/C0_$(l:2::0:)$((++x))_$f:t'
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%2f281794%2fbash-file-mass-rename-with-counter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This does what you ask:
n=1; for f in *.txt; do mv "$f" "CO_$((n++))_$f"; done
How it works
n=1
This initializes the variable
n
to 1.for f in *.txt; do
This starts a loop over all files in the current directory whose names end with
.txt
.mv "$f" "CO_$((n++))_$f"
This renames the files to have the
CO_
prefix withn
as the counter. The++
symbol tells bash to increment the variablen
.done
This signals the end of the loop.
Improvement
This version uses printf
which allows greater control over how the number will be formatted:
n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done
In particular, the %02i
format will put a leading zero before the number when n
is still in single digits.
Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt
– user3148655
May 8 '16 at 6:58
@user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.
– Gilles
May 8 '16 at 20:06
add a comment |
This does what you ask:
n=1; for f in *.txt; do mv "$f" "CO_$((n++))_$f"; done
How it works
n=1
This initializes the variable
n
to 1.for f in *.txt; do
This starts a loop over all files in the current directory whose names end with
.txt
.mv "$f" "CO_$((n++))_$f"
This renames the files to have the
CO_
prefix withn
as the counter. The++
symbol tells bash to increment the variablen
.done
This signals the end of the loop.
Improvement
This version uses printf
which allows greater control over how the number will be formatted:
n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done
In particular, the %02i
format will put a leading zero before the number when n
is still in single digits.
Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt
– user3148655
May 8 '16 at 6:58
@user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.
– Gilles
May 8 '16 at 20:06
add a comment |
This does what you ask:
n=1; for f in *.txt; do mv "$f" "CO_$((n++))_$f"; done
How it works
n=1
This initializes the variable
n
to 1.for f in *.txt; do
This starts a loop over all files in the current directory whose names end with
.txt
.mv "$f" "CO_$((n++))_$f"
This renames the files to have the
CO_
prefix withn
as the counter. The++
symbol tells bash to increment the variablen
.done
This signals the end of the loop.
Improvement
This version uses printf
which allows greater control over how the number will be formatted:
n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done
In particular, the %02i
format will put a leading zero before the number when n
is still in single digits.
This does what you ask:
n=1; for f in *.txt; do mv "$f" "CO_$((n++))_$f"; done
How it works
n=1
This initializes the variable
n
to 1.for f in *.txt; do
This starts a loop over all files in the current directory whose names end with
.txt
.mv "$f" "CO_$((n++))_$f"
This renames the files to have the
CO_
prefix withn
as the counter. The++
symbol tells bash to increment the variablen
.done
This signals the end of the loop.
Improvement
This version uses printf
which allows greater control over how the number will be formatted:
n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done
In particular, the %02i
format will put a leading zero before the number when n
is still in single digits.
edited Jun 21 '16 at 11:11
Raphael Ahrens
7,05152846
7,05152846
answered May 8 '16 at 6:04
John1024John1024
47.4k5110125
47.4k5110125
Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt
– user3148655
May 8 '16 at 6:58
@user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.
– Gilles
May 8 '16 at 20:06
add a comment |
Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt
– user3148655
May 8 '16 at 6:58
@user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.
– Gilles
May 8 '16 at 20:06
Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt
– user3148655
May 8 '16 at 6:58
Thank John! pretty much does what i want. but the second version does not increment the counter, and simply rename like CO_01_england.txt , CO_01_france.txt
– user3148655
May 8 '16 at 6:58
@user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.
– Gilles
May 8 '16 at 20:06
@user3148655 The incrementation was inside a subshell, so it didn't stick. See the edited answer.
– Gilles
May 8 '16 at 20:06
add a comment |
With the prename
utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle
:
prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt
Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_
followed by the counter value $n
(incremented before use, starting at 0 before the first incrementation and use) formatted to two decimal digits, followed by _
, followed by the original name. To rename all files with the .txt
extension, with the numbering in lexicographic order:
prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' *.txt
With zsh, use the zmv
function, and the parameter expansion flag l
to pad the number:
autoload -U zmv
zmv '*.txt' '$f:h/C0_$(l:2::0:)$((++x))_$f:t'
add a comment |
With the prename
utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle
:
prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt
Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_
followed by the counter value $n
(incremented before use, starting at 0 before the first incrementation and use) formatted to two decimal digits, followed by _
, followed by the original name. To rename all files with the .txt
extension, with the numbering in lexicographic order:
prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' *.txt
With zsh, use the zmv
function, and the parameter expansion flag l
to pad the number:
autoload -U zmv
zmv '*.txt' '$f:h/C0_$(l:2::0:)$((++x))_$f:t'
add a comment |
With the prename
utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle
:
prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt
Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_
followed by the counter value $n
(incremented before use, starting at 0 before the first incrementation and use) formatted to two decimal digits, followed by _
, followed by the original name. To rename all files with the .txt
extension, with the numbering in lexicographic order:
prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' *.txt
With zsh, use the zmv
function, and the parameter expansion flag l
to pad the number:
autoload -U zmv
zmv '*.txt' '$f:h/C0_$(l:2::0:)$((++x))_$f:t'
With the prename
utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle
:
prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt
Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_
followed by the counter value $n
(incremented before use, starting at 0 before the first incrementation and use) formatted to two decimal digits, followed by _
, followed by the original name. To rename all files with the .txt
extension, with the numbering in lexicographic order:
prename 's ([^/]*z) (sprintf("C0_%02d_%s", ++$n, $&))e' *.txt
With zsh, use the zmv
function, and the parameter expansion flag l
to pad the number:
autoload -U zmv
zmv '*.txt' '$f:h/C0_$(l:2::0:)$((++x))_$f:t'
answered May 8 '16 at 23:17
GillesGilles
540k12810941607
540k12810941607
add a comment |
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%2f281794%2fbash-file-mass-rename-with-counter%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
How do you want the numbers to be assigned? At random? Your example shows non-alphabetic, so I guess you don't care?
– Wildcard
May 8 '16 at 6:41
no ti should be sequential.. 01,02,03.. initial prefix is fixed (CO)
– user3148655
May 8 '16 at 8:04
But you don't care if it's 01 - canada, 02 - france, 03 - england? So it's evidently random which sequence the files are handled in.
– Wildcard
May 8 '16 at 20:43
yes. you're correct
– user3148655
May 9 '16 at 4:14