Thursday, January 16, 2020

Linux Command History Trips



https://www.thegeekstuff.com/2008/08/15-examples-to-master-linux-command-line-history/
1. Display timestamp using HISTTIMEFORMAT
Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timepstamp along with the command as shown below.

# export HISTTIMEFORMAT='%F %T '

https://unix.stackexchange.com/questions/343824/is-it-possible-to-replace-change-an-argument-using-bash-bang-and-history
  • Correct previous command:
    $ mv -r from to
    $ !!:s/-r//
    mv  from to
!:s/-r// can be used instead of !!:s/-r//. The s/<string>/<replacement>/ modifier replaces the first occurrence of <string> with <replacement>.
$ echo -r movableFolder/ targetFolder/
-r movableFolder/ targetFolder/

$ echo !:2*
movableFolder/ targetFolder/
All is documented in man bash:
Word Designators Word designators are used to select desired words from the event. A : separates the event specification from the word designator. It may be omitted if the word designator begins with a ^, $, *, -, or %. Words are numbered from the beginning of the line, with the first word being denoted by 0 (zero). Words are inserted into the current line separated by single spaces.
  0 (zero)  The zeroth word.  For the shell, this is the command word.
  n         The nth word.
  ^         The first argument. That is, word 1.
  $         The last word. This is usually the last argument, but will
            expand to the zeroth word if there is only one word in the line.
  %         The word matched by the most recent `?string?' search.
  x-y       A range of words; `-y' abbreviates `0-y'.
  *         All  of the words but the zeroth. This is a synonym for `1-$'.
            It is not an error to use * if there is just one word in the event;
            the empty string is returned in that case.
  x*        Abbreviates x-$.
  x-        Abbreviates x-$ like x*, but omits the last word.
If a word designator is supplied without an event specification, the previous command is used as the event.
And therefore, after this line has been used:
$ echo -r movableFolder/ targetFolder/
echo -r movableFolder/ targetFolder/
this will work:
$ !:0 !:2*
movableFolder/ targetFolder
https://askubuntu.com/questions/59846/bash-history-search-partial-up-arrow
Put the following lines in your ~/.inputrc:
## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward

https://unix.stackexchange.com/questions/53814/configure-up-arrow-to-browse-through-commands-with-same-initial-characters-rathe
The readline commands that you are looking for are the history-search-* commands:
history-search-forward
Search forward through the history for the string of characters between the start of the current line and the current cursor position (the point). This is a non-incremental search.
history-search-backward
Search backward through the history for the string of characters between the start of the current line and the point. This is a non-incremental search.
Binding these in your .inputrc, like so:
"\e[A": history-search-backward            # arrow up
"\e[B": history-search-forward             # arrow down
will allow you to enter the first characters of a command, and then use the Up and Down keys to move through only those commands in your .bash_history that begin with that string.

For example, entering vi and the Up would take you to the first previous command beginning with vi, like vim somefile. Entering Up would take you to the next previous instance, and so on.
  • For editing long commands, after setting your editor (for example export EDITOR=vim), ctrl-x ctrl-e will open the current command in an editor for multi-line editing. Or in vi style, escape-v.

readline and libedit
support editing command lines entered into interactive CLIs, including history

https://bbs.archlinux.org/viewtopic.php?id=201654
See sabotage's libedit package, for instance, and the musl-libc wiki page on alternative libraries.
I think that Mac OSX uses libedit instead of readline - as shown in the second answer to this stackoverflow question - but I'm not completely sure about that.
I guess there might be some licencing issues instead? http://thread.gmane.org/gmane.comp.db.p … ral/160471. And I'm assuming that libedit is indeed crippled in some way, but then why does OSX use it (apart from it being OSX)?

# History related
ctrl + r (reverse search)
!! (rerun last command)
!* (reuse arguments from previous command)
!$ (use last argument of last command)
shopt -s histappend (allow multiple terminals to write to the history file)
history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -nr | head (list the most used history commands)

# File and navigation
cp /home/foo/realllylongname.cpp{,-old}
cd -
rename 's/text_to_find/been_renamed/' *.txt
export CDPATH='/var/log:~' (variable is used with the cd built-in.)

# Colourize bash

# enable colors
eval "`dircolors -b`"
# force ls to always use color and type indicators
alias ls='ls -hF --color=auto'
# make the dir command work kinda like in windows (long format)
alias dir='ls --color=auto --format=long'
# make grep highlight results using color
export GREP_OPTIONS='--color=auto'

export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;33m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m' # end the info box
export LESS_TERMCAP_so=$'\E[01;42;30m' # begin the info box
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;36m'

# Bash shortcuts
    shopt -s cdspell (corrects typoos)
    ctrl + _ (undo)
    ctrl + arrow (move forward a word)
    ctrl + a (move cursor to start)
    ctrl + e (move cursor to end)
    ctrl + k (cuts everything after the cursor)
    ctrl + l (clears screen)
    ctrl + q (resume command that is in the foreground)
    ctrl + s (pause a long running command in the foreground)
    ctrl + t (swap two characters)
    ctrl + u (cuts everything before the cursor)
    ctrl + x + ctrl + e (opens the command string in an editor so that you can edit it before it runs)
    ctrl + x + * (expand glob/star)
    ctrl + xx (move to the opposite end of the line)
    ctrl + y (pastes from the buffer)
    ctrl + shift + c/v (copy/paste into terminal)

# Running commands in sequence
&& (run second command if the first is successful)
; (run second command regardless of success of first one)

# Redirecting I/O
2>&1 (redirect stdout and stderr to a file)

# check for open ports
echo > /dev/tcp/<server ip>/<port>
`` (use back ticks to shell out)

# Examine executable
which <command>
file <path/to/file>
command -V <some command binary> (tells you whether <some binary> is a built-in, binary or alias)




14. Disable the usage of history using HISTSIZE

If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.
# export HISTSIZE=0
# history
$ echo $HISTFILE
/home/myuser/.bash_history

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts