How to batch cp files and rename by adding prefix in current directory in one command-line without using for loop?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Example:
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
4 directories, 6 files
I want to copy f1.md/f2.md/f3.md
in current directory, and the results would be t-f1.md/t-f2.md/t-f3.md
.(prefix is t-
)
Trying and Hope:
for file in *md;do cp -a $file t-$file;done
will get result but it seems very long by using for
loop. I hope if there is shorter and simple command to get the same results.
shell command-line cp
add a comment |Â
up vote
1
down vote
favorite
Example:
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
4 directories, 6 files
I want to copy f1.md/f2.md/f3.md
in current directory, and the results would be t-f1.md/t-f2.md/t-f3.md
.(prefix is t-
)
Trying and Hope:
for file in *md;do cp -a $file t-$file;done
will get result but it seems very long by using for
loop. I hope if there is shorter and simple command to get the same results.
shell command-line cp
a bit shorter but still using loopfor file in *md;do cp -a ,t-$file ;done
â Ã±ÃÂsýù÷
Nov 5 '17 at 13:31
@ñÃÂsýù÷ Hi, could you explain,t-$file
is the same ast-$file
?
â Jack
Nov 5 '17 at 13:51
1
that's just a brace expansion and expands to$file t-$file
.
â Ã±ÃÂsýù÷
Nov 5 '17 at 14:41
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Example:
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
4 directories, 6 files
I want to copy f1.md/f2.md/f3.md
in current directory, and the results would be t-f1.md/t-f2.md/t-f3.md
.(prefix is t-
)
Trying and Hope:
for file in *md;do cp -a $file t-$file;done
will get result but it seems very long by using for
loop. I hope if there is shorter and simple command to get the same results.
shell command-line cp
Example:
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
4 directories, 6 files
I want to copy f1.md/f2.md/f3.md
in current directory, and the results would be t-f1.md/t-f2.md/t-f3.md
.(prefix is t-
)
Trying and Hope:
for file in *md;do cp -a $file t-$file;done
will get result but it seems very long by using for
loop. I hope if there is shorter and simple command to get the same results.
shell command-line cp
edited Nov 5 '17 at 13:10
asked Nov 5 '17 at 9:33
Jack
354
354
a bit shorter but still using loopfor file in *md;do cp -a ,t-$file ;done
â Ã±ÃÂsýù÷
Nov 5 '17 at 13:31
@ñÃÂsýù÷ Hi, could you explain,t-$file
is the same ast-$file
?
â Jack
Nov 5 '17 at 13:51
1
that's just a brace expansion and expands to$file t-$file
.
â Ã±ÃÂsýù÷
Nov 5 '17 at 14:41
add a comment |Â
a bit shorter but still using loopfor file in *md;do cp -a ,t-$file ;done
â Ã±ÃÂsýù÷
Nov 5 '17 at 13:31
@ñÃÂsýù÷ Hi, could you explain,t-$file
is the same ast-$file
?
â Jack
Nov 5 '17 at 13:51
1
that's just a brace expansion and expands to$file t-$file
.
â Ã±ÃÂsýù÷
Nov 5 '17 at 14:41
a bit shorter but still using loop
for file in *md;do cp -a ,t-$file ;done
â Ã±ÃÂsýù÷
Nov 5 '17 at 13:31
a bit shorter but still using loop
for file in *md;do cp -a ,t-$file ;done
â Ã±ÃÂsýù÷
Nov 5 '17 at 13:31
@ñÃÂsýù÷ Hi, could you explain
,t-$file
is the same as t-$file
?â Jack
Nov 5 '17 at 13:51
@ñÃÂsýù÷ Hi, could you explain
,t-$file
is the same as t-$file
?â Jack
Nov 5 '17 at 13:51
1
1
that's just a brace expansion and expands to
$file t-$file
.â Ã±ÃÂsýù÷
Nov 5 '17 at 14:41
that's just a brace expansion and expands to
$file t-$file
.â Ã±ÃÂsýù÷
Nov 5 '17 at 14:41
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
The shortest way with GNU parallel
:
parallel --no-notice 'cp "" "t-"' ::: *.md
https://www.gnu.org/software/parallel/parallel_tutorial.html
add a comment |Â
up vote
1
down vote
To copy the files the loop you already have is the shortest way to do it. If you want to do it without the loop you could combine cp
rename
and mv
mkdir tmp
cp -a *.md tmp
(cd tmp && rename '' 't-' tmp/*.md)
mv temp/*.md .
This reduces the commands you have to run (only one cp command vs one for each file), this may or may not make the program faster to run - you would need to benchmark it. But will fail if you have a very large number of files as we pass all of the files to the cp
rename
and mv
.
add a comment |Â
up vote
0
down vote
With mmv
/ mcp
:
mcp '*.md' t-#1.md
Ex. given
$ tree .
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
0 directories, 10 files
Then
$ mcp -v '*.md' t-#1.md
f1.md -> t-f1.md : done
f2.md -> t-f2.md : done
f3.md -> t-f3.md : done
resulting in
$ tree .
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
âÂÂâÂÂâ t-f1.md
âÂÂâÂÂâ t-f2.md
âÂÂâÂÂâ t-f3.md
0 directories, 13 files
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The shortest way with GNU parallel
:
parallel --no-notice 'cp "" "t-"' ::: *.md
https://www.gnu.org/software/parallel/parallel_tutorial.html
add a comment |Â
up vote
2
down vote
The shortest way with GNU parallel
:
parallel --no-notice 'cp "" "t-"' ::: *.md
https://www.gnu.org/software/parallel/parallel_tutorial.html
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The shortest way with GNU parallel
:
parallel --no-notice 'cp "" "t-"' ::: *.md
https://www.gnu.org/software/parallel/parallel_tutorial.html
The shortest way with GNU parallel
:
parallel --no-notice 'cp "" "t-"' ::: *.md
https://www.gnu.org/software/parallel/parallel_tutorial.html
edited Nov 5 '17 at 11:09
answered Nov 5 '17 at 9:58
RomanPerekhrest
22.5k12145
22.5k12145
add a comment |Â
add a comment |Â
up vote
1
down vote
To copy the files the loop you already have is the shortest way to do it. If you want to do it without the loop you could combine cp
rename
and mv
mkdir tmp
cp -a *.md tmp
(cd tmp && rename '' 't-' tmp/*.md)
mv temp/*.md .
This reduces the commands you have to run (only one cp command vs one for each file), this may or may not make the program faster to run - you would need to benchmark it. But will fail if you have a very large number of files as we pass all of the files to the cp
rename
and mv
.
add a comment |Â
up vote
1
down vote
To copy the files the loop you already have is the shortest way to do it. If you want to do it without the loop you could combine cp
rename
and mv
mkdir tmp
cp -a *.md tmp
(cd tmp && rename '' 't-' tmp/*.md)
mv temp/*.md .
This reduces the commands you have to run (only one cp command vs one for each file), this may or may not make the program faster to run - you would need to benchmark it. But will fail if you have a very large number of files as we pass all of the files to the cp
rename
and mv
.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
To copy the files the loop you already have is the shortest way to do it. If you want to do it without the loop you could combine cp
rename
and mv
mkdir tmp
cp -a *.md tmp
(cd tmp && rename '' 't-' tmp/*.md)
mv temp/*.md .
This reduces the commands you have to run (only one cp command vs one for each file), this may or may not make the program faster to run - you would need to benchmark it. But will fail if you have a very large number of files as we pass all of the files to the cp
rename
and mv
.
To copy the files the loop you already have is the shortest way to do it. If you want to do it without the loop you could combine cp
rename
and mv
mkdir tmp
cp -a *.md tmp
(cd tmp && rename '' 't-' tmp/*.md)
mv temp/*.md .
This reduces the commands you have to run (only one cp command vs one for each file), this may or may not make the program faster to run - you would need to benchmark it. But will fail if you have a very large number of files as we pass all of the files to the cp
rename
and mv
.
edited Nov 5 '17 at 10:14
answered Nov 5 '17 at 9:58
Michael Daffin
2,5801517
2,5801517
add a comment |Â
add a comment |Â
up vote
0
down vote
With mmv
/ mcp
:
mcp '*.md' t-#1.md
Ex. given
$ tree .
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
0 directories, 10 files
Then
$ mcp -v '*.md' t-#1.md
f1.md -> t-f1.md : done
f2.md -> t-f2.md : done
f3.md -> t-f3.md : done
resulting in
$ tree .
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
âÂÂâÂÂâ t-f1.md
âÂÂâÂÂâ t-f2.md
âÂÂâÂÂâ t-f3.md
0 directories, 13 files
add a comment |Â
up vote
0
down vote
With mmv
/ mcp
:
mcp '*.md' t-#1.md
Ex. given
$ tree .
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
0 directories, 10 files
Then
$ mcp -v '*.md' t-#1.md
f1.md -> t-f1.md : done
f2.md -> t-f2.md : done
f3.md -> t-f3.md : done
resulting in
$ tree .
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
âÂÂâÂÂâ t-f1.md
âÂÂâÂÂâ t-f2.md
âÂÂâÂÂâ t-f3.md
0 directories, 13 files
add a comment |Â
up vote
0
down vote
up vote
0
down vote
With mmv
/ mcp
:
mcp '*.md' t-#1.md
Ex. given
$ tree .
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
0 directories, 10 files
Then
$ mcp -v '*.md' t-#1.md
f1.md -> t-f1.md : done
f2.md -> t-f2.md : done
f3.md -> t-f3.md : done
resulting in
$ tree .
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
âÂÂâÂÂâ t-f1.md
âÂÂâÂÂâ t-f2.md
âÂÂâÂÂâ t-f3.md
0 directories, 13 files
With mmv
/ mcp
:
mcp '*.md' t-#1.md
Ex. given
$ tree .
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
0 directories, 10 files
Then
$ mcp -v '*.md' t-#1.md
f1.md -> t-f1.md : done
f2.md -> t-f2.md : done
f3.md -> t-f3.md : done
resulting in
$ tree .
.
âÂÂâÂÂâ f1.md
âÂÂâÂÂâ f2.md
âÂÂâÂÂâ f3.md
âÂÂâÂÂâ f4.txt
âÂÂâÂÂâ f5.csv
âÂÂâÂÂâ f6.doc
âÂÂâÂÂâ s1
âÂÂâÂÂâ s2
âÂÂâÂÂâ s3
âÂÂâÂÂâ s4
âÂÂâÂÂâ t-f1.md
âÂÂâÂÂâ t-f2.md
âÂÂâÂÂâ t-f3.md
0 directories, 13 files
answered Nov 5 '17 at 14:03
steeldriver
32k34979
32k34979
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402616%2fhow-to-batch-cp-files-and-rename-by-adding-prefix-in-current-directory-in-one-co%23new-answer', 'question_page');
);
Post as a guest
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
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
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
a bit shorter but still using loop
for file in *md;do cp -a ,t-$file ;done
â Ã±ÃÂsýù÷
Nov 5 '17 at 13:31
@ñÃÂsýù÷ Hi, could you explain
,t-$file
is the same ast-$file
?â Jack
Nov 5 '17 at 13:51
1
that's just a brace expansion and expands to
$file t-$file
.â Ã±ÃÂsýù÷
Nov 5 '17 at 14:41