diff --git a/index.md b/index.md index 56b6130..91f963a 100644 --- a/index.md +++ b/index.md @@ -1,1422 +1,1768 @@ -
Handy Bash oneliner commands for tsv file editing - - - -grep -o -P '(?<=w1).*(?=w2)'
grep -v bbo filename
grep -m 1 bbo filename
grep -c bbo filename
grep -i "bbo" filename
grep -o bbo filename
grep --color bbo filename
grep -R bbo /path/to/directory
or
- -grep -r bbo /path/to/directory
grep -Rh bbo /path/to/directory
or
- -grep -rh bbo /path/to/directory
grep 'A\|B\|C\|D'
-
-
-grep 'A.*B'
grep -f fileA fileB
grep $'\t'
$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 []!
- -grep -oP '\(\K[^\)]+'
grep -o -w "\w\{10\}\-R\w\{1\}"
// \w word character [0-9a-zA-Z_] \W not word character
- -http://www.cyberciti.biz/faq/grep-regular-expressions/
- -sed "/bbo/d" filename
sed -i "/bbo/d" filename
e.g. add >$i to the first line (to make a 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
- -sed '/^\s*$/d'
or
- -sed 's/^$/d'
sed '$d'
sed -i '$ s/.$//' filename
sed '$s/$/]/' filename
sed -e 's/$/\}\]/' filename
sed 's/.\{4\}/&\n/g'
sed -s '$a,' *.json > all.json
sed 's/A/B/g' filename
sed -n '/^@S/p'
sed '/bbo/d' filename
sed -n '0~3p' filename
//catch 0: start; 3: step
- -sed -n '1~2p'
sed -n '1p;0~3p'
sed -e 's/^[ \t]*//'
//notice a whitespace before '\t'!!
- -sed 's/ *//'
//notice a whitespace before '*'!!
- -sed 's/,$//g'
sed "s/$/\t$i/"
//$i is the valuable you want to add -e.g. add the filename to every last column of the file
- -for i in $(ls);do sed -i "s/$/\t$i/" $i;done
for i in T000086_1.02.n T000086_1.02.p;do sed "s/$/\t${i/*./}/" $i;done >T000086_1.02.np
sed ':a;N;$!ba;s/\n//g'
sed -n '10,33p' <filename
sed 's=/=\\/=g'
awk -F $'\t'
awk -v OFS='\t'
a=bbo;b=obb;
-awk -v a="$a" -v b="$b" "$1==a && $10=b' filename
awk '{print length ($0);}' filename
awk '{print NF}'
awk '{print $2, $1}'
awk '$1~/,/ {print}'
awk '{split($2, a,",");for (i in a) print $1"\t"a[i]} filename
awk -v N=7 '{print}/bbo/&& --N<=0 {exit}'
ls|xargs -n1 -I file awk '{s=$0};END{print FILENAME,s}' file'
awk 'BEGIN{OFS="\t"}$3="chr"$3'
awk '!/bbo/' file
cat file| awk -F '\t' 'BEGIN {SUM=0}{SUM+=$3-$2}END{print SUM}'
e.g.
+##Handy Bash oneliner commands for tsv file editing
+
+- [Grep](#grep)
+- [Sed](#sed)
+- [Awk](#awk)
+- [Xargs](#xargs)
+- [Find](#find)
+- [Loops](#loops)
+- [Download](#download)
+- [Random](#random)
+- [Others](#others)
+- [System](#system)
+
+##Grep
+#####extract text bewteen words (e.g. w1,w2)
+
+```bash
+grep -o -P '(?<=w1).*(?=w2)'
+```
+
+#####grep lines without word (e.g. bbo)
+
+```bash
+grep -v bbo filename
+```
+
+#####grep only one/first match (e.g. bbo)
+
+```bash
+grep -m 1 bbo filename
+```
+
+#####grep and count (e.g. bbo)
+
+```bash
+grep -c bbo filename
+```
+
+#####insensitive grep (e.g. bbo/BBO/Bbo)
+
+```bash
+grep -i "bbo" filename
+```
+
+#####count occurrence (e.g. three times a line count three times)
+
+```bash
+grep -o bbo filename
+```
+
+#####COLOR the match (e.g. bbo)!
+
+```bash
+grep --color bbo filename
+```
+
+#####grep search all files in a directory(e.g. bbo)
+
+```bash
+grep -R bbo /path/to/directory
+```
+
+or
+
+```bash
+grep -r bbo /path/to/directory
+```
+#####search all files in directory, only output file names with matches(e.g. bbo)
+
+```bash
+grep -Rh bbo /path/to/directory
+```
+or
+```bash
+grep -rh bbo /path/to/directory
+```
+
+#####grep OR (e.g. A or B or C or D)
+
+```
+grep 'A\|B\|C\|D'
+```
+
+#####grep AND (e.g. A and B)
+
+```bash
+grep 'A.*B'
+```
+
+#####grep all content of a fileA from fileB
+
+```bash
+grep -f fileA fileB
+```
+
+#####grep a tab
+
+```bash
+grep $'\t'
+```
+
+#####grep variable from variable
+```bash
+$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 []!
+
+#####grep strings between a bracket()
+```bash
+grep -oP '\(\K[^\)]+'
+```
+
+#####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
+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)
+
+```bash
+sed "/bbo/d" filename
+```
+
+#####edit infile (edit and save)
+
+```bash
+sed -i "/bbo/d" filename
+```
+#####when using variable (e.g. $i), use double quotes " "
+e.g. add >$i to the first line (to make a FASTA file)
+
+```bash
+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
+
+
+#####delete empty lines
+
+```bash
+sed '/^\s*$/d'
+```
+or
+
+```bash
+sed 's/^$/d'
+```
+#####delete last line
+
+```bash
+sed '$d'
+```
+
+#####delete last character from end of file
+```bash
+sed -i '$ s/.$//' filename
+```
+
+#####add string to end of file (e.g. "]")
+
+```bash
+sed '$s/$/]/' filename
+```
+
+#####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. "}")
+```bash
+sed -e 's/$/\}\]/' filename
+```
+
+#####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 ",")
+
+```bash
+sed -s '$a,' *.json > all.json
+```
+
+#####substitution (e.g. replace A by B)
+
+```bash
+sed 's/A/B/g' filename
+```
+#####select lines start with string (e.g. bbo)
+
+```bash
+sed -n '/^@S/p'
+```
+#####delete lines with string (e.g. bbo)
+
+```bash
+sed '/bbo/d' filename
+```
+#####print every nth lines
+
+```bash
+sed -n '0~3p' filename
+```
+//catch 0: start; 3: step
+
+
+#####print every odd # lines
+
+```bash
+sed -n '1~2p'
+```
+#####print every third line including the first line
+
+```bash
+sed -n '1p;0~3p'
+```
+#####remove leading whitespace and tabs
+
+```bash
+sed -e 's/^[ \t]*//'
+```
+//notice a whitespace before '\t'!!
+
+
+#####remove only leading whitespace
+
+```bash
+sed 's/ *//'
+```
+//notice a whitespace before '*'!!
+
+
+#####remove ending commas
+
+```bash
+sed 's/,$//g'
+```
+#####add a column to the end
+
+```bash
+sed "s/$/\t$i/"
+```
+//$i is the valuable you want to add
+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
+
+```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
+
+```bash
+sed ':a;N;$!ba;s/\n//g'
+```
+
+#####print a number of lines (e.g. line 10th to line 33 rd)
+
+```bash
+sed -n '10,33p' fileA 1 1 a
+fileA 1 1 a
fileA 2 2 b
fileA 3 3 c
fileB 4 1 d
-fileB 5 2 e e.g.
+e.g.
fileA:
-1 0 2 1 3 1 4 0 fileB: 1 0 2 1 3 0 4 1 1 0 2 1 3 0 4 0 e.g.
-seperate David cat,dog into David cat David dog detail here: http://stackoverflow.com/questions/33408762/bash-turning-single-comma-separated-column-into-multi-line-string //1 2 3
- 4 5 6 ///bin/echo abcd
-//abcd delete fiels with whitespace in filename (e.g. "hello 2001") or a lot faster than //v: verbose|
-//p: keep detail (e.g. owner) // "--" signals the end of options and display further option processing if no subdirectory //a space before * //M for MB, etc //there is a space between the two '<'s //(( )) use for arithmetic operation //[[ ]] use for comparison //-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 //a: added; d:delete; c:changed or //side-to-side merge of file differences or //add leading zeros //default tab seperated or or or //0: standard input; 1: standard output; 2: standard error //use -a flag to set send from (-a "From: some@mail.tld") or or or //run cat filename again or or //to make it to history //just add a "#" before~~ //skip files that are newer on receiver (i prefer this one!) //-p: make parent directory
-//this will create project/doc/html/; project/doc/info; project/lib/ext ,etc //PWD need to be capital letter //tmp/: directory //remove string before a "." //remove string after a "." e.g. replace all //for variable i, replace all 'a' with a comma //1 1, 1 2, 2 1, 2 2 or //you can now ./filename to execute it //$PS1 is a variable that defines the makeup and style of the command prompt //list of directories separated by a colon //must be the whole path to the program or or or or (e.g. the 'passwd' database) //list all user account (all local and LDAP)
-(e.g. fetch list of grop accounts) //store in database 'group' //chown user:group filename //'id -u' output 0 if it's not root or //dont try this at home //edit this file =-=-=-=-=-A lot more coming!! =-=-=-=-=-=-=-=-=-=waitwait-=-=-=-=-=-=-=-=-=-awk 'print FILENAME, NR,FNR,$0}' fileA fileB
-
+#####and gate
- and gateawk -v OFS='\t' 'NR=FNR{a[$1]=$2;next} NF {print $1,((a[$1]=$2)? $2:"0")}' fileA fileB
-
+#####give number/index to every row
+
+```bash
+awk '{printf("%s\t%s\n",NR,$0)}'
+```
+#####break combine column data into rows
- round all numbers of file (e.g. 2 significant figure)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
- \}'
-
+David cat,dog
- give number/index to every rowawk '{printf("%s\t%s\n",NR,$0)}'
-
+David cat
- break combine column data into rowsawk '{split($2,a,",");for(i in a)print $1"\t"a[i]}' file
-
+#####sort a row (e.g. 1 40 35 12 23 --> 1 12 23 35 40)
- sum up a file (each line in file contains only one number)awk '{s+=$1} END {print s}' filename
-
- average a file (each line in file contains only one number)awk '{s+=$1}END{print s/NR}'
-
+#####set tab as delimiter (default:space)
- print field start with string (e.g Linux)awk '$1 ~ /^Linux/'
-
+#####display 3 items per line
- sort a row (e.g. 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" ); }'
-
+//1 2 3
+ 4 5 6
- Xargs
-
-
+#####prompt before execution
- set tab as delimiter (default:space)xargs -d\t
-
+#####print command along with output
- display 3 items per lineecho 1 2 3 4 5 6| xargs -n 3
-
- prompt before executionecho a b c |xargs -p -n 3
-
+```bash
+find . -name "*.html"|xargs rm -rf
+```
- print command along with outputxargs -t abcd
-
+#####show limits
- with find and rmfind . -name "*.html"|xargs rm -rf
find . -name "*.c" -print0|xargs -0 rm -rf
-
+or
- show limitsxargs --show-limits
-
+#####move first 100th files to a directory (e.g. d1)
- move files to folderfind . -name "*.bak" -print 0|xargs -0 -I {} mv {} ~/old
find . -name "*.bak" -print 0|xargs -0 -I file mv file ~/old
-
+#####copy all files from A to B
- move first 100th files to a directory (e.g. d1)ls |head -100|xargs -I {} mv {} d1
-
+//v: verbose|
+//p: keep detail (e.g. owner)
- paralleltime echo {1..5} |xargs -n 1 -P 5 sleep
time echo {1..5} |xargs -n1 sleep
-
+#####add the file name to the first line of file
- copy all files from A to Bfind /dir/to/A -type f -name "*.py" -print 0| xargs -0 -r -I file cp -v -p file --target-directory=/path/to/B
-
+```bash
+ls |xargs -n1 wc -l
+```
- with sedls |xargs -n1 -I file sed -i '/^Pos/d' filename
-
+```bash
+ls -l| xargs
+```
- add the file name to the first line of filels |sed 's/.txt//g'|xargs -n1 -I file sed -i -e '1 i\>file\' file.txt
-
+```bash
+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
- count all filesls |xargs -n1 wc -l
-
+#####download dependencies files and install (e.g. requirements.txt)
+
+```bash
+cat requirements.txt| xargs -n1 sudo pip install
+```
- to filter txt to a single linels -l| xargs
-
+```bash
+ls|xargs wc -l
+```
+#####xargs and grep
+
+```bash
+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
+
+```bash
+find .
+```
- count files within directoriesecho mso{1..8}|xargs -n1 bash -c 'echo -n "$1:"; ls -la "$1"| grep -w 74 |wc -l' --
-
+#####edit all files under current directory (e.g. replace 'www' with 'ww')
+
+```bash
+find . name '*.php' -exec sed -i 's/www/w/g' {} \;
+```
+if no subdirectory
+
+```bash
+replace "www" "w" -- *
+```
+//a space before *
- download dependencies files and install (e.g. requirements.txt)cat requirements.txt| xargs -n1 sudo pip install
-
+#####find and output only filename (e.g. "mso")
+
+```bash
+find mso*/ -name M* -printf "%f\n"
+```
- count lines in all file, also count total linesls|xargs wc -l
-
- Find
-
-
+##Others
+[[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)]
+#####remove newline / nextline
+
+```bash
+tr --delete '\n' list all sub directory/file in the current directoryfind .
+or
+
+```bash
+sdiff fileA fileB
+```
+//side-to-side merge of file differences
+
+
+#####number a file (e.g. fileA)
+
+```bash
+nl fileA
+```
+or
+
+```bash
+nl -nrz fileA
+```
+//add leading zeros
-
-
- list all files under the current directoryfind . -type f
-
-
- list all directories under the current directoryfind . -type d
-
-
- edit all files under current directory (e.g. replace 'www' with 'ww')find . name '*.php' -exec sed -i 's/www/w/g' {} \;
replace "www" "w" -- *
-
-
- find and output only filename (e.g. "mso")find mso*/ -name M* -printf "%f\n"
-
-
- find and delete file with size less than (e.g. 74 byte)find . -name "*.mso" -size -74c -delete
-
-
- Loops
-
-
-
-
- while loop, column subtraction of a file (e.g. a 3 columns file)while read a b c; do echo $(($c-$b));done < <(head filename)
-
-
- while loop, sum up column subtractioni=0; while read a b c; do ((i+=$c-$b)); echo $i; done < <(head filename)
-
-
- if loopif (($j==$u+2))
if [[$age >21]]
-
-
- for loopfor i in $(ls); do echo file $i;done
-
-
- Download
-
-
-
-
- download all from a pagewget -r -l1 -H -t1 -nd -N -np -A mp3 -e robots=off http://example.com
-
-
- Random
-
-
-
-
- random pick 100 lines from a fileshuf -n 100 filename
-
-
- random order (lucky draw)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)shuf -i 0-10 -n 15
-
-
- echo a random numberecho $RANDOM
-
-
- random from 0-9echo $((RANDOM % 10))
-
-
- random from 1-10echo $(((RANDOM %10)+1))
-
-
- Others
-
-
-
-
- remove newline / nextlinetr --delete '\n' <input.txt >output.txt
-
-
- replace newlinetr '\n' ' ' <filename
-
-
- compare files (e.g. fileA, fileB)diff fileA fileB
sdiff fileA fileB
-
-
- number a file (e.g. fileA)nl fileA
nl -nrz fileA
-
-
- combine/ paste two files (e.g. fileA, fileB)paste fileA fileB
-
-
- reverse stringecho 12345| rev
-
-
- read .gz file without extractingzmore filename
zless filename
-
-
- run in background, output error file(command here) 2>log &
(command here) 2>&1| tee logfile
(command here) 2>&1 >>outfile
-
-
- send mailecho 'heres the content'| mail -A 'file.txt' -s 'mail.subject' me@gmail.com
-
-
- .xls to csvxls2csv filename
-
-
- append to file (e.g. hihi)echo 'hihi' >>filename
-
-
- make BEEP soundspeaker-test -t sine -f 1000 -l1
-
-
- set beep duration(speaker-test -t sine -f 1000) & pid=$!;sleep 0.1s;kill -9 $pid
-
-
- history edit/ delete~/.bash_history
history -d [line_number]
-
-
- get last history/record filenamehead !$
-
-
- clean screenclear
Ctrl+l
-
-
- send data to last edited filecat /directory/to/file
-echo 100>!$
-
-
- run history number (e.g. 53)!53
-
-
- run last command!!
-
-
- run last command that began with (e.g. cat filename)!cat
!c
-
-
- extract .xf
-
-1.unxz filename.tar.xz
-2.tar -xf filename.tar
-
-
-
- install python packagepip install packagename
-
-
- Download file if necessarydata=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)wget -O filename "http://example.com"
-
-
- wget files to a folderwget -P /path/to/directory "http://example.com"
-
-
- delete current bash commandCtrl+U
Ctrl+C
Alt+Shift+#
-
-
- add things to history (e.g. "addmetohistory")#addmetodistory
-
-
- sleep awhile or wait for a moment or schedule a jobsleep 5;echo hi
-
-
- count the time for executing a commandtime echo hi
-
-
- backup with rsyncrsync -av filename filename.bak
+#####combine/ paste two files (e.g. fileA, fileB)
+
+```bash
+paste fileA fileB
+```
+//default tab seperated
+
+
+#####reverse string
+
+```bash
+echo 12345| rev
+```
+
+#####read .gz file without extracting
+
+```bash
+zmore filename
+```
+or
+
+```bash
+zless filename
+```
+
+#####run in background, output error file
+
+```bash
+some_commands &>log &
+```
+
+or
+
+
+```bash
+some_commands 2>log &
+```
+
+or
+
+```bash
+some_commands 2>&1| tee logfile
+```
+
+or
+
+```bash
+some_commands 2>&1 >>outfile
+```
+//0: standard input; 1: standard output; 2: standard error
+
+
+#####send mail
+
+```bash
+echo 'heres the content'| mail -A 'file.txt' -s 'mail.subject' me@gmail.com
+```
+//use -a flag to set send from (-a "From: some@mail.tld")
+
+
+#####.xls to csv
+
+```bash
+xls2csv filename
+```
+#####append to file (e.g. hihi)
+
+```bash
+echo 'hihi' >>filename
+```
+
+#####make BEEP sound
+
+```bash
+speaker-test -t sine -f 1000 -l1
+```
+
+#####set beep duration
+
+```bash
+(speaker-test -t sine -f 1000) & pid=$!;sleep 0.1s;kill -9 $pid
+```
+
+#####history edit/ delete
+
+```bash
+~/.bash_history
+```
+or
+
+```bash
+history -d [line_number]
+```
+
+#####get last history/record filename
+
+```bash
+head !$
+```
+
+#####clean screen
+
+```bash
+clear
+```
+
+or
+
+```bash
+Ctrl+l
+```
+
+#####send data to last edited file
+
+```bash
+cat /directory/to/file
+echo 100>!$
+```
+
+#####run history number (e.g. 53)
+
+```bash
+!53
+```
+
+#####run last command
+
+```bash
+!!
+```
+
+#####run last command that began with (e.g. cat filename)
+
+```bash
+!cat
+```
+
+or
+
+```bash
+!c
+```
+//run cat filename again
+
+
+#####extract .xf
+
+ 1.unxz filename.tar.xz
+ 2.tar -xf filename.tar
+
+#####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
+
+```bash
+Ctrl+U
+```
+
+or
+
+```bash
+Ctrl+C
+```
+
+or
+
+```bash
+Alt+Shift+#
+```
+//to make it to history
+
+
+#####add things to history (e.g. "addmetohistory")
+
+```bash
+#addmetodistory
+```
+//just add a "#" before~~
+
+
+#####sleep awhile or wait for a moment or schedule a job
+
+```bash
+sleep 5;echo hi
+```
+
+#####count the time for executing a command
+
+```bash
+time echo hi
+```
+
+#####backup with rsync
+
+```bash
+rsync -av filename filename.bak
rsync -av directory directory.bak
rsync -av --ignore_existing directory/ directory.bak
-rsync -av --update directory directory.bak
-
+#####make all directories at one time!
+
+```bash
+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
- make all directories at one time!mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat}
-
+#####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
+```
- run command only if another command returns zero exit status (well done)cd tmp/ && tar xvf ~/a.tar
-
+#####use backslash "\" to break long command
+
+```bash
+cd tmp/a/b/c \
+> || \
+>mkdir -p tmp/a/b/c
+```
- run command only if another command returns non-zero exit status (not finish)cd tmp/a/b/c ||mkdir -p tmp/a/b/c
-
+#####list file type of file (e.g. /tmp/)
+
+```bash
+file /tmp/
+```
+//tmp/: directory
- extract to a pathtar xvf -C /path/to/directory filename.gz
-
+#####bash script
+
+```bash
+#!/bin/bash
+file=${1#*.}
+```
+//remove string before a "."
- use backslash "\" to break long commandcd tmp/a/b/c \
-> || \
->mkdir -p tmp/a/b/c
-
+#####search from history
+
+```bash
+Ctrl+r
+```
- get pwdVAR=$PWD; cd ~; tar xvf -C $VAR file.tar
-
+#####read user input
+
+```bash
+read input
+echo $input
+```
- list file type of file (e.g. /tmp/)file /tmp/
-
+#####find average of input list/file
+
+```bash
+i=`wc -l filename|cut -d ' ' -f1`; cat filename| echo "scale=2;(`paste -sd+`)/"$i|bc
+```
- bash script#!/bin/bash
-file=${1#*.}
file=${1%.*}
-
-
- search from historyCtrl+r
-
-
- python simple HTTP Serverpython -m SimpleHTTPServer
-
-
- variables{i/a/,}
{i//a/,}
-
-
- read user inputread input
-echo $input
-
-
- generate sequence 1-10seq 10
-
-
- sum up input list (e.g. seq 10)seq 10|paste -sd+|bc
-
-
- find average of input list/filei=`wc -l filename|cut -d ' ' -f1`; cat filename| echo "scale=2;(`paste -sd+`)/"$i|bc
-
-
- generate all combination (e.g. 1,2)echo {1,2}{1,2}
-
-
- generate all combination (e.g. A,T,C,G)set = {A,T,C,G}
+#####generate all combination (e.g. A,T,C,G)
+
+```bash
+set = {A,T,C,G}
group= 5
-for ((i=0; i<$group; i++));do
- repetition=$set$repetition;done
- bash -c "echo "$repetition""
-
-
- read file content to variablefoo=$(<test1)
-
-
- echo size of variableecho ${#foo}
-
-
- echo tabecho -e ' \t '
-
-
- arraydeclare -A array=()
-
-
- send a directoryscp -r directoryname user@ip:/path/to/send
-
-
- split file into lines (e.g. 1000 lines/smallfile)$ split -d -l 1000 bigfilename
-
-
- System
-
-
-
-
- snapshot of the current processesps
-
-
- check graphics cardlspci
-
-
- show IP address$ip add show
ifconfig
-
-
- check system versioncat /etc/*-release
-
-
- Linux Programmer's Manuel: hier- description of the filesystem hierarchyman hier
-
-
- list jobjobs -l
-
-
- export PATHexport PATH=$PATH:~/path/you/want
-
-
- make file execuablechmod +x filename
-
-
- list screenscreen -d -r
-
-
- echo screen namescreen -ls
-
-
- check system (x86-64)uname -i
-
-
- surf the netlinks www.google.com
-
-
- add user, set passwduseradd username
-passwd username
-
-
- edit variable for bash, (e.g. displaying the whole path)1. joe ~/.bash_profile
-2. export PS1='\u@\h:\w\$'
3. source ~/.bash_profile
-
-
- edit environment setting (e.g. alias)1. joe ~/.bash_profile
-2. alias pd="pwd" //no more need to type that 'w'!
-3. source ~/.bash_profile
-
-
- list environment variables (e.g. PATH)$echo $PATH
-
-
- list all environment variables for current user$env
-
-
- show partition formatlsblk
-
-
- soft link program to binln -s /path/to/program /home/usr/bin
-
-
- show hexadecimal view of datahexdump -C filename.class
-
-
- jump to different nodersh node_name
-
-
- check port (active internet connection)netstat -tulpn
-
-
- find whick link to a filereadlink filename
-
-
- check where a command link to (e.g. python)which python
-
-
- list total size of a directorydu -hs .
du -sb
-
-
- copy directory with permission settingcp -rp /path/to/directory
-
-
- store current directorypushd . $popd ;dirs -l
-
-
- show disk usagedf -h
du -h
du -sk /var/log/* |sort -rn |head -10
-
-
- show current runlevelrunlevel
-
-
- switch runlevelinit 3
telinit 3
-
-
- permanently modify runlevel1. edit /etc/init/rc-sysinit.conf
-2. env DEFAULT_RUNLEVEL=2
-
-
- become rootsu
-
-
- become somebodysu somebody
-
-
- report user quotes on devicerequota -auvs
-
-
- get entries in a number of important databasesgetent database_name
getent passwd
getent group
-
-
- little xwindow toolsxclock
-xeyes
-
-
- change owner of filechown user_name filename
-chown -R user_name /path/to/directory/
-
-
- list current mount detaildf
-
-
- list current usernames and user-numberscat /etc/passwd
-
-
- get all usernamegetent passwd| awk '{FS="[:]"; print $1}'
-
-
- show all userscompgen -u
-
-
- show all groupscompgen -g
-
-
- show group of usergroup username
-
-
- show uid, gid, group of userid username
-
-
- check if it's rootif [$(id -u) -ne 0];then
- echo "You are not root!"
- exit;
-fi
-
-
- find out CPU informationmore /proc/cpuinfo
lscpu
-
-
- set quota for user (e.g. disk soft limit: 120586240; hard limit: 125829120)setquota username 120586240 125829120 0 0 /home
-
-
- show quota for userquota -v username
-
-
- fork bomb:(){:|:&};:
-
-
- check user loginlastlog
-
-
- edit path for all usersjoe /etc/environment
-
-
- show running processesps aux
-
-
- find maximum number of processescat /proc/sys/kernal/pid_max
-
-
- show and set user limitulimit -u
-
-
- which ports are listening for TCP connections from the networknmap -sT -O localhost
-
-
- print out number of cores/ processorsnproc --all
-
-
- check status of each core
-
-
-