When running a command on a file, is there any way to reference the file without typing the entire name?...
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.
bash
marked as duplicate by Sergiy Kolodyazhnyy, muru, don_crissti, Jeff Schaller, Stéphane Chazelas
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.
add a comment |
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.
bash
marked as duplicate by Sergiy Kolodyazhnyy, muru, don_crissti, Jeff Schaller, Stéphane Chazelas
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
add a comment |
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.
bash
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
bash
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
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
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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.
Loved “$_” didn’t knew about it! Thanks a lot for your contribution, well put!
– Marco
Dec 15 '18 at 17:58
add a comment |
You can use !$
if you move the second command to a new line.
tar xvf test.tar.gz
rm !$
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
add a comment |
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?
add a comment |
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.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Loved “$_” didn’t knew about it! Thanks a lot for your contribution, well put!
– Marco
Dec 15 '18 at 17:58
add a comment |
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.
Loved “$_” didn’t knew about it! Thanks a lot for your contribution, well put!
– Marco
Dec 15 '18 at 17:58
add a comment |
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.
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.
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
add a comment |
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
add a comment |
You can use !$
if you move the second command to a new line.
tar xvf test.tar.gz
rm !$
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
add a comment |
You can use !$
if you move the second command to a new line.
tar xvf test.tar.gz
rm !$
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
add a comment |
You can use !$
if you move the second command to a new line.
tar xvf test.tar.gz
rm !$
You can use !$
if you move the second command to a new line.
tar xvf test.tar.gz
rm !$
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
add a comment |
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
add a comment |
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?
add a comment |
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?
add a comment |
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?
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?
answered Dec 16 '18 at 4:14
Sergiy KolodyazhnyySergiy Kolodyazhnyy
8,53412254
8,53412254
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 16 '18 at 14:33
eMBeeeMBee
24824
24824
add a comment |
add a comment |
Or unix.stackexchange.com/questions/139202/…, unix.stackexchange.com/questions/136599/…
– muru
Dec 16 '18 at 5:09