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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












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






share|improve this question




















  • 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















up vote
1
down vote

favorite
1












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






share|improve this question




















  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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






share|improve this question












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








share|improve this question











share|improve this question




share|improve this question










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 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
















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











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; do



    This starts a while loop. The loop continues as long as input is available. For each line of input, two parameters are read: file1 and file2.




  • mv -n -- "$file1" "$file2"



    This moves file1 to file2.



    The option -n protects 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 -.




  • done



    This signals the end of the while loop.




  • <inputfile



    This tells the while loop to gets its input from a file called inputfile.







share|improve this answer






















  • 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










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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; do



    This starts a while loop. The loop continues as long as input is available. For each line of input, two parameters are read: file1 and file2.




  • mv -n -- "$file1" "$file2"



    This moves file1 to file2.



    The option -n protects 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 -.




  • done



    This signals the end of the while loop.




  • <inputfile



    This tells the while loop to gets its input from a file called inputfile.







share|improve this answer






















  • 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














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; do



    This starts a while loop. The loop continues as long as input is available. For each line of input, two parameters are read: file1 and file2.




  • mv -n -- "$file1" "$file2"



    This moves file1 to file2.



    The option -n protects 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 -.




  • done



    This signals the end of the while loop.




  • <inputfile



    This tells the while loop to gets its input from a file called inputfile.







share|improve this answer






















  • 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












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; do



    This starts a while loop. The loop continues as long as input is available. For each line of input, two parameters are read: file1 and file2.




  • mv -n -- "$file1" "$file2"



    This moves file1 to file2.



    The option -n protects 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 -.




  • done



    This signals the end of the while loop.




  • <inputfile



    This tells the while loop to gets its input from a file called inputfile.







share|improve this answer














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; do



    This starts a while loop. The loop continues as long as input is available. For each line of input, two parameters are read: file1 and file2.




  • mv -n -- "$file1" "$file2"



    This moves file1 to file2.



    The option -n protects 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 -.




  • done



    This signals the end of the while loop.




  • <inputfile



    This tells the while loop to gets its input from a file called inputfile.








share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 16 '17 at 7:34

























answered Dec 16 '17 at 3:27









John1024

44.2k4100116




44.2k4100116











  • 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
















  • 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















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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)