mirror of
https://github.com/onceupon/Bash-Oneliner.git
synced 2024-11-26 06:17:01 +00:00
edit all comments to start with '#' instead of '//'
This commit is contained in:
parent
478751bba0
commit
7ae9dbbce9
171
README.md
171
README.md
@ -144,9 +144,9 @@ grep $'\t'
|
|||||||
```bash
|
```bash
|
||||||
$echo "$long_str"|grep -q "$short_str"
|
$echo "$long_str"|grep -q "$short_str"
|
||||||
if [ $? -eq 0 ]; then echo 'found'; fi
|
if [ $? -eq 0 ]; then echo 'found'; fi
|
||||||
|
#grep -q will output 0 if match found
|
||||||
|
#remember to add space between []!
|
||||||
```
|
```
|
||||||
//grep -q will output 0 if match found
|
|
||||||
//remember to add space between []!
|
|
||||||
|
|
||||||
##### Grep strings between a bracket()
|
##### Grep strings between a bracket()
|
||||||
```bash
|
```bash
|
||||||
@ -156,8 +156,9 @@ grep -oP '\(\K[^\)]+'
|
|||||||
##### Grep number of characters with known strings in between(e.g. AAEL000001-RA)
|
##### Grep number of characters with known strings in between(e.g. AAEL000001-RA)
|
||||||
```bash
|
```bash
|
||||||
grep -o -w "\w\{10\}\-R\w\{1\}"
|
grep -o -w "\w\{10\}\-R\w\{1\}"
|
||||||
|
# \w word character [0-9a-zA-Z_] \W not word character
|
||||||
```
|
```
|
||||||
// \w word character [0-9a-zA-Z_] \W not word character
|
|
||||||
|
|
||||||
|
|
||||||
##### A lot examples here
|
##### A lot examples here
|
||||||
@ -197,11 +198,9 @@ e.g. add >$i to the first line (to make a FASTA file)
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
sed "1i >$i"
|
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
|
||||||
```
|
```
|
||||||
//notice the double quotes! in other examples, you can use a single quote, but here, no way!
|
|
||||||
//'1i' means insert to first line
|
|
||||||
|
|
||||||
|
|
||||||
##### Delete/remove empty lines
|
##### Delete/remove empty lines
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -289,8 +288,10 @@ sed -n 500,5000p filename
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
sed -n '0~3p' filename
|
sed -n '0~3p' filename
|
||||||
|
|
||||||
|
# catch 0: start; 3: step
|
||||||
```
|
```
|
||||||
//catch 0: start; 3: step
|
|
||||||
|
|
||||||
|
|
||||||
##### Print every odd # lines
|
##### Print every odd # lines
|
||||||
@ -315,10 +316,9 @@ sed -e 's/^[ \t]*//'
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
sed 's/ *//'
|
sed 's/ *//'
|
||||||
|
|
||||||
|
# notice a whitespace before '*'!!
|
||||||
```
|
```
|
||||||
//notice a whitespace before '*'!!
|
|
||||||
|
|
||||||
|
|
||||||
##### Remove ending commas
|
##### Remove ending commas
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -328,9 +328,11 @@ sed 's/,$//g'
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
sed "s/$/\t$i/"
|
sed "s/$/\t$i/"
|
||||||
|
|
||||||
|
# $i is the valuable you want to add
|
||||||
|
# e.g. add the filename to every last column of the file
|
||||||
```
|
```
|
||||||
//$i is the valuable you want to add
|
|
||||||
e.g. add the filename to every last column of the file
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
for i in $(ls);do sed -i "s/$/\t$i/" $i;done
|
for i in $(ls);do sed -i "s/$/\t$i/" $i;done
|
||||||
@ -565,12 +567,10 @@ xargs -d\t
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo 1 2 3 4 5 6| xargs -n 3
|
echo 1 2 3 4 5 6| xargs -n 3
|
||||||
|
# 1 2 3
|
||||||
|
# 4 5 6
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
//1 2 3
|
|
||||||
4 5 6
|
|
||||||
|
|
||||||
|
|
||||||
##### Prompt before execution
|
##### Prompt before execution
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -581,12 +581,10 @@ echo a b c |xargs -p -n 3
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
xargs -t abcd
|
xargs -t abcd
|
||||||
|
# bin/echo abcd
|
||||||
|
# abcd
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
///bin/echo abcd
|
|
||||||
//abcd
|
|
||||||
|
|
||||||
|
|
||||||
##### With find and rm
|
##### With find and rm
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -637,12 +635,12 @@ time echo {1..5} |xargs -n1 sleep
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
find /dir/to/A -type f -name "*.py" -print 0| xargs -0 -r -I file cp -v -p file --target-directory=/path/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)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
//v: verbose|
|
|
||||||
//p: keep detail (e.g. owner)
|
|
||||||
|
|
||||||
|
|
||||||
##### With sed
|
##### With sed
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -671,9 +669,9 @@ ls -l| xargs
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo mso{1..8}|xargs -n1 bash -c 'echo -n "$1:"; ls -la "$1"| grep -w 74 |wc -l' --
|
echo mso{1..8}|xargs -n1 bash -c 'echo -n "$1:"; ls -la "$1"| grep -w 74 |wc -l' --
|
||||||
```
|
|
||||||
// "--" signals the end of options and display further option processing
|
|
||||||
|
|
||||||
|
# "--" signals the end of options and display further option processing
|
||||||
|
```
|
||||||
|
|
||||||
##### Download dependencies files and install (e.g. requirements.txt)
|
##### Download dependencies files and install (e.g. requirements.txt)
|
||||||
|
|
||||||
@ -727,10 +725,8 @@ if no subdirectory
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
replace "www" "w" -- *
|
replace "www" "w" -- *
|
||||||
|
# a space before *
|
||||||
```
|
```
|
||||||
//a space before *
|
|
||||||
|
|
||||||
|
|
||||||
##### Find and output only filename (e.g. "mso")
|
##### Find and output only filename (e.g. "mso")
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -741,8 +737,10 @@ find mso*/ -name M* -printf "%f\n"
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
find . -name "*.mso" -size -74c -delete
|
find . -name "*.mso" -size -74c -delete
|
||||||
|
|
||||||
|
# M for MB, etc
|
||||||
```
|
```
|
||||||
//M for MB, etc
|
|
||||||
|
|
||||||
|
|
||||||
## Loops
|
## Loops
|
||||||
@ -751,9 +749,9 @@ find . -name "*.mso" -size -74c -delete
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
while read a b c; do echo $(($c-$b));done < <(head filename)
|
while read a b c; do echo $(($c-$b));done < <(head filename)
|
||||||
```
|
|
||||||
//there is a space between the two '<'s
|
|
||||||
|
|
||||||
|
#there is a space between the two '<'s
|
||||||
|
```
|
||||||
##### While loop, sum up column subtraction
|
##### While loop, sum up column subtraction
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -766,19 +764,17 @@ i=0; while read a b c; do ((i+=$c-$b)); echo $i; done < <(head filename)
|
|||||||
while [[ $(pidof perl) ]];do echo f;sleep 10;done && python timetorunpython.py
|
while [[ $(pidof perl) ]];do echo f;sleep 10;done && python timetorunpython.py
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
##### If loop
|
##### If loop
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
if (($j==$u+2))
|
if (($j==$u+2))
|
||||||
|
#(( )) use for arithmetic operation
|
||||||
```
|
```
|
||||||
//(( )) use for arithmetic operation
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
if [[$age >21]]
|
if [[$age >21]]
|
||||||
|
#[[ ]] use for comparison
|
||||||
```
|
```
|
||||||
//[[ ]] use for comparison
|
|
||||||
|
|
||||||
|
|
||||||
##### Test if file exist
|
##### Test if file exist
|
||||||
```bash
|
```bash
|
||||||
@ -864,24 +860,23 @@ echo "var=5;--var"| bc
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Download
|
## Download
|
||||||
[[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)]
|
[[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)]
|
||||||
##### Download all from a page
|
##### Download all from a page
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
wget -r -l1 -H -t1 -nd -N -np -A mp3 -e robots=off http://example.com
|
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
|
||||||
|
# -A: type (seperate by ,)
|
||||||
|
# -e robots=off: ignore the robots.txt file which stop wget from crashing the site, sorry 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
|
|
||||||
//-A: type (seperate by ,)
|
|
||||||
//-e robots=off: ignore the robots.txt file which stop wget from crashing the site, sorry example.com
|
|
||||||
|
|
||||||
##### Upload a file to web and download (https://transfer.sh/)
|
##### Upload a file to web and download (https://transfer.sh/)
|
||||||
--> upload:
|
--> upload:
|
||||||
@ -1041,16 +1036,15 @@ tr /a-z/ /A-Z/
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
diff fileA fileB
|
diff fileA fileB
|
||||||
|
# a: added; d:delete; c:changed
|
||||||
```
|
```
|
||||||
//a: added; d:delete; c:changed
|
|
||||||
|
|
||||||
or
|
or
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
sdiff fileA fileB
|
sdiff fileA fileB
|
||||||
|
# side-to-side merge of file differences
|
||||||
```
|
```
|
||||||
//side-to-side merge of file differences
|
|
||||||
|
|
||||||
|
|
||||||
##### Number a file (e.g. fileA)
|
##### Number a file (e.g. fileA)
|
||||||
|
|
||||||
@ -1061,23 +1055,19 @@ or
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
nl -nrz fileA
|
nl -nrz fileA
|
||||||
|
# add leading zeros
|
||||||
```
|
```
|
||||||
//add leading zeros
|
|
||||||
|
|
||||||
or
|
or
|
||||||
```bash
|
```bash
|
||||||
nl -w1 -s ' '
|
nl -w1 -s ' '
|
||||||
|
# making it simple, blank seperated
|
||||||
```
|
```
|
||||||
//making it simple, blank seperated
|
|
||||||
|
|
||||||
##### Combine/ paste two files (e.g. fileA, fileB)
|
##### Combine/ paste two files (e.g. fileA, fileB)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
paste fileA fileB
|
paste fileA fileB
|
||||||
|
# default tab seperated
|
||||||
```
|
```
|
||||||
//default tab seperated
|
|
||||||
|
|
||||||
|
|
||||||
##### Reverse string
|
##### Reverse string
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -1118,17 +1108,15 @@ or
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
some_commands 2>&1 >>outfile
|
some_commands 2>&1 >>outfile
|
||||||
|
#0: standard input; 1: standard output; 2: standard error
|
||||||
```
|
```
|
||||||
//0: standard input; 1: standard output; 2: standard error
|
|
||||||
|
|
||||||
|
|
||||||
##### Send mail
|
##### Send mail
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo 'heres the content'| mail -a /path/to/attach_file.txt -s 'mail.subject' me@gmail.com
|
echo 'heres the content'| mail -a /path/to/attach_file.txt -s 'mail.subject' me@gmail.com
|
||||||
|
# use -a flag to set send from (-a "From: some@mail.tld")
|
||||||
```
|
```
|
||||||
//use -a flag to set send from (-a "From: some@mail.tld")
|
|
||||||
|
|
||||||
|
|
||||||
##### .xls to csv
|
##### .xls to csv
|
||||||
|
|
||||||
@ -1211,9 +1199,8 @@ or
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
!c
|
!c
|
||||||
|
# run cat filename again
|
||||||
```
|
```
|
||||||
//run cat filename again
|
|
||||||
|
|
||||||
|
|
||||||
##### Extract .xf
|
##### Extract .xf
|
||||||
|
|
||||||
@ -1242,17 +1229,15 @@ or
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
Alt+Shift+#
|
Alt+Shift+#
|
||||||
|
# to make it to history
|
||||||
```
|
```
|
||||||
//to make it to history
|
|
||||||
|
|
||||||
|
|
||||||
##### Add something to history (e.g. "addmetohistory")
|
##### Add something to history (e.g. "addmetohistory")
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
#addmetodistory
|
#addmetodistory
|
||||||
|
#just add a "#" before~~
|
||||||
```
|
```
|
||||||
//just add a "#" before~~
|
|
||||||
|
|
||||||
|
|
||||||
##### Sleep awhile or wait for a moment or schedule a job
|
##### Sleep awhile or wait for a moment or schedule a job
|
||||||
|
|
||||||
@ -1282,10 +1267,9 @@ rsync -av directory user@ip_address:/path/to/directory.bak
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat}
|
mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat}
|
||||||
|
# -p: make parent directory
|
||||||
|
# this will create project/doc/html/; project/doc/info; project/lib/ext ,etc
|
||||||
```
|
```
|
||||||
//-p: make parent directory
|
|
||||||
//this will create project/doc/html/; project/doc/info; project/lib/ext ,etc
|
|
||||||
|
|
||||||
|
|
||||||
##### Run command only if another command returns zero exit status (well done)
|
##### Run command only if another command returns zero exit status (well done)
|
||||||
|
|
||||||
@ -1317,15 +1301,16 @@ cd tmp/a/b/c \
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
VAR=$PWD; cd ~; tar xvf -C $VAR file.tar
|
VAR=$PWD; cd ~; tar xvf -C $VAR file.tar
|
||||||
|
# PWD need to be capital letter
|
||||||
```
|
```
|
||||||
//PWD need to be capital letter
|
|
||||||
|
|
||||||
##### List file type of file (e.g. /tmp/)
|
##### List file type of file (e.g. /tmp/)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
file /tmp/
|
file /tmp/
|
||||||
|
# tmp/: directory
|
||||||
```
|
```
|
||||||
//tmp/: directory
|
|
||||||
|
|
||||||
|
|
||||||
##### Bash script
|
##### Bash script
|
||||||
@ -1333,13 +1318,13 @@ file /tmp/
|
|||||||
```bash
|
```bash
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
file=${1#*.}
|
file=${1#*.}
|
||||||
|
# remove string before a "."
|
||||||
```
|
```
|
||||||
//remove string before a "."
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
file=${1%.*}
|
file=${1%.*}
|
||||||
|
# remove string after a "."
|
||||||
```
|
```
|
||||||
//remove string after a "."
|
|
||||||
|
|
||||||
##### Search from history
|
##### Search from history
|
||||||
|
|
||||||
@ -1367,7 +1352,7 @@ e.g. with grep
|
|||||||
```bash
|
```bash
|
||||||
test="god the father"
|
test="god the father"
|
||||||
grep ${test// /\\\|} file.txt
|
grep ${test// /\\\|} file.txt
|
||||||
#turning the space into 'or' (\|) in grep
|
# turning the space into 'or' (\|) in grep
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Read user input
|
##### Read user input
|
||||||
@ -1393,9 +1378,8 @@ i=`wc -l filename|cut -d ' ' -f1`; cat filename| echo "scale=2;(`paste -sd+`)/"$
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo {1,2}{1,2}
|
echo {1,2}{1,2}
|
||||||
|
# 1 1, 1 2, 2 1, 2 2
|
||||||
```
|
```
|
||||||
//1 1, 1 2, 2 1, 2 2
|
|
||||||
|
|
||||||
##### Generate all combination (e.g. A,T,C,G)
|
##### Generate all combination (e.g. A,T,C,G)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -1750,8 +1734,8 @@ export PATH=$PATH:~/path/you/want
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
chmod +x filename
|
chmod +x filename
|
||||||
|
# you can now ./filename to execute it
|
||||||
```
|
```
|
||||||
//you can now ./filename to execute it
|
|
||||||
|
|
||||||
##### List screen
|
##### List screen
|
||||||
|
|
||||||
@ -1789,8 +1773,9 @@ passwd username
|
|||||||
```bash
|
```bash
|
||||||
1. joe ~/.bash_profile
|
1. joe ~/.bash_profile
|
||||||
2. export PS1='\u@\h:\w\$'
|
2. export PS1='\u@\h:\w\$'
|
||||||
|
# $PS1 is a variable that defines the makeup and style of the command prompt
|
||||||
```
|
```
|
||||||
//$PS1 is a variable that defines the makeup and style of the command prompt
|
|
||||||
```bash
|
```bash
|
||||||
3. source ~/.bash_profile
|
3. source ~/.bash_profile
|
||||||
```
|
```
|
||||||
@ -1807,9 +1792,8 @@ passwd username
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
$echo $PATH
|
$echo $PATH
|
||||||
|
# list of directories separated by a colon
|
||||||
```
|
```
|
||||||
//list of directories separated by a colon
|
|
||||||
|
|
||||||
##### List all environment variables for current user
|
##### List all environment variables for current user
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -1826,8 +1810,8 @@ lsblk
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
ln -s /path/to/program /home/usr/bin
|
ln -s /path/to/program /home/usr/bin
|
||||||
|
# must be the whole path to the program
|
||||||
```
|
```
|
||||||
//must be the whole path to the program
|
|
||||||
|
|
||||||
##### Show hexadecimal view of data
|
##### Show hexadecimal view of data
|
||||||
|
|
||||||
@ -1947,22 +1931,22 @@ getent database_name
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
getent passwd
|
getent passwd
|
||||||
|
# list all user account (all local and LDAP)
|
||||||
|
# (e.g. fetch list of grop accounts)
|
||||||
```
|
```
|
||||||
//list all user account (all local and LDAP)
|
|
||||||
(e.g. fetch list of grop accounts)
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
getent group
|
getent group
|
||||||
|
# store in database 'group'
|
||||||
```
|
```
|
||||||
//store in database 'group'
|
|
||||||
|
|
||||||
##### Change owner of file
|
##### Change owner of file
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
chown user_name filename
|
chown user_name filename
|
||||||
chown -R user_name /path/to/directory/
|
chown -R user_name /path/to/directory/
|
||||||
|
# chown user:group filename
|
||||||
```
|
```
|
||||||
//chown user:group filename
|
|
||||||
|
|
||||||
##### List current mount detail
|
##### List current mount detail
|
||||||
|
|
||||||
@ -2012,9 +1996,8 @@ if [$(id -u) -ne 0];then
|
|||||||
echo "You are not root!"
|
echo "You are not root!"
|
||||||
exit;
|
exit;
|
||||||
fi
|
fi
|
||||||
|
# 'id -u' output 0 if it's not root
|
||||||
```
|
```
|
||||||
//'id -u' output 0 if it's not root
|
|
||||||
|
|
||||||
##### Find out CPU information
|
##### Find out CPU information
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -2039,11 +2022,10 @@ quota -v username
|
|||||||
```
|
```
|
||||||
|
|
||||||
##### Fork bomb
|
##### Fork bomb
|
||||||
|
# dont try this at home
|
||||||
```bash
|
```bash
|
||||||
:(){:|:&};:
|
:(){:|:&};:
|
||||||
```
|
```
|
||||||
//dont try this at home
|
|
||||||
|
|
||||||
##### Check user login
|
##### Check user login
|
||||||
|
|
||||||
@ -2055,9 +2037,8 @@ lastlog
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
joe /etc/environment
|
joe /etc/environment
|
||||||
|
# edit this file
|
||||||
```
|
```
|
||||||
//edit this file
|
|
||||||
|
|
||||||
##### Show running processes
|
##### Show running processes
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
Loading…
Reference in New Issue
Block a user