How to get last N commands from history?
I want to see what are the last N commands in my history. I thought history | tail -n 5 would make it, but I noticed that a multiline command counts for as many lines as it has.
$ echo "hello
how are you"
$ history | tail -2
how are you"
1051 history | tail -2
So my question is: do I have to parse the output of the command to accomplish this?
bash command-line command-history
add a comment |
I want to see what are the last N commands in my history. I thought history | tail -n 5 would make it, but I noticed that a multiline command counts for as many lines as it has.
$ echo "hello
how are you"
$ history | tail -2
how are you"
1051 history | tail -2
So my question is: do I have to parse the output of the command to accomplish this?
bash command-line command-history
1
// , This is why I love stackexchange
– Nathan Basanese
Mar 7 '18 at 20:53
add a comment |
I want to see what are the last N commands in my history. I thought history | tail -n 5 would make it, but I noticed that a multiline command counts for as many lines as it has.
$ echo "hello
how are you"
$ history | tail -2
how are you"
1051 history | tail -2
So my question is: do I have to parse the output of the command to accomplish this?
bash command-line command-history
I want to see what are the last N commands in my history. I thought history | tail -n 5 would make it, but I noticed that a multiline command counts for as many lines as it has.
$ echo "hello
how are you"
$ history | tail -2
how are you"
1051 history | tail -2
So my question is: do I have to parse the output of the command to accomplish this?
bash command-line command-history
bash command-line command-history
edited Jun 29 '15 at 22:37
Gilles
534k12810791596
534k12810791596
asked Jun 29 '15 at 14:28
fedorquifedorqui
4,11222056
4,11222056
1
// , This is why I love stackexchange
– Nathan Basanese
Mar 7 '18 at 20:53
add a comment |
1
// , This is why I love stackexchange
– Nathan Basanese
Mar 7 '18 at 20:53
1
1
// , This is why I love stackexchange
– Nathan Basanese
Mar 7 '18 at 20:53
// , This is why I love stackexchange
– Nathan Basanese
Mar 7 '18 at 20:53
add a comment |
3 Answers
3
active
oldest
votes
I found it!
history [n]
An argument of n lists only the last n lines.
$ echo "hello
how are you"
$ history 2
1060 echo "hello
how are you"
1061 history 2
3
That seems to be fortcsh,yashandbash. You may want to give the corresponding information for other shells likezsh,fish,ksh, and the POSIX way.
– Stéphane Chazelas
Jun 29 '15 at 14:59
1
I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799
– fedorqui
Jun 29 '15 at 16:01
5
fc -l -2works too (but doesn't add currentfc -l -2to output)
– Evgeny Vereshchagin
Jun 30 '15 at 3:44
See also: How to stop bash replacing commands in history with asterisks ? (‘*’).
– Evgeny Vereshchagin
Jun 30 '15 at 4:57
fcby the way shows the last n commands with-n. Showingfrom n commands until lastis onlyn(which does not make much sense because you need to know the total number). In german we would sayein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use-las @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.
– Timo
Mar 13 '18 at 20:44
add a comment |
Tested in OpenBSD 6.3 (PD KSH v5.2.14 99/07/13.2).
history [b]
Shows all the history beginning from an entry with a number [b]
history [b] [e]
Shows the history interval from [b] to [e]
add a comment |
When you apply history it will show last history command as well. To prevent that space waster such alias could be handy:
alias hs='history 16 | head -n 15'
(the command itself history 16 | head -n 15)
Another useful history alias:
alias hsg='history | grep '
(when ctr+R is too small to choose from)
New contributor
Alexey K. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f212872%2fhow-to-get-last-n-commands-from-history%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I found it!
history [n]
An argument of n lists only the last n lines.
$ echo "hello
how are you"
$ history 2
1060 echo "hello
how are you"
1061 history 2
3
That seems to be fortcsh,yashandbash. You may want to give the corresponding information for other shells likezsh,fish,ksh, and the POSIX way.
– Stéphane Chazelas
Jun 29 '15 at 14:59
1
I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799
– fedorqui
Jun 29 '15 at 16:01
5
fc -l -2works too (but doesn't add currentfc -l -2to output)
– Evgeny Vereshchagin
Jun 30 '15 at 3:44
See also: How to stop bash replacing commands in history with asterisks ? (‘*’).
– Evgeny Vereshchagin
Jun 30 '15 at 4:57
fcby the way shows the last n commands with-n. Showingfrom n commands until lastis onlyn(which does not make much sense because you need to know the total number). In german we would sayein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use-las @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.
– Timo
Mar 13 '18 at 20:44
add a comment |
I found it!
history [n]
An argument of n lists only the last n lines.
$ echo "hello
how are you"
$ history 2
1060 echo "hello
how are you"
1061 history 2
3
That seems to be fortcsh,yashandbash. You may want to give the corresponding information for other shells likezsh,fish,ksh, and the POSIX way.
– Stéphane Chazelas
Jun 29 '15 at 14:59
1
I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799
– fedorqui
Jun 29 '15 at 16:01
5
fc -l -2works too (but doesn't add currentfc -l -2to output)
– Evgeny Vereshchagin
Jun 30 '15 at 3:44
See also: How to stop bash replacing commands in history with asterisks ? (‘*’).
– Evgeny Vereshchagin
Jun 30 '15 at 4:57
fcby the way shows the last n commands with-n. Showingfrom n commands until lastis onlyn(which does not make much sense because you need to know the total number). In german we would sayein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use-las @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.
– Timo
Mar 13 '18 at 20:44
add a comment |
I found it!
history [n]
An argument of n lists only the last n lines.
$ echo "hello
how are you"
$ history 2
1060 echo "hello
how are you"
1061 history 2
I found it!
history [n]
An argument of n lists only the last n lines.
$ echo "hello
how are you"
$ history 2
1060 echo "hello
how are you"
1061 history 2
answered Jun 29 '15 at 14:28
fedorquifedorqui
4,11222056
4,11222056
3
That seems to be fortcsh,yashandbash. You may want to give the corresponding information for other shells likezsh,fish,ksh, and the POSIX way.
– Stéphane Chazelas
Jun 29 '15 at 14:59
1
I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799
– fedorqui
Jun 29 '15 at 16:01
5
fc -l -2works too (but doesn't add currentfc -l -2to output)
– Evgeny Vereshchagin
Jun 30 '15 at 3:44
See also: How to stop bash replacing commands in history with asterisks ? (‘*’).
– Evgeny Vereshchagin
Jun 30 '15 at 4:57
fcby the way shows the last n commands with-n. Showingfrom n commands until lastis onlyn(which does not make much sense because you need to know the total number). In german we would sayein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use-las @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.
– Timo
Mar 13 '18 at 20:44
add a comment |
3
That seems to be fortcsh,yashandbash. You may want to give the corresponding information for other shells likezsh,fish,ksh, and the POSIX way.
– Stéphane Chazelas
Jun 29 '15 at 14:59
1
I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799
– fedorqui
Jun 29 '15 at 16:01
5
fc -l -2works too (but doesn't add currentfc -l -2to output)
– Evgeny Vereshchagin
Jun 30 '15 at 3:44
See also: How to stop bash replacing commands in history with asterisks ? (‘*’).
– Evgeny Vereshchagin
Jun 30 '15 at 4:57
fcby the way shows the last n commands with-n. Showingfrom n commands until lastis onlyn(which does not make much sense because you need to know the total number). In german we would sayein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use-las @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.
– Timo
Mar 13 '18 at 20:44
3
3
That seems to be for
tcsh, yash and bash. You may want to give the corresponding information for other shells like zsh, fish, ksh, and the POSIX way.– Stéphane Chazelas
Jun 29 '15 at 14:59
That seems to be for
tcsh, yash and bash. You may want to give the corresponding information for other shells like zsh, fish, ksh, and the POSIX way.– Stéphane Chazelas
Jun 29 '15 at 14:59
1
1
I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799
– fedorqui
Jun 29 '15 at 16:01
I would love to, although I don't have any of these installed in my computer. Do you recommend any specific source to get the information from? In opengroup I see a reference to pubs.opengroup.org/onlinepubs/9699919799
– fedorqui
Jun 29 '15 at 16:01
5
5
fc -l -2 works too (but doesn't add current fc -l -2 to output)– Evgeny Vereshchagin
Jun 30 '15 at 3:44
fc -l -2 works too (but doesn't add current fc -l -2 to output)– Evgeny Vereshchagin
Jun 30 '15 at 3:44
See also: How to stop bash replacing commands in history with asterisks ? (‘*’).
– Evgeny Vereshchagin
Jun 30 '15 at 4:57
See also: How to stop bash replacing commands in history with asterisks ? (‘*’).
– Evgeny Vereshchagin
Jun 30 '15 at 4:57
fc by the way shows the last n commands with -n. Showing from n commands until last is only n (which does not make much sense because you need to know the total number). In german we would say ein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use -l as @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.– Timo
Mar 13 '18 at 20:44
fc by the way shows the last n commands with -n. Showing from n commands until last is only n (which does not make much sense because you need to know the total number). In german we would say ein kleiner aber feiner unterschied: A small but delight diff. Do not forget to use -l as @EvgenyVereshchagin pointed already because otherwise you end up in an editor which is not what you want.– Timo
Mar 13 '18 at 20:44
add a comment |
Tested in OpenBSD 6.3 (PD KSH v5.2.14 99/07/13.2).
history [b]
Shows all the history beginning from an entry with a number [b]
history [b] [e]
Shows the history interval from [b] to [e]
add a comment |
Tested in OpenBSD 6.3 (PD KSH v5.2.14 99/07/13.2).
history [b]
Shows all the history beginning from an entry with a number [b]
history [b] [e]
Shows the history interval from [b] to [e]
add a comment |
Tested in OpenBSD 6.3 (PD KSH v5.2.14 99/07/13.2).
history [b]
Shows all the history beginning from an entry with a number [b]
history [b] [e]
Shows the history interval from [b] to [e]
Tested in OpenBSD 6.3 (PD KSH v5.2.14 99/07/13.2).
history [b]
Shows all the history beginning from an entry with a number [b]
history [b] [e]
Shows the history interval from [b] to [e]
answered Oct 12 '18 at 15:41
CrimsonPermanentAssurerCrimsonPermanentAssurer
112
112
add a comment |
add a comment |
When you apply history it will show last history command as well. To prevent that space waster such alias could be handy:
alias hs='history 16 | head -n 15'
(the command itself history 16 | head -n 15)
Another useful history alias:
alias hsg='history | grep '
(when ctr+R is too small to choose from)
New contributor
Alexey K. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
When you apply history it will show last history command as well. To prevent that space waster such alias could be handy:
alias hs='history 16 | head -n 15'
(the command itself history 16 | head -n 15)
Another useful history alias:
alias hsg='history | grep '
(when ctr+R is too small to choose from)
New contributor
Alexey K. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
When you apply history it will show last history command as well. To prevent that space waster such alias could be handy:
alias hs='history 16 | head -n 15'
(the command itself history 16 | head -n 15)
Another useful history alias:
alias hsg='history | grep '
(when ctr+R is too small to choose from)
New contributor
Alexey K. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
When you apply history it will show last history command as well. To prevent that space waster such alias could be handy:
alias hs='history 16 | head -n 15'
(the command itself history 16 | head -n 15)
Another useful history alias:
alias hsg='history | grep '
(when ctr+R is too small to choose from)
New contributor
Alexey K. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Alexey K. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 6 mins ago
Alexey K.Alexey K.
1
1
New contributor
Alexey K. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Alexey K. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Alexey K. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f212872%2fhow-to-get-last-n-commands-from-history%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
// , This is why I love stackexchange
– Nathan Basanese
Mar 7 '18 at 20:53