From cca8010a69fce338e5fb0f19246acaedd551086a Mon Sep 17 00:00:00 2001 From: onceupon Date: Fri, 9 Jun 2017 16:53:36 +0800 Subject: [PATCH] Create README.md --- README.md | 535 +++++++++++++++++++++++++++--------------------------- 1 file changed, 267 insertions(+), 268 deletions(-) diff --git a/README.md b/README.md index bb59215..5035fbb 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ http://onceupon.github.io/Bash-Oneliner/ - [System](#system) ## Grep -##### extract text bewteen words (e.g. w1,w2) +##### Extract text bewteen words (e.g. w1,w2) ```bash grep -o -P '(?<=w1).*(?=w2)' @@ -44,19 +44,19 @@ grep -v bbo filename grep -m 1 bbo filename ``` -##### grep and count (e.g. bbo) +##### Grep and count (e.g. bbo) ```bash grep -c bbo filename ``` -##### insensitive grep (e.g. bbo/BBO/Bbo) +##### Insensitive grep (e.g. bbo/BBO/Bbo) ```bash grep -i "bbo" filename ``` -##### count occurrence (e.g. three times a line count three times) +##### Count occurrence (e.g. three times a line count three times) ```bash grep -o bbo filename @@ -68,7 +68,7 @@ grep -o bbo filename grep --color bbo filename ``` -##### grep search all files in a directory(e.g. bbo) +##### Grep search all files in a directory(e.g. bbo) ```bash grep -R bbo /path/to/directory @@ -79,7 +79,7 @@ or ```bash grep -r bbo /path/to/directory ``` -##### search all files in directory, only output file names with matches(e.g. bbo) +##### Search all files in directory, only output file names with matches(e.g. bbo) ```bash grep -Rh bbo /path/to/directory @@ -89,31 +89,31 @@ or grep -rh bbo /path/to/directory ``` -##### grep OR (e.g. A or B or C or D) +##### Grep OR (e.g. A or B or C or D) ``` grep 'A\|B\|C\|D' ``` -##### grep AND (e.g. A and B) +##### Grep AND (e.g. A and B) ```bash grep 'A.*B' ``` -##### grep all content of a fileA from fileB +##### Grep all content of a fileA from fileB ```bash grep -f fileA fileB ``` -##### grep a tab +##### Grep a tab ```bash grep $'\t' ``` -##### grep variable from variable +##### Grep variable from variable ```bash $echo "$long_str"|grep -q "$short_str" if [ $? -eq 0 ]; then echo 'found'; fi @@ -121,19 +121,19 @@ if [ $? -eq 0 ]; then echo 'found'; fi //grep -q will output 0 if match found //remember to add space between []! -##### grep strings between a bracket() +##### Grep strings between a bracket() ```bash 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 grep -o -w "\w\{10\}\-R\w\{1\}" ``` // \w word character [0-9a-zA-Z_] \W not word character -##### a lot examples here +##### A lot examples here http://www.cyberciti.biz/faq/grep-regular-expressions/ @@ -141,18 +141,18 @@ http://www.cyberciti.biz/faq/grep-regular-expressions/ ## Sed [[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] -##### remove lines with word (e.g. bbo) +##### Remove lines with word (e.g. bbo) ```bash sed "/bbo/d" filename ``` -##### edit infile (edit and save) +##### Edit infile (edit and save) ```bash sed -i "/bbo/d" filename ``` -##### when using variable (e.g. $i), use double quotes " " +##### When using variable (e.g. $i), use double quotes " " e.g. add >$i to the first line (to make a FASTA file) ```bash @@ -162,7 +162,7 @@ sed "1i >$i" //'1i' means insert to first line -##### delete empty lines +##### Delete empty lines ```bash sed '/^\s*$/d' @@ -172,67 +172,67 @@ or ```bash sed 's/^$/d' ``` -##### delete last line +##### Delete last line ```bash sed '$d' ``` -##### delete last character from end of file +##### Delete last character from end of file ```bash sed -i '$ s/.$//' filename ``` -##### add string to end of file (e.g. "]") +##### Add string to end of file (e.g. "]") ```bash sed '$s/$/]/' filename ``` -##### add newline to the end +##### Add newline to the end ```bash sed '$a\' ``` -##### add string to beginning of every line (e.g. bbo) +##### Add string to beginning of every line (e.g. bbo) ```bash sed -e 's/^/bbo/' file ``` -##### add string to end of each line (e.g. "}") +##### Add string to end of each line (e.g. "}") ```bash sed -e 's/$/\}\]/' filename ``` -##### add \n every nth character (e.g. every 4th character) +##### Add \n every nth character (e.g. every 4th character) ```bash sed 's/.\{4\}/&\n/g' ``` -##### concatenate/combine/join files with a seperator and next line (e.g seperate by ",") +##### Concatenate/combine/join files with a seperator and next line (e.g seperate by ",") ```bash sed -s '$a,' *.json > all.json ``` -##### substitution (e.g. replace A by B) +##### Substitution (e.g. replace A by B) ```bash sed 's/A/B/g' filename ``` -##### select lines start with string (e.g. bbo) +##### Select lines start with string (e.g. bbo) ```bash sed -n '/^@S/p' ``` -##### delete lines with string (e.g. bbo) +##### Delete lines with string (e.g. bbo) ```bash sed '/bbo/d' filename ``` -##### print every nth lines +##### Print every nth lines ```bash sed -n '0~3p' filename @@ -240,17 +240,17 @@ sed -n '0~3p' filename //catch 0: start; 3: step -##### print every odd # lines +##### Print every odd # lines ```bash sed -n '1~2p' ``` -##### print every third line including the first line +##### Print every third line including the first line ```bash sed -n '1p;0~3p' ``` -##### remove leading whitespace and tabs +##### Remove leading whitespace and tabs ```bash sed -e 's/^[ \t]*//' @@ -258,7 +258,7 @@ sed -e 's/^[ \t]*//' //notice a whitespace before '\t'!! -##### remove only leading whitespace +##### Remove only leading whitespace ```bash sed 's/ *//' @@ -266,12 +266,12 @@ sed 's/ *//' //notice a whitespace before '*'!! -##### remove ending commas +##### Remove ending commas ```bash sed 's/,$//g' ``` -##### add a column to the end +##### Add a column to the end ```bash sed "s/$/\t$i/" @@ -282,39 +282,39 @@ e.g. add the filename to every last column of the file ```bash for i in $(ls);do sed -i "s/$/\t$i/" $i;done ``` -##### add extension of filename to last column +##### Add extension of filename to last column ```bash for i in T000086_1.02.n T000086_1.02.p;do sed "s/$/\t${i/*./}/" $i;done >T000086_1.02.np ``` -##### remove newline\ nextline +##### Remove newline\ nextline ```bash sed ':a;N;$!ba;s/\n//g' ``` -##### print a number of lines (e.g. line 10th to line 33 rd) +##### Print a number of lines (e.g. line 10th to line 33 rd) ```bash sed -n '10,33p' AAA#AAA) +##### Insert character at specified position of file (e.g. AAAAAA --> AAA#AAA) ```bash`` sed -r -e 's/^.{3}/&#/' file ``` @@ -323,85 +323,85 @@ sed -r -e 's/^.{3}/&#/' file ## Awk [[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] -##### set tab as field separator +##### Set tab as field separator ```bash awk -F $'\t' ``` -##### output as tab separated (also as field separator) +##### Output as tab separated (also as field separator) ```bash awk -v OFS='\t' ``` -##### pass variable +##### Pass variable ```bash a=bbo;b=obb; awk -v a="$a" -v b="$b" "$1==a && $10=b' filename ``` -##### print line number and number of characters on each line +##### Print line number and number of characters on each line ```bash awk '{print NR,length($0);}' filename ``` -##### find number of columns +##### Find number of columns ```bash awk '{print NF}' ``` -##### reverse column order +##### Reverse column order ```bash awk '{print $2, $1}' ``` -##### check if there is a comma in a column (e.g. column $1) +##### Check if there is a comma in a column (e.g. column $1) ```bash awk '$1~/,/ {print}' ``` -##### split and do for loop +##### Split and do for loop ```bash awk '{split($2, a,",");for (i in a) print $1"\t"a[i]} filename ``` -##### print all lines before nth occurence of a string (e.g stop print lines when bbo appears 7 times) +##### Print all lines before nth occurence of a string (e.g stop print lines when bbo appears 7 times) ```bash awk -v N=7 '{print}/bbo/&& --N<=0 {exit}' ``` -##### print filename and last line of all files in directory +##### Print filename and last line of all files in directory ```bash ls|xargs -n1 -I file awk '{s=$0};END{print FILENAME,s}' file' ``` -##### add string to the beginning of a column (e.g add "chr" to column $3) +##### Add string to the beginning of a column (e.g add "chr" to column $3) ```bash awk 'BEGIN{OFS="\t"}$3="chr"$3' ``` -##### remove lines with string (e.g. bbo) +##### Remove lines with string (e.g. bbo) ```bash awk '!/bbo/' file ``` -##### column subtraction +##### Column subtraction ```bash cat file| awk -F '\t' 'BEGIN {SUM=0}{SUM+=$3-$2}END{print SUM}' ``` -##### usage and meaning of NR and FNR +##### Usage and meaning of NR and FNR e.g. fileA: a @@ -421,7 +421,7 @@ fileA 3 3 c fileB 4 1 d fileB 5 2 e -##### and gate +##### AND gate e.g. fileA: @@ -445,7 +445,7 @@ awk -v OFS='\t' 'NR=FNR{a[$1]=$2;next} NF {print $1,((a[$1]=$2)? $2:"0")}' fileA 3 0 4 0 -##### round all numbers of file (e.g. 2 significant figure) +##### Round all numbers of file (e.g. 2 significant figure) ```bash awk '{while (match($0, /[0-9]+\[0-9]+/)){ @@ -456,12 +456,12 @@ awk '{while (match($0, /[0-9]+\[0-9]+/)){ \}' ``` -##### give number/index to every row +##### Give number/index to every row ```bash awk '{printf("%s\t%s\n",NR,$0)}' ``` -##### break combine column data into rows +##### Break combine column data into rows e.g. seperate @@ -476,24 +476,24 @@ detail here: http://stackoverflow.com/questions/33408762/bash-turning-single-c awk '{split($2,a,",");for(i in a)print $1"\t"a[i]}' file ``` -##### sum up a file (each line in file contains only one number) +##### Sum up a file (each line in file contains only one number) ```bash awk '{s+=$1} END {print s}' filename ``` -##### average a file (each line in file contains only one number) +##### Average a file (each line in file contains only one number) ```bash awk '{s+=$1}END{print s/NR}' ``` -##### print field start with string (e.g Linux) +##### Print field start with string (e.g Linux) ```bash awk '$1 ~ /^Linux/' ``` -##### sort a row (e.g. 1 40 35 12 23 --> 1 12 23 35 40) +##### Sort a row (e.g. 1 40 35 12 23 --> 1 12 23 35 40) ```bash awk ' {split( $0, a, "\t" ); asort( a ); for( i = 1; i <= length(a); i++ ) printf( "%s\t", a[i] ); printf( "\n" ); }' @@ -503,13 +503,13 @@ awk ' {split( $0, a, "\t" ); asort( a ); for( i = 1; i <= length(a); i++ ) print ## Xargs [[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] -##### set tab as delimiter (default:space) +##### Set tab as delimiter (default:space) ```bash xargs -d\t ``` -##### display 3 items per line +##### Display 3 items per line ```bash echo 1 2 3 4 5 6| xargs -n 3 @@ -519,13 +519,13 @@ echo 1 2 3 4 5 6| xargs -n 3 4 5 6 -##### prompt before execution +##### Prompt before execution ```bash echo a b c |xargs -p -n 3 ``` -##### print command along with output +##### Print command along with output ```bash xargs -t abcd @@ -535,25 +535,25 @@ xargs -t abcd //abcd -##### with find and rm +##### With find and rm ```bash find . -name "*.html"|xargs rm -rf ``` -delete fiels with whitespace in filename (e.g. "hello 2001") +##### Delete fiels with whitespace in filename (e.g. "hello 2001") ```bash find . -name "*.c" -print0|xargs -0 rm -rf ``` -##### show limits +##### Show limits ```bash xargs --show-limits ``` -##### move files to folder +##### Move files to folder ```bash find . -name "*.bak" -print 0|xargs -0 -I {} mv {} ~/old @@ -565,13 +565,13 @@ or find . -name "*.bak" -print 0|xargs -0 -I file mv file ~/old ``` -##### move first 100th files to a directory (e.g. d1) +##### Move first 100th files to a directory (e.g. d1) ```bash ls |head -100|xargs -I {} mv {} d1 ``` -##### parallel +##### Parallel ```bash time echo {1..5} |xargs -n 1 -P 5 sleep @@ -581,7 +581,7 @@ a lot faster than time echo {1..5} |xargs -n1 sleep ``` -##### copy all files from A to B +##### Copy all files from A to B ```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 @@ -591,31 +591,31 @@ find /dir/to/A -type f -name "*.py" -print 0| xargs -0 -r -I file cp -v -p file //p: keep detail (e.g. owner) -##### with sed +##### With sed ```bash ls |xargs -n1 -I file sed -i '/^Pos/d' filename ``` -##### add the file name to the first line of file +##### Add the file name to the first line of file ```bash ls |sed 's/.txt//g'|xargs -n1 -I file sed -i -e '1 i\>file\' file.txt ``` -##### count all files +##### Count all files ```bash ls |xargs -n1 wc -l ``` -##### to filter txt to a single line +##### Turn output into a single line ```bash ls -l| xargs ``` -##### count files within directories +##### Count files within directories ```bash echo mso{1..8}|xargs -n1 bash -c 'echo -n "$1:"; ls -la "$1"| grep -w 74 |wc -l' -- @@ -623,18 +623,18 @@ 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 -##### download dependencies files and install (e.g. requirements.txt) +##### Download dependencies files and install (e.g. requirements.txt) ```bash cat requirements.txt| xargs -n1 sudo pip install ``` -##### count lines in all file, also count total lines +##### Count lines in all file, also count total lines ```bash ls|xargs wc -l ``` -##### xargs and grep +##### Xargs and grep ```bash cat grep_list |xargs -I{} grep {} filename @@ -642,25 +642,25 @@ cat grep_list |xargs -I{} grep {} filename ## Find [[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] -##### list all sub directory/file in the current directory +##### List all sub directory/file in the current directory ```bash find . ``` -##### list all files under the current directory +##### List all files under the current directory ```bash find . -type f ``` -##### list all directories under the current directory +##### List all directories under the current directory ```bash find . -type d ``` -##### edit all files under current directory (e.g. replace 'www' with 'ww') +##### Edit all files under current directory (e.g. replace 'www' with 'ww') ```bash find . name '*.php' -exec sed -i 's/www/w/g' {} \; @@ -673,13 +673,13 @@ replace "www" "w" -- * //a space before * -##### find and output only filename (e.g. "mso") +##### Find and output only filename (e.g. "mso") ```bash find mso*/ -name M* -printf "%f\n" ``` -##### find and delete file with size less than (e.g. 74 byte) +##### Find and delete file with size less than (e.g. 74 byte) ```bash find . -name "*.mso" -size -74c -delete @@ -689,20 +689,20 @@ find . -name "*.mso" -size -74c -delete ## Loops [[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] -##### while loop, column subtraction of a file (e.g. a 3 columns file) +##### While loop, column subtraction of a file (e.g. a 3 columns file) ```bash while read a b c; do echo $(($c-$b));done < <(head filename) ``` //there is a space between the two '<'s -##### while loop, sum up column subtraction +##### While loop, sum up column subtraction ```bash i=0; while read a b c; do ((i+=$c-$b)); echo $i; done < <(head filename) ``` -##### if loop +##### If loop ```bash if (($j==$u+2)) @@ -715,16 +715,16 @@ if [[$age >21]] //[[ ]] use for comparison -##### test if file exist +##### Test if file exist ```bash -if [ -e $DF ] +if [ -e 'filename' ] then echo -e "file exists!" fi ``` -##### for loop +##### For loop ```bash for i in $(ls); do echo file $i;done @@ -733,7 +733,7 @@ for i in $(ls); do echo file $i;done ## Download [[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] -##### download all from a page +##### Download all from a page ```bash wget -r -l1 -H -t1 -nd -N -np -A mp3 -e robots=off http://example.com @@ -748,41 +748,75 @@ wget -r -l1 -H -t1 -nd -N -np -A mp3 -e robots=off http://example.com //-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: +```bash +curl --upload-file ./filename.txt https://transfer.sh/filename.txt +``` +(the above command will return a URL, e.g: https://transfer.sh/tG8rM/filename.txt) +--> download: +```bash +curl https://transfer.sh/tG8rM/filename.txt -o filename.txt +``` + + +##### Download file if necessary + +```bash +data=file.txt +url=http://www.example.com/$data +if [! -s $data];then + echo "downloading test data..." + wget $url +fi +``` + +##### Wget to a filename (when a long name) + +```bash +wget -O filename "http://example.com" +``` + +##### Wget files to a folder + +```bash +wget -P /path/to/directory "http://example.com" +``` ## Random [[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] -##### random pick 100 lines from a file +##### Random pick 100 lines from a file ```bash shuf -n 100 filename ``` -##### random order (lucky draw) +##### Random order (lucky draw) ```bash for i in a b c d e; do echo $i; done| shuf ``` -##### echo series of random numbers between a range (e.g. generate 15 random numbers from 0-10) +##### Echo series of random numbers between a range (e.g. generate 15 random numbers from 0-10) ```bash shuf -i 0-10 -n 15 ``` -##### echo a random number +##### Echo a random number ```bash echo $RANDOM ``` -##### random from 0-9 +##### Random from 0-9 ```bash echo $((RANDOM % 10)) ``` -##### random from 1-10 +##### Random from 1-10 ```bash echo $(((RANDOM %10)+1)) @@ -792,7 +826,7 @@ echo $(((RANDOM %10)+1)) X11 GUI applications! Here are some GUI tools for you if you get bored by the text-only environment. -##### enable X11 forwarding,in order to use graphical application on servers +##### Enable X11 forwarding,in order to use graphical application on servers ```bash ssh -X user_name@ip_address ``` @@ -803,14 +837,14 @@ xorg-x11-xauth xorg-x11-fonts-* xorg-x11-utils -##### little xwindow tools +##### Little xwindow tools ```bash xclock xeyes ``` -##### open pictures/images from ssh server +##### Open pictures/images from ssh server ```bash 1. ssh -X user_name@ip_address 2. apt-get install eog @@ -823,13 +857,13 @@ xeyes 2. apt-get install gedit 3. gedit filename.txt ``` -##### open PDF file from ssh server +##### Open PDF file from ssh server ```bash 1. ssh -X user_name@ip_address 2. apt-get install evince 3. evince filename.pdf ``` -##### use google-chrome browser from ssh server +##### Use google-chrome browser from ssh server ```bash 1. ssh -X user_name@ip_address 2. apt-get install libxss1 libappindicator1 libindicator7 @@ -847,25 +881,25 @@ xeyes ## Others [[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] -##### remove newline / nextline +##### Remove newline / nextline ```bash tr --delete '\n' output.txt ``` -##### replace newline +##### Replace newline ```bash tr '\n' ' ' log & @@ -945,7 +979,7 @@ some_commands 2>&1 >>outfile //0: standard input; 1: standard output; 2: standard error -##### send mail +##### Send mail ```bash echo 'heres the content'| mail -A 'file.txt' -s 'mail.subject' me@gmail.com @@ -958,25 +992,25 @@ echo 'heres the content'| mail -A 'file.txt' -s 'mail.subject' me@gmail.com ```bash xls2csv filename ``` -##### append to file (e.g. hihi) +##### Append to file (e.g. hihi) ```bash echo 'hihi' >>filename ``` -##### make BEEP sound +##### Make BEEP sound ```bash speaker-test -t sine -f 1000 -l1 ``` -##### set beep duration +##### Set beep duration ```bash (speaker-test -t sine -f 1000) & pid=$!;sleep 0.1s;kill -9 $pid ``` -##### history edit/ delete +##### History edit/ delete ```bash ~/.bash_history @@ -987,13 +1021,13 @@ or history -d [line_number] ``` -##### get last history/record filename +##### Get last history/record filename ```bash head !$ ``` -##### clean screen +##### Clean screen ```bash clear @@ -1005,26 +1039,26 @@ or Ctrl+l ``` -##### send data to last edited file +##### Send data to last edited file ```bash cat /directory/to/file echo 100>!$ ``` -##### run history number (e.g. 53) +##### Run history number (e.g. 53) ```bash !53 ``` -##### run last command +##### Run last command ```bash !! ``` -##### run last command that began with (e.g. cat filename) +##### Run last command that began with (e.g. cat filename) ```bash !cat @@ -1038,42 +1072,18 @@ or //run cat filename again -##### extract .xf +##### Extract .xf 1.unxz filename.tar.xz 2.tar -xf filename.tar -##### install python package +##### Install python package ```bash pip install packagename ``` - -##### Download file if necessary - -```bash -data=file.txt -url=http://www.example.com/$data -if [! -s $data];then - echo "downloading test data..." - wget $url -fi -``` - -##### wget to a filename (when a long name) - -```bash -wget -O filename "http://example.com" -``` - -##### wget files to a folder - -```bash -wget -P /path/to/directory "http://example.com" -``` - -##### delete current bash command +##### Delete current bash command ```bash Ctrl+U @@ -1093,7 +1103,7 @@ Alt+Shift+# //to make it to history -##### add things to history (e.g. "addmetohistory") +##### Add something to history (e.g. "addmetohistory") ```bash #addmetodistory @@ -1101,19 +1111,19 @@ Alt+Shift+# //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 ```bash sleep 5;echo hi ``` -##### count the time for executing a command +##### Count the time for executing a command ```bash time echo hi ``` -##### backup with rsync +##### Backup with rsync ```bash rsync -av filename filename.bak @@ -1124,7 +1134,7 @@ rsync -av --update directory directory.bak //skip files that are newer on receiver (i prefer this one!) -##### make all directories at one time! +##### Make all directories at one time! ```bash mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat} @@ -1133,25 +1143,25 @@ mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat} //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) ```bash cd tmp/ && tar xvf ~/a.tar ``` -##### run command only if another command returns non-zero exit status (not finish) +##### Run command only if another command returns non-zero exit status (not finish) ```bash cd tmp/a/b/c ||mkdir -p tmp/a/b/c ``` -##### extract to a path +##### Extract to a path ```bash tar xvf -C /path/to/directory filename.gz ``` -##### use backslash "\" to break long command +##### Use backslash "\" to break long command ```bash cd tmp/a/b/c \ @@ -1159,14 +1169,14 @@ cd tmp/a/b/c \ >mkdir -p tmp/a/b/c ``` -##### get pwd +##### Get pwd ```bash VAR=$PWD; cd ~; tar xvf -C $VAR file.tar ``` //PWD need to be capital letter -##### list file type of file (e.g. /tmp/) +##### List file type of file (e.g. /tmp/) ```bash file /tmp/ @@ -1174,7 +1184,7 @@ file /tmp/ //tmp/: directory -##### bash script +##### Bash script ```bash #!/bin/bash @@ -1187,19 +1197,19 @@ file=${1%.*} ``` //remove string after a "." -##### search from history +##### Search from history ```bash Ctrl+r ``` -##### python simple HTTP Server +##### Python simple HTTP Server ```bash python -m SimpleHTTPServer ``` -##### variables +##### Variables ```bash {i/a/,} @@ -1210,39 +1220,39 @@ e.g. replace all ``` //for variable i, replace all 'a' with a comma -##### read user input +##### Read user input ```bash read input echo $input ``` -##### generate sequence 1-10 +##### Generate sequence 1-10 ```bash seq 10 ``` -##### sum up input list (e.g. seq 10) +##### Sum up input list (e.g. seq 10) ```bash seq 10|paste -sd+|bc ``` -##### find average of input list/file +##### Find average of input list/file ```bash i=`wc -l filename|cut -d ' ' -f1`; cat filename| echo "scale=2;(`paste -sd+`)/"$i|bc ``` -##### generate all combination (e.g. 1,2) +##### Generate all combination (e.g. 1,2) ```bash echo {1,2}{1,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 set = {A,T,C,G} @@ -1252,76 +1262,76 @@ for ((i=0; i<$group; i++));do bash -c "echo "$repetition"" ``` -##### read file content to variable +##### Read file content to variable ```bash foo=$( $(basename filename.gz .gz).unpacked ``` -##### use the squeeze repeat option (e.g. /t/t --> /t) +##### Use the squeeze repeat option (e.g. /t/t --> /t) ```bash tr -s "/t" < filename ``` -##### do not print nextline with echo +##### Do not print nextline with echo ```bash echo -e 'text here \c' ``` -##### use the last argument +##### Use the last argument ```bash !$ ``` -##### check last exit code +##### Check last exit code ```bash echo $? ``` -##### view first 50 characters of file +##### View first 50 characters of file ```bash head -c 50 file ``` -##### group/combine rows into one row +##### Group/combine rows into one row e.g. AAAA @@ -1338,21 +1348,21 @@ cat filename|paste - - - - AAAABBBBCCCCDDDD ``` -##### fastq to fasta +##### Fastq to fasta ```bash cat file.fastq | paste - - - - | sed 's/^@/>/g'| cut -f1-2 | tr '\t' '\n' >file.fa ``` -##### cut and get last column +##### Cut and get last column ```bash cat file|rev | cut -d/ -f1 | rev ``` -##### add one to variable/increment a numeric variable (e.g. $var) +##### Add one to variable/increment a numeric variable (e.g. $var) ```bash ((var++)) ``` -##### some handy environment variables +##### Some handy environment variables $0 :name of shell or shell script. $1, $2, $3, ... :positional parameters. $# :number of positional parameters. @@ -1361,39 +1371,28 @@ $- :current options set for the shell. $$ :pid of the current shell (not subshell). $! :is the PID of the most recent background command. -##### clear the contents of a file (e.g. filename) +##### Clear the contents of a file (e.g. filename) ```bash >filename ``` -##### Upload a file to web and download (https://transfer.sh/) ---> upload: -```bash -curl --upload-file ./filename.txt https://transfer.sh/filename.txt -``` -(the above command will return a URL, e.g: https://transfer.sh/tG8rM/filename.txt) ---> download: -```bash -curl https://transfer.sh/tG8rM/filename.txt -o filename.txt -``` - ## System [[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] -##### snapshot of the current processes +##### Snapshot of the current processes ```bash ps ``` -##### check graphics card +##### Check graphics card ```bash lspci ``` -##### show IP address +##### Show IP address ```bash $ip add show @@ -1404,7 +1403,7 @@ or ifconfig ``` -##### check system version +##### Check system version ```bash cat /etc/*-release @@ -1416,57 +1415,57 @@ cat /etc/*-release man hier ``` -##### list job +##### List job ```bash jobs -l ``` -##### export PATH +##### Export PATH ```bash export PATH=$PATH:~/path/you/want ``` -##### make file execuable +##### Make file execuable ```bash chmod +x filename ``` //you can now ./filename to execute it -##### list screen +##### List screen ```bash screen -d -r ``` -##### echo screen name +##### Echo screen name ```bash screen -ls ``` -##### check system (x86-64) +##### Check system (x86-64) ```bash uname -i ``` -##### surf the net +##### Surf the net ```bash links www.google.com ``` -##### add user, set passwd +##### Add user, set passwd ```bash useradd username passwd username ``` -##### edit variable for bash, (e.g. displaying the whole path) +##### Edit variable for bash, (e.g. displaying the whole path) ```bash 1. joe ~/.bash_profile @@ -1477,7 +1476,7 @@ passwd username 3. source ~/.bash_profile ``` -##### edit environment setting (e.g. alias) +##### Edit environment setting (e.g. alias) ```bash 1. joe ~/.bash_profile @@ -1485,63 +1484,63 @@ passwd username 3. source ~/.bash_profile ``` -##### list environment variables (e.g. PATH) +##### List environment variables (e.g. PATH) ```bash $echo $PATH ``` //list of directories separated by a colon -##### list all environment variables for current user +##### List all environment variables for current user ```bash $env ``` -##### show partition format +##### Show partition format ```bash lsblk ``` -##### soft link program to bin +##### Soft link program to bin ```bash ln -s /path/to/program /home/usr/bin ``` //must be the whole path to the program -##### show hexadecimal view of data +##### Show hexadecimal view of data ```bash hexdump -C filename.class ``` -##### jump to different node +##### Jump to different node ```bash rsh node_name ``` -##### check port (active internet connection) +##### Check port (active internet connection) ```bash netstat -tulpn ``` -##### find whick link to a file +##### Find whick link to a file ```bash readlink filename ``` -##### check where a command link to (e.g. python) +##### Check where a command link to (e.g. python) ```bash which python ``` -##### list total size of a directory +##### List total size of a directory ```bash du -hs . @@ -1552,19 +1551,19 @@ or du -sb ``` -##### copy directory with permission setting +##### Copy directory with permission setting ```bash cp -rp /path/to/directory ``` -##### store current directory +##### Store current directory ```bash pushd . $popd ;dirs -l ``` -##### show disk usage +##### Show disk usage ```bash df -h @@ -1580,13 +1579,13 @@ or du -sk /var/log/* |sort -rn |head -10 ``` -##### show current runlevel +##### Show current runlevel ```bash runlevel ``` -##### switch runlevel +##### Switch runlevel ```bash init 3 @@ -1595,32 +1594,32 @@ or ```bash telinit 3 ``` -##### permanently modify runlevel +##### Permanently modify runlevel ```bash 1. edit /etc/init/rc-sysinit.conf 2. env DEFAULT_RUNLEVEL=2 ``` -##### become root +##### Become root ```bash su ``` -##### become somebody +##### Become somebody ```bash su somebody ``` -##### report user quotes on device +##### Report user quotes on device ```bash requota -auvs ``` -##### get entries in a number of important databases +##### Get entries in a number of important databases ```bash getent database_name @@ -1638,7 +1637,7 @@ getent group ``` //store in database 'group' -##### change owner of file +##### Change owner of file ```bash chown user_name filename @@ -1646,48 +1645,48 @@ chown -R user_name /path/to/directory/ ``` //chown user:group filename -##### list current mount detail +##### List current mount detail ```bash df ``` -##### list current usernames and user-numbers +##### List current usernames and user-numbers ```bash cat /etc/passwd ``` -##### get all username +##### Get all username ```bash getent passwd| awk '{FS="[:]"; print $1}' ``` -##### show all users +##### Show all users ```bash compgen -u ``` -##### show all groups +##### Show all groups ```bash compgen -g ``` -##### show group of user +##### Show group of user ```bash group username ``` -##### show uid, gid, group of user +##### Show uid, gid, group of user ```bash id username ``` -##### check if it's root +##### Check if it's root ```bash if [$(id -u) -ne 0];then @@ -1697,7 +1696,7 @@ fi ``` //'id -u' output 0 if it's not root -##### find out CPU information +##### Find out CPU information ```bash more /proc/cpuinfo @@ -1708,100 +1707,100 @@ or lscpu ``` -##### set quota for user (e.g. disk soft limit: 120586240; hard limit: 125829120) +##### Set quota for user (e.g. disk soft limit: 120586240; hard limit: 125829120) ```bash setquota username 120586240 125829120 0 0 /home ``` -##### show quota for user +##### Show quota for user ```bash quota -v username ``` -##### fork bomb +##### Fork bomb ```bash :(){:|:&};: ``` //dont try this at home -##### check user login +##### Check user login ```bash lastlog ``` -##### edit path for all users +##### Edit path for all users ```bash joe /etc/environment ``` //edit this file -##### show running processes +##### Show running processes ```bash ps aux ``` -##### find maximum number of processes +##### Find maximum number of processes ```bash cat /proc/sys/kernal/pid_max ``` -##### show and set user limit +##### Show and set user limit ```bash ulimit -u ``` -##### which ports are listening for TCP connections from the network +##### Which ports are listening for TCP connections from the network ```bash nmap -sT -O localhost ``` -##### print out number of cores/ processors +##### Print out number of cores/ processors ```bash nproc --all ``` -##### check status of each core +##### Check status of each core 1. top 2. press '1' -##### show jobs and PID +##### Show jobs and PID ```bash jobs -l ``` -##### list all running services +##### List all running services ```bash service --status-all ``` -##### schedule shutdown server +##### Schedule shutdown server ```bash shutdown -r +5 "Server will restart in 5 minutes. Please save your work." ``` -##### cancel scheduled shutdown +##### Cancel scheduled shutdown ```bash shutdown -c ``` -##### boardcast to all users +##### Boardcast to all users ```bash wall -n hihi ``` -##### kill all process of a user +##### Kill all process of a user ```bash pkill -U user_name ``` -##### set gedit preference on server +##### Set gedit preference on server -->you might have to install the following: