Rename files add dashes after fourth and sixth characters
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have some files such as:
20150716_something-here
20150716_something-heretoo
20150716_something-hereaswell
They need to be renamed as
2015-07-16_something-here
2015-07-16_something-heretoo
2015-07-16_something-hereaswell
I tried using a perl implementation of the rename
command (see my comment on the accepted answer) but I was not successful.
bash terminal regular-expression rename
add a comment |
up vote
1
down vote
favorite
I have some files such as:
20150716_something-here
20150716_something-heretoo
20150716_something-hereaswell
They need to be renamed as
2015-07-16_something-here
2015-07-16_something-heretoo
2015-07-16_something-hereaswell
I tried using a perl implementation of the rename
command (see my comment on the accepted answer) but I was not successful.
bash terminal regular-expression rename
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have some files such as:
20150716_something-here
20150716_something-heretoo
20150716_something-hereaswell
They need to be renamed as
2015-07-16_something-here
2015-07-16_something-heretoo
2015-07-16_something-hereaswell
I tried using a perl implementation of the rename
command (see my comment on the accepted answer) but I was not successful.
bash terminal regular-expression rename
I have some files such as:
20150716_something-here
20150716_something-heretoo
20150716_something-hereaswell
They need to be renamed as
2015-07-16_something-here
2015-07-16_something-heretoo
2015-07-16_something-hereaswell
I tried using a perl implementation of the rename
command (see my comment on the accepted answer) but I was not successful.
bash terminal regular-expression rename
bash terminal regular-expression rename
edited Nov 20 at 22:25
Rui F Ribeiro
38.2k1475125
38.2k1475125
asked May 19 '16 at 21:52
Adrien Be
1085
1085
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
up vote
1
down vote
accepted
Using the perl
rename command (which is completely different to the rename
command from util-linux
):
rename -v 's/^(d4)(d2)(d2)/$1-$2-$3/' 2015*
(use -n
rather than -v
for a dry-run to test the command first).
This perl version of rename
may be called prename
or file-rename
on your system. It is far more capable and useful than the util-linux
version of rename
. If you don't have it installed, you should install it. If it isn't packaged for your distro, you can install from CPAN
BTW, you can tell if you already have it installed by running rename -V
. If it produces output like this:
$ rename -V
Unknown option: V
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
or this:
$ rename -V
/usr/bin/rename using File::Rename version 0.20
Then you have perl rename
installed. The former indicates an old version (which AFAIK lives on only on pre-jessie debian installs, included as part of the perl
package). The latter indicates the current version (now a separate package called rename
).
wooops, I did not realize I was using aperl
implementation ofrename
. Installed this package using homebrew viabrew install rename
, now doingbrew info rename
prompts"Perl-powered file rename script"
– Adrien Be
May 27 '16 at 7:24
you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
– cas
May 27 '16 at 7:32
add a comment |
up vote
2
down vote
With sed
:
LC_ALL=C sed -e 's/([0-9]4)([0-9]2)([0-9]2)/1_2_3/' <file
1
How does this rename a file?
– Wildcard
May 20 '16 at 5:27
This one just format the text, it does not rename the file
– cuonglm
May 20 '16 at 5:34
add a comment |
up vote
2
down vote
While rename
is a very powerful tool, I normally prefer the simplicity of the mmv
(multiple move) utility:
mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9'
The ?
in the search pattern stands for a single character, the *
for an arbitrarily long sequence of characters. In the replacement pattern, every #<number>
stands for a corresponding ?
or *
in the search pattern. In addition to ?
and *
, mmv
supports character ranges within brackets (like [a-f]
).
(mmv
will test for any conflicts in renaming before it starts work, so you do not risk losing files by overwriting.)
add a comment |
up vote
2
down vote
Using bash's built-in substring expansion:
for f in 2015* ; do
mv "$f" "$f::4-$f:4:2-$f:6"
done
add a comment |
up vote
0
down vote
On my distribution I have the perl-rename
command, which can use a perl-style regex to bulk-rename files. The rename
command only accepts a pair of fixed strings for the rename.
add a comment |
up vote
0
down vote
quick and dirty not full solution
#!/usr/bin/env bash
str=$1
yyyy=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 1, 4)')
mm=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 5, 2)')
dd=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 7, 2)')
new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' 'print $2'`
echo $new_str
output:
$ bash script.sh '20150716_something-here'
2015-07-16_something-here
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Using the perl
rename command (which is completely different to the rename
command from util-linux
):
rename -v 's/^(d4)(d2)(d2)/$1-$2-$3/' 2015*
(use -n
rather than -v
for a dry-run to test the command first).
This perl version of rename
may be called prename
or file-rename
on your system. It is far more capable and useful than the util-linux
version of rename
. If you don't have it installed, you should install it. If it isn't packaged for your distro, you can install from CPAN
BTW, you can tell if you already have it installed by running rename -V
. If it produces output like this:
$ rename -V
Unknown option: V
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
or this:
$ rename -V
/usr/bin/rename using File::Rename version 0.20
Then you have perl rename
installed. The former indicates an old version (which AFAIK lives on only on pre-jessie debian installs, included as part of the perl
package). The latter indicates the current version (now a separate package called rename
).
wooops, I did not realize I was using aperl
implementation ofrename
. Installed this package using homebrew viabrew install rename
, now doingbrew info rename
prompts"Perl-powered file rename script"
– Adrien Be
May 27 '16 at 7:24
you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
– cas
May 27 '16 at 7:32
add a comment |
up vote
1
down vote
accepted
Using the perl
rename command (which is completely different to the rename
command from util-linux
):
rename -v 's/^(d4)(d2)(d2)/$1-$2-$3/' 2015*
(use -n
rather than -v
for a dry-run to test the command first).
This perl version of rename
may be called prename
or file-rename
on your system. It is far more capable and useful than the util-linux
version of rename
. If you don't have it installed, you should install it. If it isn't packaged for your distro, you can install from CPAN
BTW, you can tell if you already have it installed by running rename -V
. If it produces output like this:
$ rename -V
Unknown option: V
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
or this:
$ rename -V
/usr/bin/rename using File::Rename version 0.20
Then you have perl rename
installed. The former indicates an old version (which AFAIK lives on only on pre-jessie debian installs, included as part of the perl
package). The latter indicates the current version (now a separate package called rename
).
wooops, I did not realize I was using aperl
implementation ofrename
. Installed this package using homebrew viabrew install rename
, now doingbrew info rename
prompts"Perl-powered file rename script"
– Adrien Be
May 27 '16 at 7:24
you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
– cas
May 27 '16 at 7:32
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Using the perl
rename command (which is completely different to the rename
command from util-linux
):
rename -v 's/^(d4)(d2)(d2)/$1-$2-$3/' 2015*
(use -n
rather than -v
for a dry-run to test the command first).
This perl version of rename
may be called prename
or file-rename
on your system. It is far more capable and useful than the util-linux
version of rename
. If you don't have it installed, you should install it. If it isn't packaged for your distro, you can install from CPAN
BTW, you can tell if you already have it installed by running rename -V
. If it produces output like this:
$ rename -V
Unknown option: V
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
or this:
$ rename -V
/usr/bin/rename using File::Rename version 0.20
Then you have perl rename
installed. The former indicates an old version (which AFAIK lives on only on pre-jessie debian installs, included as part of the perl
package). The latter indicates the current version (now a separate package called rename
).
Using the perl
rename command (which is completely different to the rename
command from util-linux
):
rename -v 's/^(d4)(d2)(d2)/$1-$2-$3/' 2015*
(use -n
rather than -v
for a dry-run to test the command first).
This perl version of rename
may be called prename
or file-rename
on your system. It is far more capable and useful than the util-linux
version of rename
. If you don't have it installed, you should install it. If it isn't packaged for your distro, you can install from CPAN
BTW, you can tell if you already have it installed by running rename -V
. If it produces output like this:
$ rename -V
Unknown option: V
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
or this:
$ rename -V
/usr/bin/rename using File::Rename version 0.20
Then you have perl rename
installed. The former indicates an old version (which AFAIK lives on only on pre-jessie debian installs, included as part of the perl
package). The latter indicates the current version (now a separate package called rename
).
edited May 20 '16 at 5:20
answered May 20 '16 at 5:15
cas
38.3k44898
38.3k44898
wooops, I did not realize I was using aperl
implementation ofrename
. Installed this package using homebrew viabrew install rename
, now doingbrew info rename
prompts"Perl-powered file rename script"
– Adrien Be
May 27 '16 at 7:24
you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
– cas
May 27 '16 at 7:32
add a comment |
wooops, I did not realize I was using aperl
implementation ofrename
. Installed this package using homebrew viabrew install rename
, now doingbrew info rename
prompts"Perl-powered file rename script"
– Adrien Be
May 27 '16 at 7:24
you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
– cas
May 27 '16 at 7:32
wooops, I did not realize I was using a
perl
implementation of rename
. Installed this package using homebrew via brew install rename
, now doing brew info rename
prompts "Perl-powered file rename script"
– Adrien Be
May 27 '16 at 7:24
wooops, I did not realize I was using a
perl
implementation of rename
. Installed this package using homebrew via brew install rename
, now doing brew info rename
prompts "Perl-powered file rename script"
– Adrien Be
May 27 '16 at 7:24
you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
– cas
May 27 '16 at 7:32
you might also be interested in this other answer i wrote about perl rename: unix.stackexchange.com/a/283606/7696
– cas
May 27 '16 at 7:32
add a comment |
up vote
2
down vote
With sed
:
LC_ALL=C sed -e 's/([0-9]4)([0-9]2)([0-9]2)/1_2_3/' <file
1
How does this rename a file?
– Wildcard
May 20 '16 at 5:27
This one just format the text, it does not rename the file
– cuonglm
May 20 '16 at 5:34
add a comment |
up vote
2
down vote
With sed
:
LC_ALL=C sed -e 's/([0-9]4)([0-9]2)([0-9]2)/1_2_3/' <file
1
How does this rename a file?
– Wildcard
May 20 '16 at 5:27
This one just format the text, it does not rename the file
– cuonglm
May 20 '16 at 5:34
add a comment |
up vote
2
down vote
up vote
2
down vote
With sed
:
LC_ALL=C sed -e 's/([0-9]4)([0-9]2)([0-9]2)/1_2_3/' <file
With sed
:
LC_ALL=C sed -e 's/([0-9]4)([0-9]2)([0-9]2)/1_2_3/' <file
answered May 20 '16 at 4:02
cuonglm
101k23196297
101k23196297
1
How does this rename a file?
– Wildcard
May 20 '16 at 5:27
This one just format the text, it does not rename the file
– cuonglm
May 20 '16 at 5:34
add a comment |
1
How does this rename a file?
– Wildcard
May 20 '16 at 5:27
This one just format the text, it does not rename the file
– cuonglm
May 20 '16 at 5:34
1
1
How does this rename a file?
– Wildcard
May 20 '16 at 5:27
How does this rename a file?
– Wildcard
May 20 '16 at 5:27
This one just format the text, it does not rename the file
– cuonglm
May 20 '16 at 5:34
This one just format the text, it does not rename the file
– cuonglm
May 20 '16 at 5:34
add a comment |
up vote
2
down vote
While rename
is a very powerful tool, I normally prefer the simplicity of the mmv
(multiple move) utility:
mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9'
The ?
in the search pattern stands for a single character, the *
for an arbitrarily long sequence of characters. In the replacement pattern, every #<number>
stands for a corresponding ?
or *
in the search pattern. In addition to ?
and *
, mmv
supports character ranges within brackets (like [a-f]
).
(mmv
will test for any conflicts in renaming before it starts work, so you do not risk losing files by overwriting.)
add a comment |
up vote
2
down vote
While rename
is a very powerful tool, I normally prefer the simplicity of the mmv
(multiple move) utility:
mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9'
The ?
in the search pattern stands for a single character, the *
for an arbitrarily long sequence of characters. In the replacement pattern, every #<number>
stands for a corresponding ?
or *
in the search pattern. In addition to ?
and *
, mmv
supports character ranges within brackets (like [a-f]
).
(mmv
will test for any conflicts in renaming before it starts work, so you do not risk losing files by overwriting.)
add a comment |
up vote
2
down vote
up vote
2
down vote
While rename
is a very powerful tool, I normally prefer the simplicity of the mmv
(multiple move) utility:
mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9'
The ?
in the search pattern stands for a single character, the *
for an arbitrarily long sequence of characters. In the replacement pattern, every #<number>
stands for a corresponding ?
or *
in the search pattern. In addition to ?
and *
, mmv
supports character ranges within brackets (like [a-f]
).
(mmv
will test for any conflicts in renaming before it starts work, so you do not risk losing files by overwriting.)
While rename
is a very powerful tool, I normally prefer the simplicity of the mmv
(multiple move) utility:
mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9'
The ?
in the search pattern stands for a single character, the *
for an arbitrarily long sequence of characters. In the replacement pattern, every #<number>
stands for a corresponding ?
or *
in the search pattern. In addition to ?
and *
, mmv
supports character ranges within brackets (like [a-f]
).
(mmv
will test for any conflicts in renaming before it starts work, so you do not risk losing files by overwriting.)
answered May 20 '16 at 7:11
Dubu
2,3661122
2,3661122
add a comment |
add a comment |
up vote
2
down vote
Using bash's built-in substring expansion:
for f in 2015* ; do
mv "$f" "$f::4-$f:4:2-$f:6"
done
add a comment |
up vote
2
down vote
Using bash's built-in substring expansion:
for f in 2015* ; do
mv "$f" "$f::4-$f:4:2-$f:6"
done
add a comment |
up vote
2
down vote
up vote
2
down vote
Using bash's built-in substring expansion:
for f in 2015* ; do
mv "$f" "$f::4-$f:4:2-$f:6"
done
Using bash's built-in substring expansion:
for f in 2015* ; do
mv "$f" "$f::4-$f:4:2-$f:6"
done
answered May 20 '16 at 7:16
JigglyNaga
3,469828
3,469828
add a comment |
add a comment |
up vote
0
down vote
On my distribution I have the perl-rename
command, which can use a perl-style regex to bulk-rename files. The rename
command only accepts a pair of fixed strings for the rename.
add a comment |
up vote
0
down vote
On my distribution I have the perl-rename
command, which can use a perl-style regex to bulk-rename files. The rename
command only accepts a pair of fixed strings for the rename.
add a comment |
up vote
0
down vote
up vote
0
down vote
On my distribution I have the perl-rename
command, which can use a perl-style regex to bulk-rename files. The rename
command only accepts a pair of fixed strings for the rename.
On my distribution I have the perl-rename
command, which can use a perl-style regex to bulk-rename files. The rename
command only accepts a pair of fixed strings for the rename.
answered May 19 '16 at 22:05
Chris Elston
565
565
add a comment |
add a comment |
up vote
0
down vote
quick and dirty not full solution
#!/usr/bin/env bash
str=$1
yyyy=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 1, 4)')
mm=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 5, 2)')
dd=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 7, 2)')
new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' 'print $2'`
echo $new_str
output:
$ bash script.sh '20150716_something-here'
2015-07-16_something-here
add a comment |
up vote
0
down vote
quick and dirty not full solution
#!/usr/bin/env bash
str=$1
yyyy=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 1, 4)')
mm=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 5, 2)')
dd=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 7, 2)')
new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' 'print $2'`
echo $new_str
output:
$ bash script.sh '20150716_something-here'
2015-07-16_something-here
add a comment |
up vote
0
down vote
up vote
0
down vote
quick and dirty not full solution
#!/usr/bin/env bash
str=$1
yyyy=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 1, 4)')
mm=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 5, 2)')
dd=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 7, 2)')
new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' 'print $2'`
echo $new_str
output:
$ bash script.sh '20150716_something-here'
2015-07-16_something-here
quick and dirty not full solution
#!/usr/bin/env bash
str=$1
yyyy=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 1, 4)')
mm=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 5, 2)')
dd=$(echo "$str" | awk -F '_' 'print $1' | awk 'print substr($0, 7, 2)')
new_str=$yyyy-$mm-$dd'_'`echo $str | awk -F'_' 'print $2'`
echo $new_str
output:
$ bash script.sh '20150716_something-here'
2015-07-16_something-here
edited May 20 '16 at 4:13
cuonglm
101k23196297
101k23196297
answered May 20 '16 at 2:21
cuongnv23
745
745
add a comment |
add a comment |
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%2f284282%2frename-files-add-dashes-after-fourth-and-sixth-characters%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