History linux without numbers

How can I do the `history` command and not have line numbers so I can copy multiple line commands?

Sometimes I do the history command so I can see 4 commands in a row. I want to copy those commands so I can run them again. I usually select them with the mouse and shift-ctrl-c which works ok, but I also get the line numbers. How can I do history and not have line numbers? I tried the man page but was overwhelmed.

You are probably talking about the shell built-in history command which has no manpage but a help page, run help history to see it.

Actually man history works find on my machine but there is about 20 mins of readable material and I was wondering if anyone knew the answer quickly, SO style.

@Michael-Durant here the history manpage is about the The GNU History Library, I doubt yours is about the shell-builtin.

5 Answers 5

Note that history output may depend on local environment. On my machine it looks as:

 $ history | tail -1 2993 2012-08-13 17:42:17 echo "test" 

and none of your answers will work at my side.

A useful option is fc (usually it is builtin) which works perfectly independent from your local settings.

fc has -n option that suppresses numbers in output. So your command would look like:

it outputs only the several last commands.

To output the whole history in this style use:

To output without spaces at the begin you can use sed to remove them:

Works although gives a lot of spaces at the start of the line. So far I prefer Thor’s answer. Thanks.

@MichaelDurrant you can delete all trailing spaces with sed ‘s/^\s*//’ . I’ve just added an option with it to my answer.

I added the space. Now I notice that, not too surprising, it only works for the first 3 items separated by a space, the rest get ignored.

I’ve actually updated this » awk » answer with that as it is a much better solution. Also I can then reverse my downvote. I don’t like down-votes and use them sparingly.

history | tr -s ' ' | cut -d' ' -f3- 

Edit

Also see rush’s answer about using fc .

Yup and with an alias nhistory=’history | tr -s ‘ ‘ | cut -d’ ‘ -f3-‘ in my ~/.bash_aliases file I now have nhistory for «numberless» history at my fingertips. I was tempted to use nnhistory but I also go for less characters when I can for quicky aliases.

tl; dr: Use history -p \!-

If the only reason you want to copy them is so you can run them again, you may want to simply use history expansion. (Run LESS=’+/^history expansion’ man bash to see the man page for this.)

For instance, let’s say I want to rerun the last four commands. I can just type:

If I’m not completely sure it was the last four commands and I want to verify what those commands were before I run them, I can use the modifier p to print the expansion and add it to my history without actually running it:

(Putting it on any one history expansion is enough; nothing will get run thereby.)

Читайте также:  Kali linux grub error

Now if I’m happy with it, I just press the «up» arrow and press «enter».

If I want to save a command to a file for later reference, I use the -p option to the history command. First I will usually get the history number of the last command, for instance by running history | tail . Let’s say that looks like this:

$ history | tail 1136 cd /tmp 1137 ls 1138 cd - 1139 pwd 1140 cp somefile ~-/ 1141 ping somesite.com 1142 printf '%*.*s\n' 0 "$(tput cols)" "$(printf '%0.1s' -<1..$(tput cols)>)" 1143 for ((i=1;i <=3;i++)); do awk -F: -vi="$i" '> END ' input; done | column -t -s: 1144 cd ~/documents 1145 history | tail 

If I want to save the long for loop command (line 1143) for later use, I can do so easily using history -p as follows:

$ history -p \!1143 >> my_list_of_one_liners.sh 

Note that history -p itself does not appear in the history list.

Also note that history -p can accept multiple arguments, and will expand each on a separate line. So really this is the best answer to your question, which I now realized.

Run history -p \!- and you will see the last four commands you ran printed to your terminal with no preceding numbers, perfect for copying. The 4 can be replaced by any number you like.

Источник

Linux Print History Command Without Line Numbers

Linux history command is used to get previously used commands by the current user. The default size for the history command is 1000 which means that last 1000 command will be stored in history. While listing history of bash line numbers are provided too. This is not expected in some situations. To get history without line numbers following commands can be used.

By default history command is used to print Linux bash history where the commands with the index numbers are printed to the screen. By deafult, only the last 1000 commands are printed.

Print History File with Numbers

We can see that before the command the number of the file is printed to the screen.

Printing .bash_history File Without Line Numbers

Executed bash commands are stored in a file named .bash_history . This file is located in every user’s home directory. For example for user ismail is will be located at /home/ismail/.bash_history . The simplest and easiest way is printing .bash_history file. History file stored commands in a plain format.

Printing .bash_history File

From the output, we can see that only commands are printed from the .bash_history .

Using Awk

awk is a very useful tool used to read strings and filter them according to the given expression. With awk following command can be used to filter with substring. We will remove the command history line numbers with an expression which will get only commands from history command output.

Using Awk

Using Sed

sed is an alternative to the awk . We will use a similar tactic where we will remove line numbers. We can use sed streamline editor to filter line numbers we will remove all numbers from the start of each line up to space in this example.

  • /s used to search
  • ^ used to start of the line
  • [ ]* used to spaces from start
  • 5\+ used for numbers
  • // is used to remove previously given regex which is numbers and spaces from the start of the line.

Using Sed

Using Cut

The cut is the simplest example of removing history line numbers. We will delimit output according to space and print columns start with 2 and others.

  • -d is used to specify the delimiter which is space.
  • -f is used to column number which is from 4 to the end

Using Cut

Linux Print History Command Without Line Numbers Infographic

Источник

Bash history without line numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.

The bash history command is very cool. I understand why it shows the line numbers, but is there a way I can invoke the history command and suppress the line numbers? The point here is to use the history command, so please don’t reply cat ~/.bash_history Current Output:

 529 man history 530 ls 531 ll 532 clear 533 cd ~ 534 history
man history ls ll clear cd ~ history

Historical graphic source. Thanks to everyone for your great solutions. Paul’s is the simplest and will work for me for because my bash history size is set at 2000. I also wanted to share a cool article I found this morning. It has a couple good options that I am now using, like keeping duplicate entries out of the bash history and making sure multiple bash sessions don’t overwrite the history file: http://blog.macromates.com/2008/working-with-history-in-bash/

12 Answers 12

man cut . It’s deleting the first 7 characters of each line of output of the history command. It should only have problems if the number exceeds 99,999, something I’ve never seen (and I use shells a lot). But if you’re concerned about that: history | sed ‘s/^ *9* *//’

I think @Paul R’s solution is what I need. I didn’t realize at first that the history command was padding the line numbers with spaces and now the cut syntax makes more sense 🙂 Thanks @Keith Thompson for your solution that will work for > 100k histories.

@cwd: If you have 100,000 commands in your history, it’s time to back away from the keyboard and go home. If you’re already home, go outside. 8-)> (Yes, I know history can be retained across sessions.)

This answer can fail if you have a long history.

If you were willing to switch to zsh isntead of bash, then zsh supports this natively (as well as other options for history formatting)

Actually fc is a bash builtin as well. The only difference is that the first line is 1 , so it would be fc -ln 1

while other commands work, and while this command isn’t using history (which the OP specifically asked for), I’m upvoting this answer for its simplicity. Trying to remember the syntax for cut can be clunky for me, but this is as easy for me to remember as ls -hal

From output of history —help :

-w write the current history to the history file

It writes current history to specified file — /dev/stdout in this case.

This is actually the simplest one to remember in practise! Clean and simple — plus no knowledge of regex/sed/awk needed off the top of your head 🙂

This isn’t working if you are not root , and you switched to that user with su because root will keep ownership of /dev/stdout , see this very good answer for more detail: unix.stackexchange.com/questions/38538/…. With a user, history -w /dev/tty works, but it’s not exactly the same as stdout .

I’m late on this one, but the shorter method would be to add the following in your ~/.bashrc or ~/.profile file:

HISTTIMEFORMAT If this variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each history entry displayed by the history builtin. If this variable is set, time stamps are written to the history file so they may be preserved across shell sessions. This uses the history comment character to distinguish timestamps from other history lines.

Using this capability, a smart hack consist in making the variable «print» a carriage return ( \r ) and clear the line (ANSI code K ) instead of an actual timestamp.

Alternatively, you could use sed:

Using alias, you can set this as your standard (stick it in your bash_profile):

alias history="history | sed 's/^[ ]*9\+[ ]*//'" 

\+ in a basic regular expression is not POSIX conformant. Use \ if your sed doesn’t support the non-standard \+ extension.

Although cut with the -c option works for most practical purposes, I think that piping history to awk would be a better solution. For example:

Both of these solutions do the same thing. The output of history is being fed to awk. Awk then blanks out the first column, which corresponds to the numbers in the history command’s output. Here awk is more convenient because you don’t have to concern yourself with the number of characters in the number part of the output.

print $0 is equivalent to print , since the default is to print everything that appears on the line. Typing print $0 is more explicit, but which one you choose is up to you. The behavior of print $0 and simply print when used with awk is more evident if you used awk to print a file ( cat would be faster to type instead of awk, but this is for illustrating a point).

[Ex] Using awk to display the contents of a file with $0

$ awk '' /tmp/hello-world.txt Hello World! 

[Ex] Using awk to display the contents of a file without explicit $0

$ awk '' /tmp/hello-world.txt Hello World! 

[Ex] Using awk when the history line spans multiple lines

$ history 11 clear 12 echo "In word processing and desktop publishing, a hard return or paragraph break indicates a new paragraph, to be distinguished from the soft return at the end of a line internal to a paragraph. This distinction allows word wrap to automatically re-flow text as it is edited, without losing paragraph breaks. The software may apply vertical whitespace or indenting at paragraph breaks, depending on the selected style." $ history | awk ' $1=""; ' clear echo "In word processing and desktop publishing, a hard return or paragraph break indicates a new paragraph, to be distinguished from the soft return at the end of a line internal to a paragraph. This distinction allows word wrap to automatically re-flow text as it is edited, without losing paragraph breaks. The software may apply vertical whitespace or indenting at paragraph breaks, depending on the selected style." 

Источник

Оцените статью
Adblock
detector