How to read multiples arguments per line for a bash command?

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a file (ordered_names) which is in the format
pub 000.html
pub.19 001.html
for about 300 lines, And I can't find a way to feed this to the mv command.
I have read Provide strings stored in a file as a list of arguments to a command?, but I could not get what I came for.
Here are some of the attempts I made :
for line in "$(cat ../ordered_files.reversed)"; do mv $(echo "$line"); done
for line in "$(cat ../ordered_files.reversed)"; do echo mv $(echo $line | cut -d' ' -f 1) $(echo $line | cut -d' ' -f 2) ; done
mv for
add a comment |Â
up vote
1
down vote
favorite
I have a file (ordered_names) which is in the format
pub 000.html
pub.19 001.html
for about 300 lines, And I can't find a way to feed this to the mv command.
I have read Provide strings stored in a file as a list of arguments to a command?, but I could not get what I came for.
Here are some of the attempts I made :
for line in "$(cat ../ordered_files.reversed)"; do mv $(echo "$line"); done
for line in "$(cat ../ordered_files.reversed)"; do echo mv $(echo $line | cut -d' ' -f 1) $(echo $line | cut -d' ' -f 2) ; done
mv for
This is BashFAQ 001. See also Why is looping over find's output bad practice?. The basic problem is the same, but of coursefind -execdoesn't work with cat.
â ilkkachu
Dec 16 '17 at 13:37
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a file (ordered_names) which is in the format
pub 000.html
pub.19 001.html
for about 300 lines, And I can't find a way to feed this to the mv command.
I have read Provide strings stored in a file as a list of arguments to a command?, but I could not get what I came for.
Here are some of the attempts I made :
for line in "$(cat ../ordered_files.reversed)"; do mv $(echo "$line"); done
for line in "$(cat ../ordered_files.reversed)"; do echo mv $(echo $line | cut -d' ' -f 1) $(echo $line | cut -d' ' -f 2) ; done
mv for
I have a file (ordered_names) which is in the format
pub 000.html
pub.19 001.html
for about 300 lines, And I can't find a way to feed this to the mv command.
I have read Provide strings stored in a file as a list of arguments to a command?, but I could not get what I came for.
Here are some of the attempts I made :
for line in "$(cat ../ordered_files.reversed)"; do mv $(echo "$line"); done
for line in "$(cat ../ordered_files.reversed)"; do echo mv $(echo $line | cut -d' ' -f 1) $(echo $line | cut -d' ' -f 2) ; done
mv for
asked Dec 16 '17 at 3:21
Pierre Antoine Guillaume
261211
261211
This is BashFAQ 001. See also Why is looping over find's output bad practice?. The basic problem is the same, but of coursefind -execdoesn't work with cat.
â ilkkachu
Dec 16 '17 at 13:37
add a comment |Â
This is BashFAQ 001. See also Why is looping over find's output bad practice?. The basic problem is the same, but of coursefind -execdoesn't work with cat.
â ilkkachu
Dec 16 '17 at 13:37
This is BashFAQ 001. See also Why is looping over find's output bad practice?. The basic problem is the same, but of course
find -exec doesn't work with cat.â ilkkachu
Dec 16 '17 at 13:37
This is BashFAQ 001. See also Why is looping over find's output bad practice?. The basic problem is the same, but of course
find -exec doesn't work with cat.â ilkkachu
Dec 16 '17 at 13:37
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Try:
while read -r file1 file2; do mv -n -- "$file1" "$file2"; done <inputfile
This assumes that the file names on each line in the input file are space-separated. This, of course, only works if the file names themselves do not contain spaces. If they do, then you need a different input format.
How it works
while read -r file1 file2; doThis starts a
whileloop. The loop continues as long as input is available. For each line of input, two parameters are read:file1andfile2.mv -n -- "$file1" "$file2"This moves
file1tofile2.The option
-nprotects you from overwriting any file at the destination. Of course, if it is your intention to overwrite files, remove this option.The string
--signals the end of the options. This protects you from problems should any of the file names start with a-.doneThis signals the end of the
whileloop.<inputfileThis tells the
whileloop to gets its input from a file calledinputfile.
I always recommend usingmv -normv -ifor bulk moves like this, to avoid accidental overwriting (silent & irreversible) of files due to name conflicts. In this case,mv -iwould try to read overwrite confirmations from the file list, which wouldn't work well at all, somv -nis the way to go.
â Gordon Davisson
Dec 16 '17 at 7:24
@GordonDavisson Excellent point. I just added-nadded to the answer.
â John1024
Dec 16 '17 at 7:34
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Try:
while read -r file1 file2; do mv -n -- "$file1" "$file2"; done <inputfile
This assumes that the file names on each line in the input file are space-separated. This, of course, only works if the file names themselves do not contain spaces. If they do, then you need a different input format.
How it works
while read -r file1 file2; doThis starts a
whileloop. The loop continues as long as input is available. For each line of input, two parameters are read:file1andfile2.mv -n -- "$file1" "$file2"This moves
file1tofile2.The option
-nprotects you from overwriting any file at the destination. Of course, if it is your intention to overwrite files, remove this option.The string
--signals the end of the options. This protects you from problems should any of the file names start with a-.doneThis signals the end of the
whileloop.<inputfileThis tells the
whileloop to gets its input from a file calledinputfile.
I always recommend usingmv -normv -ifor bulk moves like this, to avoid accidental overwriting (silent & irreversible) of files due to name conflicts. In this case,mv -iwould try to read overwrite confirmations from the file list, which wouldn't work well at all, somv -nis the way to go.
â Gordon Davisson
Dec 16 '17 at 7:24
@GordonDavisson Excellent point. I just added-nadded to the answer.
â John1024
Dec 16 '17 at 7:34
add a comment |Â
up vote
3
down vote
accepted
Try:
while read -r file1 file2; do mv -n -- "$file1" "$file2"; done <inputfile
This assumes that the file names on each line in the input file are space-separated. This, of course, only works if the file names themselves do not contain spaces. If they do, then you need a different input format.
How it works
while read -r file1 file2; doThis starts a
whileloop. The loop continues as long as input is available. For each line of input, two parameters are read:file1andfile2.mv -n -- "$file1" "$file2"This moves
file1tofile2.The option
-nprotects you from overwriting any file at the destination. Of course, if it is your intention to overwrite files, remove this option.The string
--signals the end of the options. This protects you from problems should any of the file names start with a-.doneThis signals the end of the
whileloop.<inputfileThis tells the
whileloop to gets its input from a file calledinputfile.
I always recommend usingmv -normv -ifor bulk moves like this, to avoid accidental overwriting (silent & irreversible) of files due to name conflicts. In this case,mv -iwould try to read overwrite confirmations from the file list, which wouldn't work well at all, somv -nis the way to go.
â Gordon Davisson
Dec 16 '17 at 7:24
@GordonDavisson Excellent point. I just added-nadded to the answer.
â John1024
Dec 16 '17 at 7:34
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Try:
while read -r file1 file2; do mv -n -- "$file1" "$file2"; done <inputfile
This assumes that the file names on each line in the input file are space-separated. This, of course, only works if the file names themselves do not contain spaces. If they do, then you need a different input format.
How it works
while read -r file1 file2; doThis starts a
whileloop. The loop continues as long as input is available. For each line of input, two parameters are read:file1andfile2.mv -n -- "$file1" "$file2"This moves
file1tofile2.The option
-nprotects you from overwriting any file at the destination. Of course, if it is your intention to overwrite files, remove this option.The string
--signals the end of the options. This protects you from problems should any of the file names start with a-.doneThis signals the end of the
whileloop.<inputfileThis tells the
whileloop to gets its input from a file calledinputfile.
Try:
while read -r file1 file2; do mv -n -- "$file1" "$file2"; done <inputfile
This assumes that the file names on each line in the input file are space-separated. This, of course, only works if the file names themselves do not contain spaces. If they do, then you need a different input format.
How it works
while read -r file1 file2; doThis starts a
whileloop. The loop continues as long as input is available. For each line of input, two parameters are read:file1andfile2.mv -n -- "$file1" "$file2"This moves
file1tofile2.The option
-nprotects you from overwriting any file at the destination. Of course, if it is your intention to overwrite files, remove this option.The string
--signals the end of the options. This protects you from problems should any of the file names start with a-.doneThis signals the end of the
whileloop.<inputfileThis tells the
whileloop to gets its input from a file calledinputfile.
edited Dec 16 '17 at 7:34
answered Dec 16 '17 at 3:27
John1024
44.2k4100116
44.2k4100116
I always recommend usingmv -normv -ifor bulk moves like this, to avoid accidental overwriting (silent & irreversible) of files due to name conflicts. In this case,mv -iwould try to read overwrite confirmations from the file list, which wouldn't work well at all, somv -nis the way to go.
â Gordon Davisson
Dec 16 '17 at 7:24
@GordonDavisson Excellent point. I just added-nadded to the answer.
â John1024
Dec 16 '17 at 7:34
add a comment |Â
I always recommend usingmv -normv -ifor bulk moves like this, to avoid accidental overwriting (silent & irreversible) of files due to name conflicts. In this case,mv -iwould try to read overwrite confirmations from the file list, which wouldn't work well at all, somv -nis the way to go.
â Gordon Davisson
Dec 16 '17 at 7:24
@GordonDavisson Excellent point. I just added-nadded to the answer.
â John1024
Dec 16 '17 at 7:34
I always recommend using
mv -n or mv -i for bulk moves like this, to avoid accidental overwriting (silent & irreversible) of files due to name conflicts. In this case, mv -i would try to read overwrite confirmations from the file list, which wouldn't work well at all, so mv -n is the way to go.â Gordon Davisson
Dec 16 '17 at 7:24
I always recommend using
mv -n or mv -i for bulk moves like this, to avoid accidental overwriting (silent & irreversible) of files due to name conflicts. In this case, mv -i would try to read overwrite confirmations from the file list, which wouldn't work well at all, so mv -n is the way to go.â Gordon Davisson
Dec 16 '17 at 7:24
@GordonDavisson Excellent point. I just added
-n added to the answer.â John1024
Dec 16 '17 at 7:34
@GordonDavisson Excellent point. I just added
-n added to the answer.â John1024
Dec 16 '17 at 7:34
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%2f411195%2fhow-to-read-multiples-arguments-per-line-for-a-bash-command%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
This is BashFAQ 001. See also Why is looping over find's output bad practice?. The basic problem is the same, but of course
find -execdoesn't work with cat.â ilkkachu
Dec 16 '17 at 13:37