CLI - alex-aleyan/linux_wiki GitHub Wiki
clear history script <filename.txt>
Move:
Ctrl-a Move cursor to the beginning of the line.
Ctrl-e Move cursor to the end of the line.
Ctrl-f Move cursor forward one character; same as the right arrow key.
Ctrl-b Move cursor backward one character; same as the left arrow key.
Alt-f Move cursor forward one word.
Alt-b Move cursor backward one word.
Ctrl-l Clear the screen and move the cursor to the top left corner.
Delece/Yank:
Ctr-d Delete the character at the cursor location.
Alt-Backspace Kill text from the cursor location to the beginning of the current word.
Alt-d Kill text from the cursor location to the end of the current word.
Ctr-u Kill text from the cursor location to the beginning of the line.
Ctr-k Kill text from the cursor location to the end of line.
Ctr-y Yank text from the kill-ring and insert it at the cursor location.
Transpose/Convers:
Ctr-t Transpose (exchange) the character at the cursor location with the one preceding it.
Alt-t Transpose the word at the cursor location with the one preceding it.
Alt-l Convert the characters from the cursor location to the end of the word to lowercase.
Alt-u Convert the characters from the cursor location to the end of the word to uppercase.
Alt-? Display list of possible completions. On most systems you can also do this by pressing the tab key a second time, which is much easier.
Alt-* Insert all possible completions.
Search (Add "stty -ixon" to your bashrc to enable forward search: source):
ctr-r Start history reverse search
ctr-s Start history forward search
ctr-j Cancel history search (same as ctr-c)
Ctr-p Move to the previous history entry. Same action as the up arrow.
Ctr-n Move to the next history entry. Same action as the down arrow.
Alt-< Move to the beginning (top) of the history list.
Alt-> Move to the end (bottom) of the history list, i.e., the current command line.
Alt-p Reverse search, non-incremental. With this key, type in the search string and press enter before the search is performed.
Alt-n Forward search, non-incremental.
Ctr-o Execute the current item in the history list and advance to the next one. This is handy if you are trying to re-execute a sequence of commands in the history list.
History:
!! Repeat the last command. It is probably easier to press up arrow and enter.
!<number> Repeat history list item number.
!<string> Repeat last history list item starting with string.
!?<string> Repeat last history list item containing string.
* any number of any characters character\\
D*
*.txt
/usr/*/share*
[[:%%upper%%:]]*
[!.]
? exactly one character\\
[^a-c]* any word not starting with a,b, or c!
echo $((2 + 2))
echo $(($((5**2)) * 3))
+ Addition
- Subtraction
* Multiplication
/ Division (but remember, since expansion only supports integer arithmetic, results are integers.)
% Modulo, which simply means, “ remainder.”
** Exponentiation
echo {Z..A}
echo data{1..6}
echo a{A{1,2},B{3,4}}b
$,!,&, <space>
\a,\b,\n,\r,\t
- Only these special characters are recognized:
$
\
'
- parameter expansion; Ex:
$1
,$MYVAR
,$((1+5))
- Character escaping and escape sequences; Ex:
echo "The price is \$5.00"
sleep 10; echo -e "Time's up\a"
- command substitutions; Ex:
echo "the system: `uname -a`
same asecho "the system: $(uname -a)
- parameter expansion; Ex:
- These are suppressed:
- word splitting; Ex:
ls -l "two words.txt"
where it's a single parameter, not 2! - pathname expansion; Ex:
*
- tilde expansion (home path); Ex:
~
- brace expansion; Ex:
{1..5}
- word splitting; Ex:
-
\a
Bell (“Alert” - causes the computer to beep); Ex: echo -e "ring a bell\a" -
\b
Backspace; Ex: echo -e "ring a belll\bRing it again" -
\n
Newline. On Unix-like systems, this produces a linefeed; Ex: echo -e "Ring a bell\nRing it again" -
\r
Carriage return; Ex: echo -e "ring a bell\rRing it again" -
\t
Tab; Ex: echo -e "Ring a bell\tRing it again"
- Suppresses any expansions! Treats every character literally!
- A regular expression is a pattern template you define that a Linux utility uses to filter text. A Linux utility (such as the sed editor or the gawk program) matches the regular expression pattern against data as that data flows into the utility. If the data matches the pattern, it’s accepted for processing. If the data doesn’t match the pattern, it’s rejected.
- The regular expression pattern makes use of wildcard characters to represent one or more characters in the data stream.
- A regular expression is implemented using a regular expression engine. A regular expression engine is the underlying software that interprets regular expression patterns and uses those patterns to match text. The 2 popular regular expression engines are:
- The POSIX Basic Regular Expression (BRE) engine (used by sed).
- The POSIX Extended Regular Expression (ERE) engine.
- Metacharacters: used to specify more complex matches (found in programming languages that rely on regular expressions for text filtering like gawk).
- Must be inclosed in single quotes!
-
^ $ . [ ] { } - ? * + ( ) | \
-
.
; Ex:grep -h '.zip'
at least one character and zip! The dot special character is used to match any single character except a newline character.$ cat data6 This is a test of a line. The cat is sleeping. That is a very nice hat. This test is at line four. at ten o'clock we'll go home. $ sed -n '/.at/p' data6 The cat is sleeping. That is a very nice hat. This test is at line four.
-
^
; Ex:grep -h '^zip'
anything starting with zip; anchored at begining! The caret anchor character checks for the pattern at the beginning of each new line of data, as determined by the newline character. As long as the pattern appears at the start of a new line, the caret anchor catches it. If you position the caret character in any place other than at the beginning of the pattern, it acts like a normal character and not as a special character.echo "The book store" | sed -n '/^book/p' echo "Books are great" | sed -n '/^Book/p' echo "This ^ is a test" | sed -n '/s ^/p'
-
$
; Ex:grep -h 'zip$'
anything ending with zip; anchored at end!echo "This is a good book" | sed -n '/book$/p' echo "This book is good" | sed -n '/book$/p' echo "There are a lot of good books" | sed -n '/book$/p'
-
Combining
^$
anchors is a great way to find blank lines and maybe remove them:sed '/^$/d' data5 sed -n '/^this is a test$/p' data4
-
[]
; Ex:grep -h '[bg]zip'
anything matching bzip or gzip anywhere withing the word.-
grep '[^bg]zip'
negation (^ withing []); anything not matching bzip or gzip at the beginning. The square brackets are called a Character Class which limits what characters to match. If one of the characters from the character class is in the data stream, it matches the pattern. A line must contain a character from the character class for match to occure.
$ sed -n '/[ch]at/p' data6 The cat is sleeping. That is a very nice hat. $ echo "Yes" | sed -n '/[Yy]es/p' Yes $ echo "yes" | sed -n '/[Yy]es/p' yes $ echo "Yes" | sed -n '/[Yy][Ee][Ss]/p' Yes $ echo "yEs" | sed -n '/[Yy][Ee][Ss]/p' yEs $ echo "yeS" | sed -n '/[Yy][Ee][Ss]/p' yeS $ cat data7 This line doesn't contain a number. This line has 1 number on it. This line a number 2 on it. This line has a number 4 on it. $ sed -n '/[0123]/p' data7 This line has 1 number on it. This line a number 2 on it. $ sed -n '/[^ch]at/p' data6 This test is at line four.
-
grep -h '^[A-Z].txt'
ranges (- between characters)anything starting with CAPS and ending with .txt. Using the dash symbol, specify a range of characters within a character class. Just specify the first character in the range, a dash, and then the last character in the range. You can also specify multiple, non-continuous ranges in a single character class:
grep -h '^[A-Za-z0-9].txt'``` echo "a8392" | sed -n '/^[0-9][0-9][0-9][0-9][0-9]$/p' echo "1839a" | sed -n '/^[0-9][0-9][0-9][0-9][0-9]$/p' echo "18a92" | sed -n '/^[0-9][0-9][0-9][0-9][0-9]$/p' echo "The cat is sleeping." | sed -n '/[c-h]at/p' #allows the ranges c through h echo "The cat is sleeping." | sed -n '/[a-ch-m]at/p' #reject any letters between d and g:
-
grep -h '[-AZ]' dirlist*.txt'
anything starting with dash,A or Z
-
-
- Traditional ASCII Character range:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
- POSIX Character Classes
- can be used for both pathname expansion and regular expressions
- When
export LANG=en_US.UTF-8
aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ
-
[[:alnum:]
The alphanumeric characters. In ASCII, equivalent to:[A-Za-z0-9] -
[[:word:]]
The same as [:alnum:], with the addition of the underscore (_) character. -
[[:alpha:]]
The alphabetic characters. In ASCII, equivalent to:[A-Za-z] -
[[:blank:]]
Includes the space and tab characters. -
[[:cntrl:]]
The ASCII control codes. Includes the ASCII characters zerothrough thirty-one and 127. -
[[:digit:]]
The numerals zero through nine. -
[[:graph:]]
The visible characters. In ASCII, it includes characters 33through 126. -
[[:lower:]]
The lowercase letters. -
[[:punct:]]
The punctuation characters. In ASCII, equivalent to:[-!"#$%&'()*+,./:;<=>?@[\]_`{|}~] -
[[:print:]]
The printable characters. All the characters in [:graph:]plus the space character. -
[[:space:]]
The whitespace characters including space, tab, carriagereturn, newline, vertical tab, and form feed. In ASCII,equivalent to:[ \t\r\n\v\f] -
[[:upper:]]
The upper case characters. -
[[:xdigit:]]
Characters used to express hexadecimal numbers. In ASCII,equivalent to:[0-9A-Fa-f]
- 2 flavors of regular expressions:
-
Basic Regular Expressions BRE (used above; recognized by sed)
- following metacharacters are recognized:
-
^ $ . [ ] *
-
*
placing an asterisk after a character specifies that the character must appear zero or more times!echo "ik" | sed -n '/ie*k/p' echo "iek" | sed -n '/ie*k/p' echo "ieek" | sed -n '/ie*k/p' echo "ieeek" | sed -n '/ie*k/p' echo "ieeeek" | sed -n '/ie*k/p' echo "I'm getting a color TV" | sed -n '/colou*r/p' echo "I'm getting a colour TV" | sed -n '/colou*r/p' #when used with dot (any one char) produces match any char 0 or more times: regular<anything here>expression echo "this is a regular pattern expression" | sed -n '/regular.*expression/p'
-
-
( ) { }
treated as metacharacters when escaped with a backlash:\(somethingGoesHere\)
-
BRE special characters you can use:
[[:alpha:]] Matches any alphabetical character, either upper or lower case [[:alnum:]] Matches any alphanumeric character 0–9, A–Z, or a–z [[:blank:]] Matches a space or Tab character [[:digit:]] Matches a numerical digit from 0 through 9 [[:lower:]] Matches any lowercase alphabetical character a–z [[:print:]] Matches any printable character [[:punct:]] Matches a punctuation character [[:space:]] Matches any whitespace character: space, Tab, NL, FF, VT, CR [[:upper:]] Matches any uppercase alphabetical character A–Z echo "abc" | sed -n '/[[:digit:]]/p' echo "abc" | sed -n '/[[:alpha:]]/p' echo "abc123" | sed -n '/[[:digit:]]/p' echo "This is, a test" | sed -n '/[[:punct:]]/p' echo "This is a test" | sed -n '/[[:punct:]]/p'
-
- following metacharacters are recognized:
-
Extended Regular Expressions ERE (recognized by gawk, but not by sed)
- following metacharacters are recognized:
- escaping any characters with
\
causes them to be treated as literals. -
^ $ . [ ] * ( ) { } ? + |
-
?
the preceding character can appear 0 or 1 time.echo "bt" | gawk '/be?t/{print $0}' echo "bet" | gawk '/be?t/{print $0}' echo "beet" | gawk '/be?t/{print $0}' echo "beat" | gawk '/b[e]?at/{print $0}' echo "beeat" | gawk '/b[e]?at/{print $0}' echo "beeeeeat" | gawk '/b[e]*at/{print $0}'
-
*
match an element 0 or more times. -
.
match an element 1 or 1 time. <- not a preceding element but the element in place. -
+
the preceding character can appear 1 or more times.echo "beeet" | gawk '/be+t/{print $0}' echo "beet" | gawk '/be+t/{print $0}' echo "bet" | gawk '/be+t/{print $0}' echo "bt" | gawk '/be+t/{print $0}' echo "bt" | gawk '/b[ae]+t/{print $0}' echo "bat" | gawk '/b[ae]+t/{print $0}'
-
{n,m}
match element if it occurs >=n times but <=m times-
{n}
match exactly n times -
{n,}
match element if it occurs >=n times -
{,m}
match element if it occurs <=m times
# Match ae in any order 2 or 3 times between bb and t; thus bb<a or e 2 or 3 times here>t : echo "bbeeet" | gawk --re-interval '/^bb[ae]{2,3}t/{print $0}' echo "bbaeet" | gawk --re-interval '/^bb[ae]{2,3}t/{print $0}' echo "bbaaet" | gawk --re-interval '/^bb[ae]{2,3}t/{print $0}' echo "bbaaat" | gawk --re-interval '/^bb[ae]{2,3}t/{print $0}' echo "bbaaeet" | gawk --re-interval '/^bb[ae]{2,3}t/{print $0}' echo "bbeeeet" | gawk --re-interval '/^bb[ae]{2,3}t/{print $0}' echo "bbaaaat" | gawk --re-interval '/^bb[ae]{2,3}t/{print $0}'
-
-
|
Pipe which is logical OR. Specifies two or more patterns that the regular expression engine uses in a logical OR formula when examining the data stream. Any spaces within the regular expressions and the pipe symbol are added to the regular expression pattern.echo "The cat is asleep" | gawk '/cat|dog/{print $0}' echo "The dog is asleep" | gawk '/cat|dog/{print $0}' echo "The sheep is asleep" | gawk '/cat|dog/{print $0}' echo "He has a hat." | gawk '/[ch]at|dog/{print $0}'
-
()
Grouping expression patterns. When q regular expression pattern is group, the group is treated like a standard character.echo "Sat" | gawk '/Sat(urday)?/{print $0}' echo "Saturday" | gawk '/Sat(urday)?/{print $0}' echo "cat" | gawk '/(c|b)a(b|t)/{print $0}' echo "cab" | gawk '/(c|b)a(b|t)/{print $0}' echo "bat" | gawk '/(c|b)a(b|t)/{print $0}' echo "bab" | gawk '/(c|b)a(b|t)/{print $0}' echo "tab" | gawk '/(c|b)a(b|t)/{print $0}' echo "tac" | gawk '/(c|b)a(b|t)/{print $0}'
-
- escaping any characters with
- following metacharacters are recognized:
-
arr=($(ls Olivia*)); for i in "${arr[@]}"; do mv $i $(echo $i | sed -r 's/(\{|\})//g'); done```
arr=($(ls ../ | gawk '/[^(advanced)]/{print $0}')); for i in ${arr[@]}; do cp ./../$i ./; done