When running a command on a file, is there any way to reference the file without typing the entire name?...












8
















This question already has an answer here:




  • Supply the same filename as argument to two commands [duplicate]

    1 answer



  • Run two commands on one argument (without scripting)

    10 answers



  • How do you recall the last (n-th?) passed argument of the previous command you used with bash?

    5 answers




For example:



tar xvf test.tar.gz ;  rm test.tar.gz


Is there a faster way to reference the file name on the second execution?



I was thinking something like this (which is invalid):



tar xvf test.tar.gz ; rm $1


Anything possible?



I'm fully aware of wildcards.










share|improve this question















marked as duplicate by Sergiy Kolodyazhnyy, muru, don_crissti, Jeff Schaller, Stéphane Chazelas bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 16 '18 at 15:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • Or unix.stackexchange.com/questions/139202/…, unix.stackexchange.com/questions/136599/…

    – muru
    Dec 16 '18 at 5:09
















8
















This question already has an answer here:




  • Supply the same filename as argument to two commands [duplicate]

    1 answer



  • Run two commands on one argument (without scripting)

    10 answers



  • How do you recall the last (n-th?) passed argument of the previous command you used with bash?

    5 answers




For example:



tar xvf test.tar.gz ;  rm test.tar.gz


Is there a faster way to reference the file name on the second execution?



I was thinking something like this (which is invalid):



tar xvf test.tar.gz ; rm $1


Anything possible?



I'm fully aware of wildcards.










share|improve this question















marked as duplicate by Sergiy Kolodyazhnyy, muru, don_crissti, Jeff Schaller, Stéphane Chazelas bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 16 '18 at 15:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • Or unix.stackexchange.com/questions/139202/…, unix.stackexchange.com/questions/136599/…

    – muru
    Dec 16 '18 at 5:09














8












8








8


1







This question already has an answer here:




  • Supply the same filename as argument to two commands [duplicate]

    1 answer



  • Run two commands on one argument (without scripting)

    10 answers



  • How do you recall the last (n-th?) passed argument of the previous command you used with bash?

    5 answers




For example:



tar xvf test.tar.gz ;  rm test.tar.gz


Is there a faster way to reference the file name on the second execution?



I was thinking something like this (which is invalid):



tar xvf test.tar.gz ; rm $1


Anything possible?



I'm fully aware of wildcards.










share|improve this question

















This question already has an answer here:




  • Supply the same filename as argument to two commands [duplicate]

    1 answer



  • Run two commands on one argument (without scripting)

    10 answers



  • How do you recall the last (n-th?) passed argument of the previous command you used with bash?

    5 answers




For example:



tar xvf test.tar.gz ;  rm test.tar.gz


Is there a faster way to reference the file name on the second execution?



I was thinking something like this (which is invalid):



tar xvf test.tar.gz ; rm $1


Anything possible?



I'm fully aware of wildcards.





This question already has an answer here:




  • Supply the same filename as argument to two commands [duplicate]

    1 answer



  • Run two commands on one argument (without scripting)

    10 answers



  • How do you recall the last (n-th?) passed argument of the previous command you used with bash?

    5 answers








bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 16 '18 at 0:00









SeldomNeedy

1436




1436










asked Dec 15 '18 at 16:29









user327021user327021

411




411




marked as duplicate by Sergiy Kolodyazhnyy, muru, don_crissti, Jeff Schaller, Stéphane Chazelas bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 16 '18 at 15:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Sergiy Kolodyazhnyy, muru, don_crissti, Jeff Schaller, Stéphane Chazelas bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 16 '18 at 15:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Or unix.stackexchange.com/questions/139202/…, unix.stackexchange.com/questions/136599/…

    – muru
    Dec 16 '18 at 5:09



















  • Or unix.stackexchange.com/questions/139202/…, unix.stackexchange.com/questions/136599/…

    – muru
    Dec 16 '18 at 5:09

















Or unix.stackexchange.com/questions/139202/…, unix.stackexchange.com/questions/136599/…

– muru
Dec 16 '18 at 5:09





Or unix.stackexchange.com/questions/139202/…, unix.stackexchange.com/questions/136599/…

