Convenient Bash terminal skills Collection

The article was forwarded from the professional Laravel developer community, the original link: https://learnku.com/laravel/t...

Bash set

I'm glad you're here! A few years ago, I was engaged in the research of bioinformatics. Surprised by the simple bash commands, they are much faster than my boring scripts. I saved a lot of time by learning command line shortcuts and scripts. In recent years, I have been engaged in cloud computing related work and continue to record those useful commands here. And I'm trying to keep them short and quick. I mainly use Ubuntu, RedHat, Linux Mint and CentOS. If the command doesn't work on your system, I'm sorry.

This blog will focus on the simple commands I got from my work and LPIC exam to parse data and maintain Linux system, but they may come from dear Google and stack overflow.

English and bash are not my mother tongue. Please correct me at any time. Thank you. If you know any other interesting orders, please ask me.

This is the latest version Bash-Oneliner~

[](https://github.com/onceupon/B... bash one line command

[](https://github.com/onceupon/B...

Use Ctrl
Ctrl + n: similar to down key
 Ctrl + p: similar to the up key
 Ctrl + r: reverse search command history (Ctrl + r)
Ctrl + s: the terminal stops output
 Ctrl + q: restore the previous terminal after Ctrl + s
 Ctrl + a: move the cursor to the beginning of the line
 Ctrl + e: move the cursor to the end of the line
 Ctrl + d: if the current terminal command line has input, Ctrl + d or delete the character at the cursor, otherwise the current terminal will exit
 Ctrl + k: delete all characters from the current cursor to the end
 Ctrl + x + backspace: delete all characters from the current cursor to the beginning of the line
 Ctrl + t: exchanges the position of the character under the current cursor and the character before it. Esc + t exchanges the two words before the cursor
 Ctrl + w: cut the word before the cursor, then Ctrl + y paste it
 Ctrl + u: cut the line before the cursor; then Ctrl + y paste it
 Ctrl +: undo previous action
 Ctrl + l: equivalent to clear
 Ctrl + x + Ctrl + e: calls up the EDITOR program of $EDITOR environment variable setting, which is valid for multi line commands
[](https://github.com/onceupon/B...
Esc + u
# Converts text from the beginning to the end of the cursor to uppercase
Esc + l
# Converts text from the beginning to the end of the cursor to lowercase
Esc + c
# Convert letters under the cursor to uppercase
[](https://github.com/onceupon/B... (e.g. e.g. 53)
!53
[](https://github.com/onceupon/B...
!!
[](https://github.com/onceupon/B... : echo 'AAA' - > now run: echo 'bbb')
#Last command: echo 'aaa'
^aaa^bbb

#echo 'bbb'
#bbb

#Note that only the first aaa will be replaced. If you want to replace all aaa, use "&" instead:
^aaa^bbb^:&
#perhaps
!!:gs/aaa/bbb/
[](https://github.com/onceupon/B... (e.g. cat filename)
!cat
# perhaps
!c
#Run cat filename again
[](https://github.com/onceupon/B... wildcard
# '*' is used as a wildcard for file name extensions.
/b?n/?at      #/bin/cat

# '?' is used as a single character "wildcard" for file name extensions.
/etc/pa*wd    #/etc/passwd

# '[]' is used to match characters in the range.
ls -l [a-z]*   #Lists files with letters in all file names.

# '{}' can be used to match file names of multiple patterns 
ls {*.sh,*.py}   #List all. sh and. py files
[](https://github.com/onceupon/B...
$0: the name of the shell or shell script.
$1, $2, $3,...: position parameter.
$#: number of location parameters.
$?: latest pipeline exit status.
$-: the current option set for the shell.
$$: pid of the current shell (not subshell).
$!: PID of the latest background command.

$desktop? Session current display manager
 $EDITOR preferred text EDITOR.
$LANG current language.
$PATH searches the directory list of executable files (i.e. programs ready to run)
$PWD current directory
 $shell current shell
 $USER current USER name
 $HOSTNAME current HOSTNAME

screen

[back to top]

[](https://github.com/onceupon/B...
grep = grep -G # Basic Regular Expression (BRE)
fgrep = grep -F # fixed text, ignoring meta-charachetrs
egrep = grep -E # Extended Regular Expression (ERE)
pgrep = grep -P # Perl Compatible Regular Expressions (PCRE)
rgrep = grep -r # recursive
[](https://github.com/onceupon/B...
grep -c "^$"
[](https://github.com/onceupon/B...
grep -o '[0-9]*'
#or
grep -oP '\d'
[](https://github.com/onceupon/B... (example 3)
grep '[0-9]\{3\}'
# or
grep -E '[0-9]{3}'
# or
grep -P '\d{3}'
[](https://github.com/onceupon/B...
grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
# or
grep -Po '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
[](https://github.com/onceupon/B... (e.g. "target")
grep -w 'target'

#or using RE
grep '\btarget\b'
[](https://github.com/onceupon/B... (e.g. "bbo")
# return also 3 lines after match
grep -A 3 'bbo'

# return also 3 lines before match
grep -B 3 'bbo'

# return also 3 lines before and after match
grep -C 3 'bbo'
[](https://github.com/onceupon/B... (e.g. "S")
grep -o 'S.*'
[](https://github.com/onceupon/B... (for example, w1, w2)
grep -o -P '(?<=w1).*(?=w2)'
[](https://github.com/onceupon/B... (e.g. bbo)
grep -v bbo filename
[](https://github.com/onceupon/B... (e.g.)
grep -v '^#' file.txt
[](https://github.com/onceupon/B... (for example, bbo="some strings")
grep "$boo" filename
#remember to quote the variable!
[](https://github.com/onceupon/B... (e.g. bbo)
grep -m 1 bbo filename
[](https://github.com/onceupon/B... (eg bb)
grep -c bbo filename
[](https://github.com/onceupon/B... (for example, three times for a row)
grep -o bbo filename |wc -l
[](https://github.com/onceupon/B... (e.g. BBO / BBO / bbbo)
grep -i "bbo" filename
[](https://github.com/onceupon/B... (e.g. bbo)
grep --color bbo filename
[](https://github.com/onceupon/B... (e.g. bbo)
grep -R bbo /path/to/directory
# or
grep -r bbo /path/to/directory
[](https://github.com/onceupon/B... , do not output file names (for example, bbo)
grep -rh bbo /path/to/directory
[](https://github.com/onceupon/B... , only matching file names (such as bbo) are output
grep -rl bbo /path/to/directory
[](https://github.com/onceupon/B... (e.g. A or B or C or D)
grep 'A\|B\|C\|D'
[](https://github.com/onceupon/B... (e.g. A and B)
grep 'A.*B'
[](https://github.com/onceupon/B... (e.g. ACB or AEB)
grep 'A.B'
[](https://github.com/onceupon/B... (for example, color or colour)
grep 'colou?r'
[](https://github.com/onceupon/B... Find the contents of file A in file B
grep -f fileA fileB
[](https://github.com/onceupon/B...
grep $'\t'
[](https://github.com/onceupon/B...
$echo "$long_str"|grep -q "$short_str"
if [ $? -eq 0 ]; then echo 'found'; fi
#grep -q will output 0 if match found
#remember to add space between []!
[](https://github.com/onceupon/B...
grep -oP '\(\K[^\)]+'
[](https://github.com/onceupon/B... (e.g. AAEL0000-RA)
grep -o -w "\w\{10\}\-R\w\{1\}"
# \W text character [0-9a-zA-Z] \ w non text character
[](https://github.com/onceupon/B... (e.g. bbo)
grep -d skip 'bbo' /path/to/files/*

stream editor

[back to top]

[](https://github.com/onceupon/B...
sed 1d filename
[](https://github.com/onceupon/B... (delete lines 1-100)
sed 1,100d filename
[](https://github.com/onceupon/B...: bbo)
sed "/bbo/d" filename
- case insensitive:
sed "/bbo/Id" filename
[](https://github.com/onceupon/B... (for example, the 5th character is not equal to 2)
sed -E '/^.{5}[^2]/d'
#aaaa2aaa (you can stay)
#aaaa1aaa (delete!)
[](https://github.com/onceupon/B... (edit and save)
sed -i "/bbo/d" filename
[](https://github.com/onceupon/B... (for example, $i), use double quotes ""
# e.g. add >$i to the first line (to make a bioinformatics FASTA file)
sed "1i >$i"
# notice the double quotes! in other examples, you can use a single quote, but here, no way!
# '1i' means insert to first line
[](https://github.com/onceupon/B...
# Use the backslash $character and double quotes to mark variables
sed -e "\$s/\$/\n+--$3-----+/"
[](https://github.com/onceupon/B...
sed '/^\s*$/d'

# or

sed '/^$/d'
[](https://github.com/onceupon/B...
sed '$d'
[](https://github.com/onceupon/B...
sed -i '$ s/.$//' filename
[](https://github.com/onceupon/B... (for example, []
sed -i '1s/^/[/' file
[](https://github.com/onceupon/B... (for example, add "something" to lines 1 and 3)
sed -e '1isomething -e '3isomething'
[](https://github.com/onceupon/B... (for example, [])
sed '$s/$/]/' filename
[](https://github.com/onceupon/B...
sed '$a\'
[](https://github.com/onceupon/B... (e.g. bbo)
sed -e 's/^/bbo/' file
[](https://github.com/onceupon/B... (e.g.,})
sed -e 's/$/\}\]/' filename
[](https://github.com/onceupon/B... N (e.g. add n every four characters t)
sed 's/.\{4\}/&\n/g'
[](https://github.com/onceupon/B... Connect / combine / merge files. For example (use "," to split)
sed -s '$a,' *.json > all.json
[](https://github.com/onceupon/B... B to replace A)
sed 's/A/B/g' filename
[](https://github.com/onceupon/B... aaa = replace the first line with aaa=/my/new/path)
sed "s/aaa=.*/aaa=\/my\/new\/path/g"
[](https://github.com/onceupon/B... bbo)
sed -n '/^@S/p'
[](https://github.com/onceupon/B...
sed '/bbo/d' filename
[](https://github.com/onceupon/B...
sed -n 500,5000p filename
[](https://github.com/onceupon/B... n lines printed once
sed -n '0~3p' filename

# catch 0: start; 3: step
[](https://github.com/onceupon/B...
sed -n '1~2p'
[](https://github.com/onceupon/B... , including the first line
sed -n '1p;0~3p'
[](https://github.com/onceupon/B...
sed -e 's/^[ \t]*//'
# Notice a whitespace before '\t'!!
[](https://github.com/onceupon/B...
sed 's/ *//'

# Notice the space before '*'!!
[](https://github.com/onceupon/B...
sed 's/,$//g'
[](https://github.com/onceupon/B...
sed "s/$/\t$i/"
# $i is the value you want to add

# Add the filename to the last column of the file
for i in $(ls);do sed -i "s/$/\t$i/" $i;done
[](https://github.com/onceupon/B...
for i in T000086_1.02.n T000086_1.02.p;do sed "s/$/\t${i/*./}/" $i;done >T000086_1.02.np
[](https://github.com/onceupon/B...
sed ':a;N;$!ba;s/\n//g'
[](https://github.com/onceupon/B... : Line 123)
sed -n -e '123p'
[](https://github.com/onceupon/B... : lines 10 to 33)
sed -n '10,33p' <filename
[](https://github.com/onceupon/B...
sed 's=/=\\/=g'
[](https://github.com/onceupon/B... (e.g. A-1-e, A-2-e or A-3-e...)
sed 's/A-.*-e//g' filename
[](https://github.com/onceupon/B...
sed '$ s/.$//'
[](https://github.com/onceupon/B...: AAAAAA—> AAA#AAA)
sed -r -e 's/^.{3}/&#/' file

Awk

[back to top]

[](https://github.com/onceupon/B... tab is the separator
awk -F $'\t'
[](https://github.com/onceupon/B... tab is the output field separator
awk -v OFS='\t'
[](https://github.com/onceupon/B...
a=bbo;b=obb;
awk -v a="$a" -v b="$b" "$1==a && $10=b" filename
[](https://github.com/onceupon/B...
awk '{print NR,length($0);}' filename
[](https://github.com/onceupon/B...
awk '{print NF}'
[](https://github.com/onceupon/B...
awk '{print $2, $1}'
[](https://github.com/onceupon/B... (e.g. check first column)
awk '$1~/,/ {print}'
[](https://github.com/onceupon/B... , and cycle output
awk '{split($2, a,",");for (i in a) print $1"\t"a[i]}' filename
[](https://github.com/onceupon/B... , print data (for example, stop outputting data after bbo appears 7 times)
awk -v N=7 '{print}/bbo/&& --N<=0 {exit}'
[](https://github.com/onceupon/B...
ls|xargs -n1 -I file awk '{s=$0};END{print FILENAME,s}' file
[](https://github.com/onceupon/B... (for example, add "chr" before the third column)
awk 'BEGIN{OFS="\t"}$3="chr"$3'
[](https://github.com/onceupon/B... (for example, delete the row with bbo)
awk '!/bbo/' file
[](https://github.com/onceupon/B...
awk 'NF{NF-=1};1' file
[](https://github.com/onceupon/B... Usage of NR and FNR
# For example, the following 2 files:
# fileA:
# a
# b
# c
# fileB:
# d
# e
awk 'print FILENAME, NR,FNR,$0}' fileA fileB
# fileA    1    1    a
# fileA    2    2    b
# fileA    3    3    c
# fileB    4    1    d
# fileB    5    2    e
Logic and
# For example, the following two files:
# File A:
# 1    0
# 2    1
# 3    1
# 4    0
# File B:
# 1    0
# 2    1
# 3    0
# 4    1

awk -v OFS='\t' 'NR=FNR{a[$1]=$2;next} NF {print $1,((a[$1]=$2)? $2:"0")}' fileA file B
# 1    0
# 2    1
# 3    0
# 4    0
[](https://github.com/onceupon/B...
awk '{while (match($0, /[0-9]+\[0-9]+/)){
    \printf "%s%.2f", substr($0,0,RSTART-1),substr($0,RSTART,RLENGTH)
    \$0=substr($0, RSTART+RLENGTH)
    \}
    \print
    \}'
[](https://github.com/onceupon/B...
awk '{printf("%s\t%s\n",NR,$0)}'
[](https://github.com/onceupon/B...
#For example, separate the following:
# David    cat,dog
# into
# David    cat
# David    dog

awk '{split($2,a,",");for(i in a)print $1"\t"a[i]}' file

# For details, please click here: http://stackoverflow.com/questions/33408762/bash-turning-single-comma-separated-column-into-multi-line-string
[](https://github.com/onceupon/B...
awk '{s+=$1}END{print s/NR}'
[](https://github.com/onceupon/B... Linux)
awk '$1 ~ /^Linux/'
[](https://github.com/onceupon/B... (for example, 1 40 35 12 23 -- > 1 12 23 35 40)
awk ' {split( $0, a, "\t" ); asort( a ); for( i = 1; i <= length(a); i++ ) printf( "%s\t", a[i] ); printf( "\n" ); }'
[](https://github.com/onceupon/B... , which equals column4 minus the last column5)
awk '{$6 = $4 - prev5; prev5 = $5; print;}'

Xargs

[back to top]

[](https://github.com/onceupon/B... : space)
xargs -d\t
[](https://github.com/onceupon/B...
echo 1 2 3 4 5 6| xargs -n 3
# 1 2 3
# 4 5 6
[](https://github.com/onceupon/B...
echo a b c |xargs -p -n 3
[](https://github.com/onceupon/B...
xargs -t abcd
# bin/echo abcd
# abcd
[](https://github.com/onceupon/B...
find . -name "*.html"|xargs rm

# when using a backtick
rm `find . -name "*.html"`
[](https://github.com/onceupon/B... 「hello 2001」)
find . -name "*.c" -print0|xargs -0 rm -rf
[](https://github.com/onceupon/B...
xargs --show-limits
[](https://github.com/onceupon/B...
find . -name "*.bak" -print 0|xargs -0 -I {} mv {} ~/old

# or
find . -name "*.bak" -print 0|xargs -0 -I file mv file ~/old
[](https://github.com/onceupon/B...
ls |head -100|xargs -I {} mv {} d1
[](https://github.com/onceupon/B...
time echo {1..5} |xargs -n 1 -P 5 sleep

# a lot faster than:
time echo {1..5} |xargs -n1 sleep
[](https://github.com/onceupon/B... Copy A to B
find /dir/to/A -type f -name "*.py" -print 0| xargs -0 -r -I file cp -v -p file --target-directory=/path/to/B

# v: verbose|
# p: keep detail (e.g. owner)
sed correlation
ls |xargs -n1 -I file sed -i '/^Pos/d' filename
[](https://github.com/onceupon/B...
ls |sed 's/.txt//g'|xargs -n1 -I file sed -i -e '1 i\>file\' file.txt
[](https://github.com/onceupon/B...
ls |xargs -n1 wc -l
[](https://github.com/onceupon/B...
ls -l| xargs
[](https://github.com/onceupon/B...
echo mso{1..8}|xargs -n1 bash -c 'echo -n "$1:"; ls -la "$1"| grep -w 74 |wc -l' --
# End of "--" signal option and further processing of option
[](https://github.com/onceupon/B... , also count the total number of rows.
ls|xargs wc -l
[](https://github.com/onceupon/B... And grep command
cat grep_list |xargs -I{} grep {} filename
[](https://github.com/onceupon/B... And sed command (replace all old ip addresses with new ones under etc file)
grep -rl '192.168.1.111' /etc | xargs sed -i 's/192.168.1.111/192.168.2.111/g'

Find (query)

[back to top]

[](https://github.com/onceupon/B... List all subdirectories / files in the current directory
find .
[](https://github.com/onceupon/B... List all files in the current directory
find . -type f
[](https://github.com/onceupon/B... List all directories under the current file
find . -type d
[](https://github.com/onceupon/B... Edit all files in the current directory (for example, replace "www" with "ww")
find . -name '*.php' -exec sed -i 's/www/w/g' {} \;

# If there is no subdirectory
replace "www" "w" -- *
# a space before *
[](https://github.com/onceupon/B... Query the file and print the file name (for example, "mso")
find mso*/ -name M* -printf "%f\n"
[](https://github.com/onceupon/B... Find and delete files smaller than (for example, 74 bytes)
find . -name "*.mso" -size -74c -delete

# M for MB, etc

Condition and loop

[back to top]

[](https://github.com/onceupon/B... Sentence
# Use if and else to make conditional judgment 
if [[ "$c" == "read" ]]; then outputdir="seq"; else outputdir="write" ; fi

# Determine whether myfile contains the string "test"
if grep -q hello myfile; then ...

# Determine whether mydir is a directory, modify the contents of mydir and perform other operations:
if cd mydir; then
  echo 'some content' >myfile
else
  echo >&2 "Fatal error. This script requires mydir."
fi

# Determine if the variable is empty
if [ ! -s "myvariable" ]
#Returns the length of the specified object. If string, returns 0

# Judge whether the file exists
if [ -e 'filename' ]
then
  echo -e "file exists!"
fi

# Determine whether the myfile file exists or whether the myfile connection exists 
if [ -e myfile ] || [ -L myfile ]
then
  echo -e "file exists!"
fi

# Judge whether the variable x is greater than or equal to 5
if [ "$x" -ge 5 ]; then ...

#  In bash/ksh/zsh, judge whether the variable x is greater than or equal to 5
if ((x >= 5)); then ...

# Use (()) (double bracket) to perform mathematical operation and assign the calculation result of u+2 to j 
if ((j==u+2))

# Numerical comparison using [[]] (double brackets) automatically converts special symbols to comparison symbols in [[]] (for example = to = =)  
if [[ $age -gt 21 ]]

More if commands

[](https://github.com/onceupon/B...
For syntax
for i in $(ls); do echo file $i;done
#perhaps
for i in *; do echo file $i; done

# Press any key to continue traversal
for i in $(cat tpc_stats_0925.log |grep failed|grep -o '\query\w\{1,2\}');do cat ${i}.log; read -rsp $'Press any key to continue...\n' -n1 key;done

# When a key is pressed, the document will be printed line by line
oifs="$IFS"; IFS=$'\n'; for line in $(cat myfile); do ...; done
while read -r line; do ...; done <myfile

# If only one word a line, simply
for line in $(cat myfile); do echo $line; read -n1; done

#Traversing an array
for i in "${arrayName[@]}"; do echo $i;done
[](https://github.com/onceupon/B... Sentence,
# Column subtraction of a file (for example, a 3 columns file)
while read a b c; do echo $(($c-$b));done < <(head filename)
#There is a space between two "< s" 

# Total column subtraction
i=0; while read a b c; do ((i+=$c-$b)); echo $i; done < <(head filename)

# Continue checking for running processes (such as perl) and start another new process (such as python) as soon as it starts (it's best to use the wait command! Ctrl + F " wait")
while [[ $(pidof perl) ]];do echo f;sleep 10;done && python timetorunpython.py
[](https://github.com/onceupon/B... (case in bash)
read type;
case $type in
  '0')
    echo 'how'
    ;;
  '1')
    echo 'are'
    ;;
  '2')
    echo 'you'
    ;;
esac

[](https://github.com/onceupon/B...

variable

[back to top]

[](https://github.com/onceupon/B...
# foo=bar
 echo "'$foo'"
#'bar'
# Single quotes / double quotes around single quotes make the inner single quotes expand variables
[](https://github.com/onceupon/B...
var="some string"
echo ${#var}
# 11
[](https://github.com/onceupon/B...
var=string
echo "${var:0:1}"
#s

# or
echo ${var%%"${var#?}"}
[](https://github.com/onceupon/B... Delete n bytes in variable from first or last bit
var="some string"
echo ${var:2}
#me string
[](https://github.com/onceupon/B... (for example. Delete 0 in the first location)
var="0050"
echo ${var[@]#0}
#050
[](https://github.com/onceupon/B... (for example, replace the character "a" with the character ",")
{var/a/,}
# Using grep 
 test="god the father"
 grep ${test// /\\\|} file.txt
 # turning the space into 'or' (\|) in grep
[](https://github.com/onceupon/B... (parameter extension)
var=HelloWorld
echo ${var,,}
helloworld
[](https://github.com/onceupon/B... Expand and then execute variable/argument
cmd="bar=foo"
eval "$cmd"
echo "$bar" # foo

Mathematics

[back to top]

[](https://github.com/onceupon/B... (operators: +, -, *, /,%, etc.)
#Summary: a + + first calculates a, then adds 1 to the value of a; + + A, on the contrary, first adds one, and then participates in the operation. Same as a --, and -- A
echo $(( 10 + 5 ))  #15
x=1
echo $(( x++ )) #1. Notice that it's still 1, because it's incremented
echo $(( x++ )) #2
echo $(( ++x )) #4. Note that it's not 3, because it's a pre increase
echo $(( x-- )) #4
echo $(( x-- )) #3
echo $(( --x )) #1
x=2
y=3
echo $(( x ** y )) #8
[](https://github.com/onceupon/B... (for example: 50)
factor 50
[](https://github.com/onceupon/B... . (for example: seq 10)
seq 10|paste -sd+|bc
[](https://github.com/onceupon/B... (each line in the file contains only one number)
awk '{s+=$1} END {print s}' filename
[](https://github.com/onceupon/B...
cat file| awk -F '\t' 'BEGIN {SUM=0}{SUM+=$3-$2}END{print SUM}'
[](https://github.com/onceupon/B...
expr 10+20 #30
expr 10\*20 #600
expr 30 \> 20 #1 (true)
[](https://github.com/onceupon/B...
# Decimal / significant
echo "scale=2;2/3" | bc
#.66

# Exponential operator
echo "10^2" | bc
#100

# Using variables
echo "var=5;--var"| bc
#4

Time

[back to top]

[](https://github.com/onceupon/B...
time echo hi
[](https://github.com/onceupon/B... (e.g. 10 seconds)
sleep 10
[](https://github.com/onceupon/B... (e.g. 10 seconds)
TMOUT=10
# Once you set this variable, the logout timer starts!
[](https://github.com/onceupon/B...
# Only run 'sleep 10' for one second.
timeout 1 sleep 10
[](https://github.com/onceupon/B... (for example, one minute from now)
at now + 1min  # The time unit can be minutes, hours, days, or weeks
⚠️: Command will use /bin/sh
at> echo hihigithub >~/itworks
at> <EOT>   # Press Ctrl + D to exit
job 1 at Wed Apr 18 11:16:00 2018

[](https://github.com/onceupon/B...

Download

[back to top]

[](https://github.com/onceupon/B... (what you are reading)
curl https://raw.githubusercontent.com/onceupon/Bash-Oneliner/master/README.md | pandoc -f markdown -t man | man -l -

# Or w3m (a text-based browser and pager)
curl https://raw.githubusercontent.com/onceupon/Bash-Oneliner/master/README.md | pandoc | w3m -T text/html

# Or use emacs (in the emac text editor)
emacs --eval '(org-mode)' --insert <(curl https://raw.githubusercontent.com/onceupon/Bash-Oneliner/master/README.md | pandoc -t org)

# Or use emacs (first press Ctrl+x on the terminal, then press Ctrl+c to exit)
emacs -nw --eval '(org-mode)' --insert <(curl https://raw.githubusercontent.com/onceupon/Bash-Oneliner/master/README.md | pandoc -t org)
[](https://github.com/onceupon/B...
wget -r -l1 -H -t1 -nd -N -np -A mp3 -e robots=off http://example.com

# -r: recursive and download all links on page
# -l1: only one level link
# -H: span host, visit other hosts
# -t1: numbers of retries
# -nd: don't make new directories, download to here
# -N: turn on timestamp
# -nd: no parent no parent
# -A: type (separate by,) type (separated by, bean)
# -e robots=off: ignore the robots.txt file which stop wget from crashing the site, sorry example.com
[](https://github.com/onceupon/B... (https://transfer.sh/)
#  Upload file (for example: filename.txt):
curl --upload-file ./filename.txt https://transfer.sh/filename.txt
# The above command will return a url, for example: https://transfer.sh/tG8rM/filename.txt

# Next you can download it in the following ways:
curl https://transfer.sh/tG8rM/filename.txt -o filename.txt
[](https://github.com/onceupon/B... (if necessary)
data=file.txt
url=http://www.example.com/$data
if [ ! -s $data ];then
    echo "downloading test data..."
    wget $url
fi
[](https://github.com/onceupon/B... Command to get the filename (when the filename is long)
wget -O filename "http://example.com"
[](https://github.com/onceupon/B... Save files to folder
wget -P /path/to/directory "http://example.com"
[](https://github.com/onceupon/B... Until the final destination:
curl -L google.com

[](https://github.com/onceupon/B...

random

[back to top]

[](https://github.com/onceupon/B... (for example, generate 5 passwords of length 13)
sudo apt install pwgen
pwgen 13 5
#sahcahS9dah4a xieXaiJaey7xa UuMeo0ma7eic9 Ahpah9see3zai acerae7Huigh7
[](https://github.com/onceupon/B...
shuf -n 100 filename
[](https://github.com/onceupon/B...
for i in a b c d e; do echo $i; done| shuf
[](https://github.com/onceupon/B... (for example, randomly select 15 numbers from 0-100)
shuf -i 0-100 -n 15
[](https://github.com/onceupon/B...
echo $RANDOM
[](https://github.com/onceupon/B...
echo $((RANDOM % 10))
[](https://github.com/onceupon/B...
echo $(((RANDOM %10)+1))

[](https://github.com/onceupon/B...

Xwindow

[back to top]

X11 GUI application! If you're tired of a plain text environment, here are some GUI tools for you.

[](https://github.com/onceupon/B... X11 forward to use the graphics application on the server.
ssh -X user_name@ip_address

# Or set it through xhost
# --> Install the following for Centos:
# xorg-x11-xauth
# xorg-x11-fonts-*
# xorg-x11-utils
[](https://github.com/onceupon/B... X window tools
xclock
xeyes
xcowsay
[](https://github.com/onceupon/B... Open picture / image in ssh server
1\. ssh -X user_name@ip_address
2. apt-get install eog
3. eog picture.png
[](https://github.com/onceupon/B... Watch video on server
1\. ssh -X user_name@ip_address
2. sudo apt install mpv
3. mpv myvideo.mp4
[](https://github.com/onceupon/B... gedit (GUI edit)
1\. ssh -X user_name@ip_address
2. apt-get install gedit
3. gedit filename.txt
[](https://github.com/onceupon/B... Open PDF on ssh server
1\. ssh -X user_name@ip_address
2. apt-get install evince
3. evince filename.pdf
[](https://github.com/onceupon/B... Using Google browser on ssh server
1\. ssh -X user_name@ip_address
2. apt-get install libxss1 libappindicator1 libindicator7
3. wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
4. sudo apt-get install -f
5. dpkg -i google-chrome*.deb
6. google-chrome

[](https://github.com/onceupon/B...

system

[back to top]

[](https://github.com/onceupon/B...
journalctl -u <service_name> -f
[](https://github.com/onceupon/B...
# The zombie process is dead, so you can't kill it. You can eliminate the process by killing its parent.
# First, find the PID of the zombie process
ps aux| grep 'Z'
# Next we find the PID of the Zombie's parent process
pstree -p -s <zombie_PID>
# Then you can kill its parent process, and you will find that the zombie process is gone.
sudo kill 9 <parent_PID>
[](https://github.com/onceupon/B...
free -c 10 -mhs 1
# Print 10 times every 1 second
[](https://github.com/onceupon/B....
#Refresh once per second
iostat -x -t 1
[](https://github.com/onceupon/B... (for example, enp175s0f0)
iftop -i enp175s0f0
[](https://github.com/onceupon/B...
uptime
[](https://github.com/onceupon/B...
if [ "$EUID" -ne 0 ]; then
        echo "Please run this as root"
        exit 1
fi
[](https://github.com/onceupon/B...: bonnie)
chsh -s /bin/sh bonnie
# /etc/shells: valid login shells
[](https://github.com/onceupon/B... (e.g. change root to newroot)
chroot /home/newroot /bin/bash

# To exit chroot
exit
[](https://github.com/onceupon/B... File status (size; access, modification, change time, etc.) for (for example, filename.txt)
stat filename.txt
[](https://github.com/onceupon/B...
ps aux
[](https://github.com/onceupon/B...
pstree
[](https://github.com/onceupon/B...
cat /proc/sys/kernel/pid_max
[](https://github.com/onceupon/B...
dmesg
[](https://github.com/onceupon/B...
$ip add show

# or
ifconfig
[](https://github.com/onceupon/B...
Print previous and current SysV run levels
runlevel

#perhaps
who -r
[](https://github.com/onceupon/B... SysV run level (e.g. change to 5)
init 5
#perhaps
telinit 5
[](https://github.com/onceupon/B...
chkconfig --list
# Equivalent to update-rc.d of chkconfig in ubntu
[](https://github.com/onceupon/B...
cat /etc/*-release
[](https://github.com/onceupon/B... Programmer's Manual: a description of the hierarchy of file systems
man hier
[](https://github.com/onceupon/B...
#  Check the status of cron
systemctl status cron.service

#  Stop a cron service
systemctl stop cron.service
[](https://github.com/onceupon/B...
jobs -l
[](https://github.com/onceupon/B... (for example. / test.sh)
# Better values are adjusted between - 20 (most favorable) and 19
# The better the application, the lower the priority
# Default: 10, default priority: 80

nice -10 ./test.sh
[](https://github.com/onceupon/B...
export PATH=$PATH:~/path/you/want
[](https://github.com/onceupon/B...
Make files executable
chmod +x filename
# Now you can execute. / filename
[](https://github.com/onceupon/B...
uname -a

# Check the system hardware platform (x86-64)
uname -i
[](https://github.com/onceupon/B...
links www.google.com
[](https://github.com/onceupon/B... , set password
useradd username
passwd username
[](https://github.com/onceupon/B... bash variables (for example, show the entire path)
1\. joe ~/.bash_profile
2. export PS1='\u@\h:\w\$'
# $PS1 is a variable that defines the appearance and style of the command prompt
3\. source ~/.bash_profile
[](https://github.com/onceupon/B... (for example, alias)
1\. joe ~/.bash_profile
2. alias pd="pwd" //no more need to type that 'w'!
3\. source ~/.bash_profile
[](https://github.com/onceupon/B...
alias -p
[](https://github.com/onceupon/B... (for example, after the alias ls='ls --color=auto ')
unalias ls
[](https://github.com/onceupon/B... shell options
# Print all shell options
shopt

# Cancel (or stop) alias
shopt -u expand_aliases

# Set (or start) alias
shopt -s expand_aliases
[](https://github.com/onceupon/B... (e.g. PATH)
echo $PATH
#List of directories separated by colons
[](https://github.com/onceupon/B...
env
[](https://github.com/onceupon/B...
Unset environment variable (e.g. unset variable 'MYVAR')
unset MYVAR
[](https://github.com/onceupon/B...
lsblk
[](https://github.com/onceupon/B...
partprobe
[](https://github.com/onceupon/B... bin
ln -s /path/to/program /home/usr/bin
# Must be the absolute path of the program
[](https://github.com/onceupon/B...
hexdump -C filename.class
[](https://github.com/onceupon/B...
rsh node_name
[](https://github.com/onceupon/B... (network port occupied)
netstat -tulpn
[](https://github.com/onceupon/B...
readlink filename
[](https://github.com/onceupon/B... (e.g. python)
type python
# python is / usr/bin/python
# Here are 5 different types, which are checked with the 'type -f' flag
# 1\. alias    (shell alias)
# 2\. function (shell function, also prints function body)
# 3\. builtin  (shell builtin)
# 4\. file     (disk file)
# 5\. keyword  (shell reserved word)

# You can also use 'which'`
which python
# /usr/bin/python
[](https://github.com/onceupon/B...
declare -F
[](https://github.com/onceupon/B...
du -hs .

# or
du -sb
[](https://github.com/onceupon/B...
Copy permission directory
cp -rp /path/to/directory
[](https://github.com/onceupon/B...
pushd .

# then pop
popd

#Or use dirs to display a list of the current directories.
dirs -l
[](https://github.com/onceupon/B...
df -h

# perhaps
du -h

#perhaps
du -sk /var/log/* |sort -rn |head -10
[](https://github.com/onceupon/B...
runlevel
[](https://github.com/onceupon/B...
init 3

#perhaps
telinit 3
[](https://github.com/onceupon/B...
1\. edit /etc/init/rc-sysinit.conf
2. env DEFAULT_RUNLEVEL=2
[](https://github.com/onceupon/B... root users
su
[](https://github.com/onceupon/B...
su somebody
[](https://github.com/onceupon/B...
repquota -auvs
[](https://github.com/onceupon/B...
getent database_name

# (e.g.'passwd 'database)
getent passwd
# List all user accounts (all local accounts and LDAP)

# (e.g. get user group list)
getent group
# Save in 'group' database
[](https://github.com/onceupon/B...
chown user_name filename
chown -R user_name /path/to/directory/
# Change user group name
Mount and unmount
# For example, mount / dev/sdb to / home/test
mount /dev/sdb /home/test

# For example, unmount / home/test
umount /home/test
[](https://github.com/onceupon/B...
mount
# perhaps
df
[](https://github.com/onceupon/B...
cat /etc/passwd
[](https://github.com/onceupon/B...
getent passwd| awk '{FS="[:]"; print $1}'
[](https://github.com/onceupon/B...
compgen -u
[](https://github.com/onceupon/B...
compgen -g
[](https://github.com/onceupon/B...
group username
[](https://github.com/onceupon/B... uid, gid, user group
id username
[](https://github.com/onceupon/B... root
if [ $(id -u) -ne 0 ];then
    echo "You are not root!"
    exit;
fi
# 'id -u' output 0 if it's not root
[](https://github.com/onceupon/B... CPU information
more /proc/cpuinfo

# perhaps
lscpu
[](https://github.com/onceupon/B... (e.g. disk soft size limit: 120586240; hard limit: 125829120)
setquota username 120586240 125829120 0 0 /home
[](https://github.com/onceupon/B...
quota -v username
[](https://github.com/onceupon/B...
ldconfig -p
[](https://github.com/onceupon/B... (for example, for 'ls')
ldd /bin/ls
[](https://github.com/onceupon/B...
lastlog
[](https://github.com/onceupon/B...
Edit paths for all users
joe /etc/environment
#Edit this file
[](https://github.com/onceupon/B...
ulimit -u
[](https://github.com/onceupon/B... TCP connection
nmap -sT -O localhost
#notice that some companies might not like you using nmap
[](https://github.com/onceupon/B...
nproc --all
[](https://github.com/onceupon/B...
1\. top
2. press '1'
[](https://github.com/onceupon/B... PID
jobs -l
[](https://github.com/onceupon/B...
service --status-all
[](https://github.com/onceupon/B...
shutdown -r +5 "Server will restart in 5 minutes. Please save your work."
[](https://github.com/onceupon/B...
shutdown -c
[](https://github.com/onceupon/B...
wall -n hihi
[](https://github.com/onceupon/B...
pkill -U user_name
[](https://github.com/onceupon/B...
kill -9 $(ps aux | grep 'program_name' | awk '{print $2}')
[](https://github.com/onceupon/B...
Setting gedit preferences on the server
# You may need to install the following software:

apt-get install libglib2.0-bin;
# perhaps
yum install dconf dconf-editor;
yum install dbus dbus-x11;

# Checklist
gsettings list-recursively

# Modify some settings
gsettings set org.gnome.gedit.preferences.editor highlight-current-line true
gsettings set org.gnome.gedit.preferences.editor scheme 'cobalt'
gsettings set org.gnome.gedit.preferences.editor use-default-font false
gsettings set org.gnome.gedit.preferences.editor editor-font 'Cantarell Regular 12'
[](https://github.com/onceupon/B... (for example, never add the user name "nice" to the group "docker", so that this user can run docker without sudo)
sudo gpasswd -a nice docker
[](https://github.com/onceupon/B... python package
1\. pip installation -- user package
 2. You may need to export ~ /. local/bin / to PATH: export PATH=$PATH:~/.local/bin/
[](https://github.com/onceupon/B... Linux kernel (when / boot is almost full...)
1\. uname -a  #Check the current kernel, which cannot be removed
2\. sudo apt-get purge linux-image-X.X.X-X-generic  #Replace the old version
[](https://github.com/onceupon/B...
sudo hostname your-new-name

#If it doesn't work, you can:
hostnamectl set-hostname your-new-hostname
# Then check:
hostnamectl
# Or check / etc/hostname

# If you don't work..., edit:
/etc/sysconfig/network
/etc/sysconfig/network-scripts/ifcfg-ensxxx
#Add hostname = your new hostname
[](https://github.com/onceupon/B...
apt list --installed

# Or at Red Hat:
yum list installed
[](https://github.com/onceupon/B...
lsof /mnt/dir
[](https://github.com/onceupon/B...
killall pulseaudio
# Then press Alt-F2 and enter pulseaudio
When the sound doesn't work
killall pulseaudio
[](https://github.com/onceupon/B... SCSI device information list
lsscsi
[](https://github.com/onceupon/B... DNS server tutorial

http://onceuponmine.blogspot....

[](https://github.com/onceupon/B...

http://onceuponmine.blogspot....

[](https://github.com/onceupon/B...

http://onceuponmine.blogspot....

[](https://github.com/onceupon/B... telnet to test the open port and whether it can connect to the server, such as the server (192.168.2.106) port (53)
telnet 192.168.2.106 53
[](https://github.com/onceupon/B... (mtu) (modified to 9000, for example)
ifconfig eth0 mtu 9000
[](https://github.com/onceupon/B... pid (for example, python)
pidof python

# perhaps
ps aux|grep python
[](https://github.com/onceupon/B...
# Start ntp:
ntpd

# Check ntp:
ntpq -p
[](https://github.com/onceupon/B...
sudo apt-get autoremove
sudo apt-get clean
sudo rm -rf ~/.cache/thumbnails/*

# Remove old kernel
sudo dpkg --list 'linux-image*'
sudo apt-get remove linux-image-OLDER_VERSION
[](https://github.com/onceupon/B... (root partition is LVM logical volume)
pvscan
lvextend -L +130G /dev/rhel/root -r
# Add - r will increase to the file system after resizing the volume
[](https://github.com/onceupon/B...
Create a UEFI bootable USB drive (such as; / dev/sdc1)
sudo dd if=~/path/to/isofile.iso of=/dev/sdc1 oflag=direct bs=1048576
[](https://github.com/onceupon/B...
sudo dpkg -l | grep <package_name>
sudo dpkg --purge <package_name>
[](https://github.com/onceupon/B...
ssh -f -L 9000:targetservername:8088 root@192.168.14.72 -N
#-f: running in the background; - L: listening; - N: doing nothing
# Your computer's 9000 port is now connected to port 8088 with the target server name of 192.168.14.72
# So now you can open the browser and type localhost:9000 to check port 8088 of your target computer
[](https://github.com/onceupon/B... (for example: sublime_text)
#pidof get process id
pidof sublime_text

#pgrep, you don't have to type the entire program name
pgrep sublim

#pgrep, if a process is found, returns 1; if there is no such process, returns 0

pgrep -q sublime_text && echo 1 || echo 0

#top, it will take longer

top|grep sublime_text
[](https://github.com/onceupon/B...

aio-stress - AIO benchmark. bandwidth - memory bandwidth benchmark. bonnie++ - hard drive and file system performance benchmark. dbench - generate I/O workloads to either a filesystem or to a networked CIFS or NFS server. dnsperf - authorative and recursing DNS servers. filebench - model based file system workload generator. fio - I/O benchmark. fs_mark - synchronous/async file creation benchmark. httperf - measure web server performance. interbench - linux interactivity benchmark. ioblazer - multi-platform storage stack micro-benchmark. iozone - filesystem benchmark. iperf3 - measure TCP/UDP/SCTP performance. kcbench - kernel compile benchmark, compiles a kernel and measures the time it takes. lmbench - Suite of simple, portable benchmarks. netperf - measure network performance, test unidirectional throughput, and end-to-end latency. netpipe - network protocol independent performance evaluator. nfsometer - NFS performance framework. nuttcp - measure network performance. phoronix-test-suite - comprehensive automated testing and benchmarking platform. seeker - portable disk seek benchmark. siege - http load tester and benchmark. sockperf - network benchmarking utility over socket API. spew - measures I/O performance and/or generates I/O load. stress - workload generator for POSIX systems. sysbench - scriptable database and system performance benchmark. tiobench - threaded IO benchmark. unixbench - the original BYTE UNIX benchmark suite, provide a basic indicator of the performance of a Unix-like system. wrk - HTTP benchmark.

[](https://github.com/onceupon/B....
lastb
[](https://github.com/onceupon/B... , print their information
who
[](https://github.com/onceupon/B...
w
[](https://github.com/onceupon/B....
users
[](https://github.com/onceupon/B...
Stop tracking a file on the terminating program
tail -f --pid=<PID> filename.txt
# Replace < PID > with the process ID of the program 
[](https://github.com/onceupon/B...
systemctl list-unit-files|grep enabled

[](https://github.com/onceupon/B...

[back to top]

[](https://github.com/onceupon/B...
lshw -json >report.json
# Other options: [- HTML] [- short] [- XML] [- JSON] [- businfo] [- sanitize], etc
[](https://github.com/onceupon/B...
sudo dmidecode -t memory
[](https://github.com/onceupon/B... CPU hardware details
dmidecode -t 4
#          Type information
#          0   BIOS
#          1 system
#          2 substrate
#          3 chassis
#          4 processor
#          5 memory controller
#          6 memory module
#          7 cache
#          8-port connector
#          9 system slot
#         11 OEM string
#         13 BIOS language
#         15 system event log
#         16 physical memory array
#         17 storage device
#         18 32 bit memory error
#         19 storage mapping address
#         20 storage device mapping address
#         21 built in positioning device
#         22 portable battery
#         23 system reset
#         24 hardware security
#         25 system power control
#         26 voltage probe
#         27 cooling device
#         28 temperature detector
#         29 current probe
#         30 remote access
#         31 guide integrity services
#         32 system startup
#         34 management device
#         35 management device components
#         36 manage device threshold data
#         37 memory channel
#         38 IPMI device
#         39 power supply
[](https://github.com/onceupon/B...
lsscsi|grep SEAGATE|wc -l
# perhaps
sg_map -i -x|grep SEAGATE|wc -l
[](https://github.com/onceupon/B...
Or the UUID of the hard disk (for example, sdb)
blkid /dev/sdb
[](https://github.com/onceupon/B...
lsblk -io KNAME,TYPE,MODEL,VENDOR,SIZE,ROTA
#Where ROTA means rotating equipment / rotating hard disk (1 is true, 0 is false)
[](https://github.com/onceupon/B... PCI (peripheral interconnect) device
lspci
# List information about NIC
lspci | egrep -i --color 'network|ethernet'
[](https://github.com/onceupon/B... USB device
lsusb
[](https://github.com/onceupon/B... Modular
# Display module status in Linux kernel
lsmod

# Add or remove modules from the Linux kernel
modprobe

# perhaps
# Remove a module
rmmod

# insert module
insmod
[](https://github.com/onceupon/B... IPMI enabled device (e.g. BMC)
# View the power status of the server remotely
ipmitool -U <bmc_username> -P <bmc_password> -I lanplus -H <bmc_ip_address> power status

# Turn on the server remotely
ipmitool -U <bmc_username> -P <bmc_password> -I lanplus -H <bmc_ip_address> power on

# Turn on the panel identification light (default 15 seconds)
ipmitool chassis identify 255

#Or server sensor temperature
ipmitool sensors |grep -i Temp

# Reset BMC
ipmitool bmc reset cold

# Prnt BMC network
ipmitool lan print 1

# Set up BMC network
ipmitool -I bmc lan set 1 ipaddr 192.168.0.55
ipmitool -I bmc lan set 1 netmask 255.255.255.0
ipmitool -I bmc lan set 1 defgw ipaddr 192.168.0.1

network

[back to top]

[](https://github.com/onceupon/B... IP address
ip a
[](https://github.com/onceupon/B...
ip r
[](https://github.com/onceupon/B... ARP cache (ARP cache shows the MAC address of the same network device you are connected to)
ip n
[](https://github.com/onceupon/B... IP address (reset after restart) (for example, add 192.168.140.3/24 to device eno16777736)
ip address add 192.168.140.3/24 dev eno16777736
[](https://github.com/onceupon/B...
sudo vi /etc/sysconfig/network-scripts/ifcfg-enoxxx
# Then edit the fields: BOOTPROT, DEVICE, IPADDR, NETMASK, GATEWAY, DNS1 etc
[](https://github.com/onceupon/B... NetworkManager
sudo nmcli c reload
[](https://github.com/onceupon/B...
sudo systemctl restart network.service
[](https://github.com/onceupon/B... hostname, OS, kernal, architecture !
hostnamectl
[](https://github.com/onceupon/B... (set all temporary, static and beautiful host names at one time)
hostnamectl set-hostname "mynode"

[](https://github.com/onceupon/B...

Other

[back to top]

[](https://github.com/onceupon/B... Automatic completion (for example, when you input "do this" and then press "tab" to display "now tomorrow never")

More cases

Complete - W "now tomorrow never" dothis
# ~$ dothis  
#Never now or tomorrow
 #After entering "n" or "t", press "tab" again to finish automatically
[](https://github.com/onceupon/B... n times (for example, printing "hello world" repeatedly 5 times)
printf 'hello world\n%.0s' {1..5}
[](https://github.com/onceupon/B... Base64 string
echo test|base64
#dGVzdAo=
[](https://github.com/onceupon/B...
username=`echo -n "bashoneliner"`
[](https://github.com/onceupon/B...
dirname `pwd`
[](https://github.com/onceupon/B... (for example, copy file A to file (B-D))
tee <fileA fileB fileC fileD >/dev/null
[](https://github.com/onceupon/B...
tr --delete '\n' <input.txt >output.txt
[](https://github.com/onceupon/B...
tr '\n' ' ' <filename
[](https://github.com/onceupon/B...
tr /a-z/ /A-Z/
[](https://github.com/onceupon/B... (for example, convert a-z to a)
echo 'something' |tr a-z a
# aaaaaaaaa
[](https://github.com/onceupon/B... (for example, fileA, fileB)
diff fileA fileB
# a: added; d: deleted; c: Modified

# perhaps
sdiff fileA fileB
# Side by side merge of file differences
[](https://github.com/onceupon/B...
Compare two files, delete the tail carriage return (for example, fileA, fileB)
 diff fileA fileB --strip-trailing-cr
[](https://github.com/onceupon/B... (for example, number fileA)
nl fileA

#perhaps
nl -nrz fileA
# add leading zeros

#It's fine too
nl -w1 -s ' '
# making it simple, blank separate
[](https://github.com/onceupon/B... tab to connect two files by field (the default connection is the first column of the file, and the default separator is space)
# File A and file B should have the same line order
join -t '\t' fileA fileB

# Use the specified fields to join (for example, the third column of file A and the fifth column of file B)
join -1 3 -2 5 fileA fileB
[](https://github.com/onceupon/B... (for example, fileA, fileB, fileC)
paste fileA fileB fileC
# Separate default options
[](https://github.com/onceupon/B...
echo 12345| rev
[](https://github.com/onceupon/B... . gz file but not unzip
zmore filename

# perhaps
zless filename
[](https://github.com/onceupon/B... , output error file
some_commands  &>log &

# perhaps
some_commands 2>log &

# perhaps
some_commands 2>&1| tee logfile

# perhaps
some_commands |& tee logfile

# Just so so
some_commands 2>&1 >>outfile
#0: standard input; 1: standard output; 2: standard error
[](https://github.com/onceupon/B...
# Run in sequence
(sleep 2; sleep 3) &

#Parallel operation
sleep 2 & sleep 3 &
[](https://github.com/onceupon/B...
Even if you log off, you can run the process (immune to hangups, with output to a non TTY)
# For example, even if you log off, the myscript.sh script will run
nohup bash myscript.sh
[](https://github.com/onceupon/B...
Here's the message -a /path/to/attach_file.txt -s 'mail.subject' me@gmail.com
# use -a flag to set send from (-a "From: some@mail.tld")
[](https://github.com/onceupon/B... . xls to csv
xls2csv filename
[](https://github.com/onceupon/B... (for example, append hihi content to the specified file)
echo 'hihi' >>filename
[](https://github.com/onceupon/B... BEEP's voice
speaker-test -t sine -f 1000 -l1
[](https://github.com/onceupon/B... beep sound duration
(speaker-test -t sine -f 1000) & pid=$!;sleep 0.1s;kill -9 $pid
[](https://github.com/onceupon/B... Edit / delete
~/.bash_history

#perhaps
history -d [line_number]
[](https://github.com/onceupon/B...
# list 5 previous command (similar to `history |tail -n 5` but wont print the history command itself)
fc -l -5
[](https://github.com/onceupon/B...
head !$
[](https://github.com/onceupon/B... It's easy to use)
clear

# perhaps
Ctrl+l
[](https://github.com/onceupon/B...
cat /directory/to/file
echo 100>!$
[](https://github.com/onceupon/B... .xf
unxz filename.tar.xz
# Then?
tar -xf filename.tar
[](https://github.com/onceupon/B... python package
pip install packagename
[](https://github.com/onceupon/B... bash command
Ctrl+U

# perhaps
Ctrl+C

# perhaps
Alt+Shift+#
# Become history
[](https://github.com/onceupon/B...
Add something to history (for example, "add to history")
# addmetodistory
# just add a "#" before~~
[](https://github.com/onceupon/B...
sleep 5;echo hi
[](https://github.com/onceupon/B... rsync backup
rsync -av filename filename.bak
rsync -av directory directory.bak
rsync -av --ignore_existing directory/ directory.bak
rsync -av --update directory directory.bak

rsync -av directory user@ip_address:/path/to/directory.bak
#Skip updated files on the receiver (I prefer this!)
[](https://github.com/onceupon/B...
mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat}
# -p: set as parent directory
# This will make project / Doc / HTML /; project / Doc / Info; project / lib / ext, etc
[](https://github.com/onceupon/B...
cd tmp/ && tar xvf ~/a.tar
[](https://github.com/onceupon/B...
cd tmp/a/b/c ||mkdir -p tmp/a/b/c
[](https://github.com/onceupon/B...
tar xvf -C /path/to/directory filename.gz
[](https://github.com/onceupon/B... "" to interrupt long commands
cd tmp/a/b/c \
> || \
>mkdir -p tmp/a/b/c
[](https://github.com/onceupon/B... pwd
VAR=$PWD; cd ~; tar xvf -C $VAR file.tar
# PWD must be capital
[](https://github.com/onceupon/B... (e.g. /tmp/)
file /tmp/
# tmp/: directory
[](https://github.com/onceupon/B... Script
#!/bin/bash
file=${1#*.}
# remove string before a "."
[](https://github.com/onceupon/B... Simple HTTP service
python -m SimpleHTTPServer
# Or when you use Python 3:
python3 -m http.server
Read user input
read input
echo $input
[](https://github.com/onceupon/B... 1-10
seq 10
[](https://github.com/onceupon/B...
i=`wc -l filename|cut -d ' ' -f1`; cat filename| echo "scale=2;(`paste -sd+`)/"$i|bc
[](https://github.com/onceupon/B... (e.g. 1,2)
echo {1,2}{1,2}
# 1 1, 1 2, 2 1, 2 2
[](https://github.com/onceupon/B... (e.g. A,T,C,G)
set = {A,T,C,G}
group= 5
for ((i=0; i<$group; i++));do
    repetition=$set$repetition;done
    bash -c "echo "$repetition""
[](https://github.com/onceupon/B...
foo=$(<test1)
[](https://github.com/onceupon/B...
echo ${#foo}
[](https://github.com/onceupon/B...
echo -e ' \t '
[](https://github.com/onceupon/B...
declare -a array=()

# perhaps
declare array=()

# Or associative array
declare -A array=()
[](https://github.com/onceupon/B...
scp -r directoryname user@ip:/path/to/send
[](https://github.com/onceupon/B...
# E.g. 1000 lines / smallfile
split -d -l 1000 largefile.txt

# Split by bytes without breaking lines between files
split -C 10 largefile.txt
[](https://github.com/onceupon/B... (e.g 100000 files, 10 byte split):
#1 \. Create file
dd if=/dev/zero of=bigfile bs=1 count=1000000

#2 \. Split the large file into 100000 10 byte files
 split -b 10 -a 10 bigfile
[](https://github.com/onceupon/B... (e.g. rename all files to ABC)
rename 's/ABC//' *.gz
[](https://github.com/onceupon/B... Delete. GZ of filename.gz)
basename filename.gz .gz

zcat filename.gz> $(basename filename.gz .gz).unpacked
[](https://github.com/onceupon/B...
Fork bomb
#Don't try this at home
 #It is a function that will be called twice every time until the system resource is exhausted
 #For security, add a "X" to the front. When you really test, please remove it
# :(){:|:&};:
[](https://github.com/onceupon/B... .txt)
rename s/$/.txt/ *
# You can use rename - n s/$/.txt / * to check the results first, if it just prints these:
# rename(a, a.txt)
# rename(b, b.txt)
# rename(c, c.txt)
[](https://github.com/onceupon/B... (for example, / T / T -- > / T)
tr -s "/t" < filename
[](https://github.com/onceupon/B... echo print nextline
echo -e 'text here \c'
[](https://github.com/onceupon/B...
!$
[](https://github.com/onceupon/B...
echo $?
[](https://github.com/onceupon/B...
head -c 50 file
[](https://github.com/onceupon/B...
# for example
# AAAA
# BBBB
# CCCC
# DDDD
cat filename|paste - -
# AAAABBBB
# CCCCDDDD
cat filename|paste - - - -
# AAAABBBBCCCCDDDD
[](https://github.com/onceupon/B... Turn fasta
cat file.fastq | paste - - - - | sed 's/^@/>/g'| cut -f1-2 | tr '\t' '\n' >file.fa
[](https://github.com/onceupon/B...
cat file|rev | cut -d/ -f1 | rev
[](https://github.com/onceupon/B... Add a variable in i + + (for example, $val)
((var++))
# perhaps
var=$((var+1))
[](https://github.com/onceupon/B... (for example, filename)
>filename
[](https://github.com/onceupon/B... tar.bz2 file (for example, unzip file.tar.bz2)
tar xvfj file.tar.bz2
[](https://github.com/onceupon/B... tar.xz file (for example, unzip file.tar.xz)
unxz file.tar.xz
tar xopf file.tar
[](https://github.com/onceupon/B... y/n until termination
# 'y':
yes

# Or'n':
yes n

# Or 'anything':
yes anything

# For example:

yes | rm -r large_directory


##### [](https://github.com/onceupon/Bash-OneLiner#create-dummy-file-of-certain-size-instantly-eg-200mb)



##### Create a virtual file of a certain size immediately (for example, 200mb)

dd if=/dev/zero of=//dev/shm/200m bs=1024k count=200

perhaps

dd if=/dev/zero of=//dev/shm/200m bs=1M count=200

Standard output:

200 + 0 records

200 + 0 recorded

209715200 bytes (210 MB) copied, 0.0955679 s, 2.2 GB/s


##### [](https://Github.com/onceupon/bash-oneliner (cat-to-a-file)

cat >myfile
Let me add here
exit by control + c
^C


##### [](https://Github.com/onceupon/bash-oneliner ා keep repeatedly executing the same command eg repeat WC -- l-filename-every-1-second) keeps executing the same command repeatedly (for example, 'wc -l filename' every second)

watch -n 1 wc -l filename


##### [](https://Github.com/onceupon/bash-oneliner ා print-commands-and-their-arguments-when-execute-eg-echo-expr-10 -- 20 -) prints commands and their parameters (for example, echo ` expr 10 + 20 ')

set -x; echo expr 10 + 20


##### [](https://Github.com/onceupon/bash-oneliner? Print some meaningful sentences to you install fortune first

fortune


##### [](https://Github.com/onceupon/bash-oneliner "colorful and useful version of top install top first"

htop


##### [](https://GitHub. COM / onceupon / bash oneliner (press any key to continue)

read -rsp $'Press any key to continue...n' -n1 key


##### [](https://GitHub. COM / onceupon / bash oneliner ා run sql like command on files from terminal)

Download:

https://github.com/harelba/q

For example:

q -d "," "select c3,c4,c5 from /path/to/file.txt where c3='foo' and c5='boo'"


##### [](https://GitHub. COM / onceupon / bash oneliner (using Screen for multiple terminal sessions)

Create a session and attach:

screen

To create a detached session foo:

screen -S foo -d -m

Independent session foo:

screen: ^a^d

Session list:

screen -ls

Attach to previous session:

screen -r

Attach to session foo:

screen -r foo

Kill the conversation foo:

screen -r foo -X quit

Roll:

Click on the screen prefix combination (C-a / control+A), then press Escape
Move up and down the direction key (↑ and ↓)

Redirect the output of a running process in the screen:

(C-a / control+A), then hit 'H'

Screen reality of storage screen:

Ctrl+A, Shift+H

Then find the screen.log file in the current directory


##### [](https://GitHub. COM / onceupon / bash oneliner (using TMUX for multiple terminal sessions) 


##### Using Tmux for multiple terminal callbacks

Create a session and attach:

tmux

Attach to session foo:

tmux attach -t foo

Separated session foo:

^bd

Session list:

tmux ls

Attach last session:

tmux attach

Kill conversation foo:

tmux kill-session -t foo

Create a stand-alone session foo:

tmux new -s foo -d

Send commands to all panes of tmux:

Ctrl-B
:setw synchronize-panes

Some commands controlled by tmux Pane:

Ctrl-B

Pane (split), press Ctrl+B, and enter the following characters:

% horizontal split

" vertical split

o swap panes

q show pane numbers

x kill pane

Space - switch between layouts

Vertical distribution (rows):

select-layout even-vertical

perhaps

Ctrl+b, Alt+2

Vertical distribution (columns):

select-layout even-horizontal

perhaps

Ctrl+b, Alt+1

Scroll

Ctrl-b and then [then you can use your normal arrow keys to scroll
Press q to quit scroll mode.


##### [](https://GitHub. COM / onceupon / bash oneliner (cut the last column)

cat filename|rev|cut -f1|rev


##### [](https://GitHub. COM / onceupon / bash oneliner ා pass password to ssh)

sshpass -p mypassword ssh root@10.102.14.88 "df -h"


##### [](https://Github.com/onceupon/bash-oneliner "wait-for-a-pid-job-to-complete" waits for a PID (task) to complete

wait %1

perhaps

wait $PID
wait ${!}

Wait ${!} to wait for the last background ($! Is the piid of the last background)


##### [](https://Github.com/onceupon/bash-oneliner × convert-pdf-to-txt) convert PDF to TXT

sudo apt-get install poppler-utils
pdftotext example.pdf example.txt


##### [](https://GitHub. COM / onceupon / bash oneliner (list only directory)

ls -ld -- */


##### [](https://Github.com/onceupon/bash-oneliner × capturecordsave-terminal-output-capture-everything-you-type-and-output) capture / j record / save terminal output (capture all your input and output)

script output.txt

Start using terminal l

Exit screen session (stop saving content), exit


#####Lists the contents of the directory in a tree format.

tree

Go to the directory you want to list and type tree (sudo apt get install tree)

output:

home/

└── project

├── 1

├── 2

├── 3

├── 4

└── 5

Set directory depth level (for example, level 1)

tree -L 1

home/

└── project


##### [](https://Github.com/onceupon/bash-oneliner × set-up-virtualenv sandbox-for-python) set virtualenv (sandbox) for Python

1. Install virtualenv.

sudo apt-get install virtualenv

2. Create a directory for the new isolation environment (name it. venv or whatever you want).

virtualenv .venv

3. Import the virtual execution directory

source .venv/bin/activate

4. You can check whether it is in the sandbox now

type pip

5. Now you can install your pip package. requirements.txt here is just a TXT file containing all the packages you want (for example, tornado==4.5.3).

pip install -r requirements.txt


##### [](https://Github.com/onceupon/bash-oneliner ා working with json data) using json data

Install the very useful jq package

sudo apt-get install jq

e.g. to get all the values of the 'url' key, simply pipe the json to the following jq command(you can use .[]. to select inner json, i.e jq '.[].url')

For example, to get all the values of the url key, simply pipe the json to the following jq command (you can use [] to select the internal json, that is, jq '[].url')

jq '.url'


##### [](https://Github.com/onceupon/bash-oneliner (editing your history) edit history

history -w
vi ~/.bash_history
history -r


##### [](https://Github.com/onceupon/bash-oneliner ා decimal-to-binary-eg-get-binary-of-5) convert decimal to binary (for example, get binary of 5)

D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
echo -e ${D2B[5]}

00000101

echo -e ${D2B[255]}

11111111


##### [](https://Github.com/onceupon/bash-oneliner (wrap-each-input-line-to-fit-in-specified-width-eg-4-integers-per-line)

echo "00110010101110001101" | fold -w4

0011

0010

1011

1000

1101


##### [](https://Github.com/onceupon/bash-oneliner (sort-a-file-by-column-and-keep-the-original-order) sorts files by column and keeps the original order

sort -k3,3 -s


##### [](https://Github.com/onceupon/bash-oneliner ා right-align-a-column-right-align-the-2nd-column)

cat file.txt|rev|column -t|rev


##### [](https://Github.com/onceupon/bash-oneliner (to-both-view-and-store-the-output)

echo 'hihihihi' | tee outputfile.txt

tee with "- a" can be attached to the file


##### [](https://Github.com/onceupon/bash-oneliner (show non-printing-Ctrl-characters-with-cat) use cat to display non printing (Ctrl) characters

cat -v filename


##### [](https://GitHub. COM / onceupon / bash oneliner (convert tab to space)

expand filename


##### [](https://Github.com/onceupon/bash-oneliner × convert-space-to-tab) convert spaces to tabs

unexpand filename


##### [](https://Github.com/onceupon/bash-oneliner × display-file-in-total -- you-can-also-use-od-to-display-hexadecimal-decimal-etc) displays files in octal (you can also use od to display hex, decimal, etc.)

od filename


##### [](https://Github.com/onceupon/bash-oneliner (reverse cat-a-file) reverse the result of 'cat'

tac filename


##### [](https://Github.com/onceupon/bash-oneliner? Reverse the result from uniq -- C) reverse the result of 'uniq-c'

while read a b; do yes $b |head -n $a ;done <test.txt

>There will be more in the future!

Tags: PHP github sudo Python Session

Posted on Mon, 04 Nov 2019 03:13:44 -0500 by Webxplosion