– muru
Dec 16 '18 at 5:09










4 Answers
4






active

oldest

votes


















19














You could assign the filename to a variable first:



f=test.tar.gz; tar xvf "$f"; rm "$f"


Or use the $_ special parameter, it contains the last word of the previous command, which is often (but of course not always) the filename you've been working with:



tar xvf test.tar.gz; rm "$_"


This works with multiple commands too, as long as the filename is always the last argument to the commands (e.g. echo foo; echo $_; echo $_ prints three times foo.)



As an aside, you may want to consider using tar ... && rm ..., i.e. with the && operator instead of a semicolon. That way, the rm will not run if the first command fails.






share|improve this answer


























  • Loved “$_” didn’t knew about it! Thanks a lot for your contribution, well put!

    – Marco
    Dec 15 '18 at 17:58



















4














You can use !$ if you move the second command to a new line.



tar xvf test.tar.gz
rm !$





share|improve this answer


























  • What does it do in general? Is it similar to $_?

    – Solomon Ucko
    Dec 16 '18 at 13:01











  • it inserts last argument from last command in history.

    – RiaD
    Dec 16 '18 at 19:17











  • Do you know what the difference between !$ and $_ is?

    – Solomon Ucko
    Dec 16 '18 at 19:18





















3














In bash version above 4, you can use history expansion to reference the nth positional parameter of the current command. For instance, in tar xvf test.tar.gz that is 2nd positional parameter to the command, thus the command can be reduced to



tar xvf test.tar.gz && rm !#:2


Another, more portable way ( tested with /bin/dash ) is to use $_ variable to reference last positional parameter:



tar xvf test.tar.gz && rm "$_"


See also
How do I execute multiple commands using the same argument?






share|improve this answer































    0














    if you ever used a loop to process multiple files, then you were already using a way that you can also use for just one file:



    for i in a.tgz b.tgz c.tgz; do tar xvf $i; rm $i; done


    for one file:



    for i in a.tgz; do tar xvf $i; rm $i; done


    i find this solution noteworthy because this for loop is a common pattern that you may already be using, but have not thought to apply to this question. it didn't come to my mind either when i initially thought it.






    share|improve this answer






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      19














      You could assign the filename to a variable first:



      f=test.tar.gz; tar xvf "$f"; rm "$f"


      Or use the $_ special parameter, it contains the last word of the previous command, which is often (but of course not always) the filename you've been working with:



      tar xvf test.tar.gz; rm "$_"


      This works with multiple commands too, as long as the filename is always the last argument to the commands (e.g. echo foo; echo $_; echo $_ prints three times foo.)



      As an aside, you may want to consider using tar ... && rm ..., i.e. with the && operator instead of a semicolon. That way, the rm will not run if the first command fails.






      share|improve this answer


























      • Loved “$_” didn’t knew about it! Thanks a lot for your contribution, well put!

        – Marco
        Dec 15 '18 at 17:58
















      19














      You could assign the filename to a variable first:



      f=test.tar.gz; tar xvf "$f"; rm "$f"


      Or use the $_ special parameter, it contains the last word of the previous command, which is often (but of course not always) the filename you've been working with:



      tar xvf test.tar.gz; rm "$_"


      This works with multiple commands too, as long as the filename is always the last argument to the commands (e.g. echo foo; echo $_; echo $_ prints three times foo.)



      As an aside, you may want to consider using tar ... && rm ..., i.e. with the && operator instead of a semicolon. That way, the rm will not run if the first command fails.






      share|improve this answer


























      • Loved “$_” didn’t knew about it! Thanks a lot for your contribution, well put!

        – Marco
        Dec 15 '18 at 17:58














      19












      19








      19







      You could assign the filename to a variable first:



      f=test.tar.gz; tar xvf "$f"; rm "$f"


      Or use the $_ special parameter, it contains the last word of the previous command, which is often (but of course not always) the filename you've been working with:



      tar xvf test.tar.gz; rm "$_"


      This works with multiple commands too, as long as the filename is always the last argument to the commands (e.g. echo foo; echo $_; echo $_ prints three times foo.)



      As an aside, you may want to consider using tar ... && rm ..., i.e. with the && operator instead of a semicolon. That way, the rm will not run if the first command fails.






      share|improve this answer















      You could assign the filename to a variable first:



      f=test.tar.gz; tar xvf "$f"; rm "$f"


      Or use the $_ special parameter, it contains the last word of the previous command, which is often (but of course not always) the filename you've been working with:



      tar xvf test.tar.gz; rm "$_"


      This works with multiple commands too, as long as the filename is always the last argument to the commands (e.g. echo foo; echo $_; echo $_ prints three times foo.)



      As an aside, you may want to consider using tar ... && rm ..., i.e. with the && operator instead of a semicolon. That way, the rm will not run if the first command fails.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 15 '18 at 22:22

























      answered Dec 15 '18 at 16:32









      ilkkachuilkkachu

      56.6k784156




      56.6k784156













      • Loved “$_” didn’t knew about it! Thanks a lot for your contribution, well put!

        – Marco
        Dec 15 '18 at 17:58



















      • Loved “$_” didn’t knew about it! Thanks a lot for your contribution, well put!

        – Marco
        Dec 15 '18 at 17:58

















      Loved “$_” didn’t knew about it! Thanks a lot for your contribution, well put!

      – Marco
      Dec 15 '18 at 17:58





      Loved “$_” didn’t knew about it! Thanks a lot for your contribution, well put!

      – Marco
      Dec 15 '18 at 17:58













      4














      You can use !$ if you move the second command to a new line.



      tar xvf test.tar.gz
      rm !$





      share|improve this answer


























      • What does it do in general? Is it similar to $_?

        – Solomon Ucko
        Dec 16 '18 at 13:01











      • it inserts last argument from last command in history.

        – RiaD
        Dec 16 '18 at 19:17











      • Do you know what the difference between !$ and $_ is?

        – Solomon Ucko
        Dec 16 '18 at 19:18


















      4














      You can use !$ if you move the second command to a new line.



      tar xvf test.tar.gz
      rm !$





      share|improve this answer


























      • What does it do in general? Is it similar to $_?

        – Solomon Ucko
        Dec 16 '18 at 13:01











      • it inserts last argument from last command in history.

        – RiaD
        Dec 16 '18 at 19:17











      • Do you know what the difference between !$ and $_ is?

        – Solomon Ucko
        Dec 16 '18 at 19:18
















      4












      4








      4







      You can use !$ if you move the second command to a new line.



      tar xvf test.tar.gz
      rm !$





      share|improve this answer















      You can use !$ if you move the second command to a new line.



      tar xvf test.tar.gz
      rm !$






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 15 '18 at 22:30









      guntbert

      1,04511017




      1,04511017










      answered Dec 15 '18 at 22:25









      RiaDRiaD

      1415




      1415













      • What does it do in general? Is it similar to $_?

        – Solomon Ucko
        Dec 16 '18 at 13:01











      • it inserts last argument from last command in history.

        – RiaD
        Dec 16 '18 at 19:17











      • Do you know what the difference between !$ and $_ is?

        – Solomon Ucko
        Dec 16 '18 at 19:18





















      • What does it do in general? Is it similar to $_?

        – Solomon Ucko
        Dec 16 '18 at 13:01











      • it inserts last argument from last command in history.

        – RiaD
        Dec 16 '18 at 19:17











      • Do you know what the difference between !$ and $_ is?

        – Solomon Ucko
        Dec 16 '18 at 19:18



















      What does it do in general? Is it similar to $_?

      – Solomon Ucko
      Dec 16 '18 at 13:01





      What does it do in general? Is it similar to $_?

      – Solomon Ucko
      Dec 16 '18 at 13:01













      it inserts last argument from last command in history.

      – RiaD
      Dec 16 '18 at 19:17





      it inserts last argument from last command in history.

      – RiaD
      Dec 16 '18 at 19:17













      Do you know what the difference between !$ and $_ is?

      – Solomon Ucko
      Dec 16 '18 at 19:18







      Do you know what the difference between !$ and $_ is?

      – Solomon Ucko
      Dec 16 '18 at 19:18













      3














      In bash version above 4, you can use history expansion to reference the nth positional parameter of the current command. For instance, in tar xvf test.tar.gz that is 2nd positional parameter to the command, thus the command can be reduced to



      tar xvf test.tar.gz && rm !#:2


      Another, more portable way ( tested with /bin/dash ) is to use $_ variable to reference last positional parameter:



      tar xvf test.tar.gz && rm "$_"


      See also
      How do I execute multiple commands using the same argument?






      share|improve this answer




























        3














        In bash version above 4, you can use history expansion to reference the nth positional parameter of the current command. For instance, in tar xvf test.tar.gz that is 2nd positional parameter to the command, thus the command can be reduced to



        tar xvf test.tar.gz && rm !#:2


        Another, more portable way ( tested with /bin/dash ) is to use $_ variable to reference last positional parameter:



        tar xvf test.tar.gz && rm "$_"


        See also
        How do I execute multiple commands using the same argument?






        share|improve this answer


























          3












          3








          3







          In bash version above 4, you can use history expansion to reference the nth positional parameter of the current command. For instance, in tar xvf test.tar.gz that is 2nd positional parameter to the command, thus the command can be reduced to



          tar xvf test.tar.gz && rm !#:2


          Another, more portable way ( tested with /bin/dash ) is to use $_ variable to reference last positional parameter:



          tar xvf test.tar.gz && rm "$_"


          See also
          How do I execute multiple commands using the same argument?






          share|improve this answer













          In bash version above 4, you can use history expansion to reference the nth positional parameter of the current command. For instance, in tar xvf test.tar.gz that is 2nd positional parameter to the command, thus the command can be reduced to



          tar xvf test.tar.gz && rm !#:2


          Another, more portable way ( tested with /bin/dash ) is to use $_ variable to reference last positional parameter:



          tar xvf test.tar.gz && rm "$_"


          See also
          How do I execute multiple commands using the same argument?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 16 '18 at 4:14









          Sergiy KolodyazhnyySergiy Kolodyazhnyy

          8,53412254




          8,53412254























              0














              if you ever used a loop to process multiple files, then you were already using a way that you can also use for just one file:



              for i in a.tgz b.tgz c.tgz; do tar xvf $i; rm $i; done


              for one file:



              for i in a.tgz; do tar xvf $i; rm $i; done


              i find this solution noteworthy because this for loop is a common pattern that you may already be using, but have not thought to apply to this question. it didn't come to my mind either when i initially thought it.






              share|improve this answer




























                0














                if you ever used a loop to process multiple files, then you were already using a way that you can also use for just one file:



                for i in a.tgz b.tgz c.tgz; do tar xvf $i; rm $i; done


                for one file:



                for i in a.tgz; do tar xvf $i; rm $i; done


                i find this solution noteworthy because this for loop is a common pattern that you may already be using, but have not thought to apply to this question. it didn't come to my mind either when i initially thought it.






                share|improve this answer


























                  0












                  0








                  0







                  if you ever used a loop to process multiple files, then you were already using a way that you can also use for just one file:



                  for i in a.tgz b.tgz c.tgz; do tar xvf $i; rm $i; done


                  for one file:



                  for i in a.tgz; do tar xvf $i; rm $i; done


                  i find this solution noteworthy because this for loop is a common pattern that you may already be using, but have not thought to apply to this question. it didn't come to my mind either when i initially thought it.






                  share|improve this answer













                  if you ever used a loop to process multiple files, then you were already using a way that you can also use for just one file:



                  for i in a.tgz b.tgz c.tgz; do tar xvf $i; rm $i; done


                  for one file:



                  for i in a.tgz; do tar xvf $i; rm $i; done


                  i find this solution noteworthy because this for loop is a common pattern that you may already be using, but have not thought to apply to this question. it didn't come to my mind either when i initially thought it.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 16 '18 at 14:33









                  eMBeeeMBee

                  24824




                  24824















                      Popular posts from this blog

                      Список кардиналов, возведённых папой римским Каликстом III

                      Deduzione

                      Mysql.sock missing - “Can't connect to local MySQL server through socket”