Differences
This shows you the differences between two versions of the page.
Previous revision | |||
— | techwiki:bash [2022/12/03 10:55] (current) – [Bash profile] ying | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | This page contains Bash cmd info in general, and specific binary tool command in Mac and Linux. | ||
+ | ====== Bash profile ====== | ||
+ | user profile located at : <code bash> | ||
+ | |||
+ | edit user profile environment variable in the bash profile: | ||
+ | * display: <code bash> | ||
+ | * display with escape <code bash> | ||
+ | * display raw text: <code bash> | ||
+ | * display with variable: <code bash> | ||
+ | * display block of text: <code bash> | ||
+ | Block of text here | ||
+ | Text for $PATH | ||
+ | EOF</ | ||
+ | * display block of raw text: <code bash> | ||
+ | Block of text here | ||
+ | Text Again but no $PATH | ||
+ | EOF</ | ||
+ | * ref: http:// | ||
+ | * session based variable set:< | ||
+ | * permanently change to path in profile file:< | ||
+ | |||
+ | Note: by adding your_Path_To_3rd_Party_Binary to the $PATH variable, you can directly call them in terminal. | ||
+ | |||
+ | **My linux bash profile code** | ||
+ | |||
+ | <code bash> | ||
+ | # User specific aliases and functions | ||
+ | alias fx='cd ~/ | ||
+ | # refresh fx installation | ||
+ | alias fxnew=' | ||
+ | </ | ||
+ | ====== Bash tips ====== | ||
+ | * find path to a command file <code bash> | ||
+ | * escape by " | ||
+ | * create a txt file <code bash> | ||
+ | * create a empty file or update a file <code bash> | ||
+ | * append to a txt file <code bash> | ||
+ | * create a symbolic link <code bash>ln -s ~/real_dir ~/ | ||
+ | * make folder accessible to all <code bash> | ||
+ | * batch file rename< | ||
+ | for i in *.JPG ; do mv $i ${i/ | ||
+ | </ | ||
+ | |||
+ | * Vi text editor < | ||
+ | vi | ||
+ | # cmd mode | ||
+ | # i : enter text edit mode | ||
+ | # dd: delete line | ||
+ | # x : delete a char | ||
+ | # w : write file wq: save & quit | ||
+ | # q : quite q!: quit without save | ||
+ | # edit mode | ||
+ | # esc: back to cmd mode | ||
+ | # ':': | ||
+ | </ | ||
+ | |||
+ | ^ shortcut ^^ | ||
+ | | ctrl + z | suspend | | ||
+ | | ctrl + a | goto cmd line start | | ||
+ | | ctrl + e | goto cmd line end | | ||
+ | |||
+ | Full bash terminal shortcuts: http:// | ||
+ | |||
+ | ^ quick list ^^ | ||
+ | | man | manual, q:quit man; space: next | | ||
+ | | ls | ls -la: long list directory; ls -sh: list size human read | | ||
+ | | cp | cp -R: recursive copy directory | | ||
+ | | mv | move file | | ||
+ | | rm | rm -R: recursive delete directory | | ||
+ | | mkdir | make directory | | ||
+ | | source | source a shell script file | | ||
+ | | %%|%% | pipe prev cmd to next cmd | | ||
+ | | cmd& | run cmd background | | ||
+ | | clear | clear screen | | ||
+ | |||
+ | ====== bash - computer management: user, admin, network ====== | ||
+ | <code bash> | ||
+ | # check users: w, who, users | ||
+ | # | ||
+ | #< | ||
+ | # ifconfig | ||
+ | wifi(){ | ||
+ | #os x version | ||
+ | ifconfig en1 | grep 'inet ' | cut -d ' ' -f 2 | ||
+ | # linux version | ||
+ | # ifconfig en1 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}' | ||
+ | } | ||
+ | |||
+ | #< | ||
+ | # kill | ||
+ | |||
+ | # kill by name | ||
+ | alias kbn=ka; | ||
+ | ka(){ | ||
+ | for X in `ps acx | grep -i $1 | awk {' | ||
+ | kill $X; | ||
+ | done | ||
+ | } | ||
+ | alias ki='ka itunes' | ||
+ | </ | ||
+ | |||
+ | ====== bash - file operation ====== | ||
+ | <code bash> | ||
+ | |||
+ | #< | ||
+ | # ls | ||
+ | |||
+ | lss(){ | ||
+ | ls -R | grep ": | ||
+ | } | ||
+ | |||
+ | lsss(){ | ||
+ | ls -trR | ||
+ | } | ||
+ | |||
+ | # ls -file tools | ||
+ | # ref: http:// | ||
+ | alias a=' | ||
+ | alias aa='ls -AF' | ||
+ | alias aaa=' | ||
+ | alias ll='ls -lF' | ||
+ | alias lt='ls -ltF' # by time | ||
+ | alias lt.=' | ||
+ | alias lsB=' | ||
+ | alias lsS=' | ||
+ | alias lh='ls -lhF' | ||
+ | alias l=' | ||
+ | alias ld='ls -d */' | ||
+ | alias 1='ls -1F' | ||
+ | # find and remove all | ||
+ | # sudo find * -type d -name .svn -exec rm -rf {} \; | ||
+ | # | ||
+ | #< | ||
+ | md5(){ | ||
+ | openssl md5 " | ||
+ | # tip: quote $1 | ||
+ | } | ||
+ | |||
+ | # print directory structure | ||
+ | |||
+ | graph(){ | ||
+ | set `pwd` | ||
+ | set `basename $1` | ||
+ | ls -FR> | ||
+ | } | ||
+ | |||
+ | # clean ._mac file | ||
+ | #find . -name ._* -exec rm ' | ||
+ | |||
+ | # advanced sys function | ||
+ | |||
+ | |||
+ | uflat(){ | ||
+ | find . -not -type d -print0 | xargs -0J % mv -f % . | ||
+ | find . -type d -depth 1 -print0 | xargs -0 rm -rf | ||
+ | } | ||
+ | |||
+ | # replace text; usage: urep \[array of file] [find] [replace] | ||
+ | urep(){ | ||
+ | for i in $1; do j=`echo $i | sed ' | ||
+ | |||
+ | #for i in *.txt; do j=`echo $i | sed ' | ||
+ | } | ||
+ | urep2(){ | ||
+ | rename ' | ||
+ | } | ||
+ | |||
+ | # rename prefix; usage: upre/upost \[array of file] [pre/post] | ||
+ | upre(){ | ||
+ | for i in $1; do mv " | ||
+ | } | ||
+ | upost(){ | ||
+ | for i in $1; do mv " | ||
+ | } | ||
+ | |||
+ | # switch ext; usage: uext \[array of file] [old ext] [new ext] | ||
+ | uext(){ | ||
+ | for i in $1; do mv " | ||
+ | } | ||
+ | uext2(){ | ||
+ | for i in $1; do mv " | ||
+ | } | ||
+ | uext3(){ | ||
+ | rename ' | ||
+ | } | ||
+ | |||
+ | # rename case switch; usage: ucase \[array of file] | ||
+ | ucaseUnix(){ | ||
+ | rename ' | ||
+ | } | ||
+ | |||
+ | # clean space in name; usage: uclean \[array of file] | ||
+ | ucleanUnix(){ | ||
+ | rename 's/ //' $1 | ||
+ | } | ||
+ | |||
+ | # working (test) | ||
+ | # add date to pre/post; usage: udate \[array of file] [pre/post] [ext] | ||
+ | udate(){ | ||
+ | if [ $2 ] && [ $3 ]; then | ||
+ | if [ $2 == " | ||
+ | for i in $1 | ||
+ | do mv " | ||
+ | done | ||
+ | fi | ||
+ | else | ||
+ | for i in $1; do mv " | ||
+ | fi | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | Get running script directory | ||
+ | <code bash># | ||
+ | echo "The script you are running has basename `basename $0`, dirname `dirname $0`" | ||
+ | echo "The present working directory is `pwd`" | ||
+ | </ | ||
+ | |||
+ | * get file list <code bash> | ||
+ | * rename find list <code bash>for i in `find *v10*.jpg`; do j=`echo $i | sed ' | ||
+ | # for each file, generate new name, replace old name with new name</ | ||
+ | ====== bash - utility ====== | ||
+ | |||
+ | ===== grep ===== | ||
+ | * get lines contains word A <code bash> | ||
+ | * ignore case <code bash> | ||
+ | * match exact word <code bash> | ||
+ | * inverse selection, lines without word A <code bash> | ||
+ | * count <code bash> | ||
+ | * line number <code bash> | ||
+ | * list file <code bash> | ||
+ | |||
+ | * Pattern | ||
+ | * ^wordA: line starts with wordA | ||
+ | * wordA$: line ends with wordA | ||
+ | * ^$: blank line | ||
+ | * [set]: match one from set [0-9][A-Za-z] [abc] [x-z] | ||
+ | * [:class:]: match one from class [:alnum:] [:alpha:] [:blank:] [:digit:] [:lower:] [:upper:] [:space:] | ||
+ | * escape: \ | ||
+ | * .: any char | ||
+ | * \< \>: begin/end of word | ||
+ | * wordA|wordB: | ||
+ | * {n}: how many times appera in line {min,max} | ||
+ | * ?: at most once | ||
+ | * *: none to unlimited | ||
+ | * +: at least once | ||
+ | * ^: inverse range; begin of line | ||
+ | |||
+ | ref: | ||
+ | |||
+ | ===== sed ===== | ||
+ | |||
+ | line editor | ||
+ | <code bash>sed '/ | ||
+ | # same with _ | ||
+ | sed '/ | ||
+ | #line range ^start, $end | ||
+ | sed '1,100 s/ | ||
+ | </ | ||
+ | |||
+ | remove all comments in bash script | ||
+ | <code bash>sed '/ | ||
+ | |||
+ | ref: http:// | ||
+ | |||
+ | ===== awk ===== | ||
+ | awk is a words grabber, like grep for lines grabber | ||
+ | |||
+ | ref: http:// | ||
+ | |||
+ | |||
+ | ===== Vi ===== | ||
+ | <code bash> | ||
+ | # vi | ||
+ | # --- change current line to lower case | ||
+ | # : | ||
+ | |||
+ | # -- set file ending with DOS or Unix | ||
+ | # :set ff=dos or :set ff=unix | ||
+ | |||
+ | # -- mass converting dos< | ||
+ | # vim +" | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====== Mac specific binary tool ====== | ||
+ | An A-Z Index of the Mac OS X command line : http:// | ||
+ | |||
+ | enable root on mac : http:// | ||
+ | |||
+ | * osascript <code applescript> | ||
+ | * open : open cmd opens every thing, like " | ||
+ | |||
+ | ===== mac specific cmd - disk related operation ===== | ||
+ | |||
+ | * mount smb share folder (Windows, | ||
+ | if [ ! -d /Volumes/me ]; then mkdir / | ||
+ | </ | ||
+ | * Dmg file creation and convertion <code bash> | ||
+ | # make an DMG image name $2 from folder called $1 | ||
+ | dmgF(){ | ||
+ | echo "CMD: hdiutil create -srcdir \" | ||
+ | hdiutil create -srcdir " | ||
+ | } | ||
+ | |||
+ | # make an ISO image named as $2 from folder called $1 | ||
+ | isoF(){ | ||
+ | hdiutil makehybrid -iso -o " | ||
+ | } | ||
+ | |||
+ | # burn DMG file $1 | ||
+ | burn(){ | ||
+ | hdiutil burn $1 | ||
+ | } | ||
+ | |||
+ | # create a DMG file with size $1 MB named as $2 | ||
+ | dmg(){ | ||
+ | hdiutil create -size $1m $2.dmg -fs HFS+ -volname $2 | ||
+ | } | ||
+ | |||
+ | # convert DMG file of $1 to an ISO image file | ||
+ | dmg2iso(){ | ||
+ | hdiutil convert $1.dmg -format UDTO -o $1.iso | ||
+ | } | ||
+ | </ | ||
+ | * other disk related cmds <code bash> | ||
+ | ################################ | ||
+ | |||
+ | # force reject disk | ||
+ | #drutil tray open | ||
+ | #drutil eject | ||
+ | |||
+ | #< | ||
+ | vo(){ | ||
+ | cd / | ||
+ | } | ||
+ | |||
+ | #< | ||
+ | # dd function for disk dump | ||
+ | # | ||
+ | # dd back cd | ||
+ | # dd if=/ | ||
+ | # dd if=/ | ||
+ | |||
+ | # dd backup disk | ||
+ | # dd bs=512 if=/ | ||
+ | # dd bs=512 if=/ | ||
+ | |||
+ | # file rescue | ||
+ | # dd bs=512 if=/ | ||
+ | |||
+ | # back and split | ||
+ | # dd if=/ | ||
+ | # cat | ||
+ | # cat / | ||
+ | |||
+ | # list disk and device | ||
+ | # disktool -l | ||
+ | # df -k | ||
+ | # diskutil list | ||
+ | |||
+ | |||
+ | #< | ||
+ | # disktool | ||
+ | eject2 () { | ||
+ | / | ||
+ | } | ||
+ | |||
+ | eject(){ | ||
+ | eject2 /Volumes/$1 | ||
+ | } | ||
+ | |||
+ | #< | ||
+ | # tar | ||
+ | #create tar | ||
+ | #tar -czvf MyArchive Source_file | ||
+ | #tar --create --gzip --verbose --file=MyArchive Source_file | ||
+ | #extract tar | ||
+ | #tar -xzvf MyArchive Source_file | ||
+ | #tar --extract --gunzip --verbose --file=MyArchive Source_file | ||
+ | |||
+ | #tar xjvf filename.tar.bz2 | ||
+ | |||
+ | #tar -pvczf BackUpDirectory.tar.gz / | ||
+ | #tar --exclude=" | ||
+ | #tar -xvzf filename.tar.gz -C / | ||
+ | tarp(){ | ||
+ | tar -xvzf $1 -C $2 | ||
+ | } | ||
+ | tard(){ | ||
+ | tar czvf $1.$(date +%Y%m%d-%H%M%S).tgz $1 | ||
+ | exit $? | ||
+ | } | ||
+ | |||
+ | #tar -czvf my_backup.$(date +%Y%m%d-%H%M%S).tgz source_file | ||
+ | |||
+ | # unrar x test.part1.rar | ||
+ | |||
+ | </ | ||
+ | ===== mac specific bash cmd - others===== | ||
+ | <code bash> | ||
+ | # convert to pdf | ||
+ | alias pdf2="/ | ||
+ | pdf(){ | ||
+ | pdf2 -f " | ||
+ | } | ||
+ | #< | ||
+ | # man | ||
+ | function wman() { | ||
+ | | ||
+ | open `eval $url` | ||
+ | } | ||
+ | |||
+ | xman() { | ||
+ | | ||
+ | open $url$1 | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== Linux specific binary tool ====== | ||
+ | |||
+ | * convert | ||
+ | * convert image sequence into gif<code bash> | ||
+ | * convert big image into small tile parts <code bash> | ||
+ | * convert several images into one big image horizontally or vertically \\ <code bash> | ||
+ | convert -append 1.jpg 2.jpg out.jpg # vertically | ||
+ | </ | ||
+ | * zip \\ zip a folder into a zip file <code bash>zip -9 -r myZipFile.zip myFileOrFolderName</ | ||
+ | * xwininfo \\ get window info | ||
+ | * sha1sum \\ get sha1 sum of a file like md5 | ||
+ | * 7zip \\ 7z file opener <code bash># install or http:// | ||
+ | sudo apt-get install p7zip | ||
+ | # extract | ||
+ | p7zip e example.7z | ||
+ | # more ref: http:// | ||
+ | </ | ||
+ | |||
+ | ====== Other 3rd Party tool ====== | ||
+ | * lame - convert aiff to mp3 <code bash> | ||
+ | \" | ||
+ | * ffmpeg - convert image & video formats ([[http:// | ||
+ | // convert image seq into avi | ||
+ | ffmpeg -f image2 -i seq.%04d.jpg -r 30 -s 640x480 -vcodec mpeg4 test.avi | ||
+ | // -f: format; -i: input, %04d means 0001 like pattern; -r: framerate; -b:v video bitrate; -fs: file size limite; -s resolution; -vcodec: encode format | ||
+ | |||
+ | // get info | ||
+ | ffmpeg -i video.avi | ||
+ | |||
+ | |||
+ | // img seq to video | ||
+ | ffmpeg -f image2 -i image.%04d.jpg video.mpg | ||
+ | // ref: https:// | ||
+ | ffmpeg -r 8 -i frame.%04d.jpeg -vcodec libx264 -vpre medium -crf 24 -threads 0 -s 640x480 output.mp4 | ||
+ | |||
+ | // video to img seq | ||
+ | ffmpeg -i video.mpg image.%04d.jpg | ||
+ | |||
+ | // video to iphone mp4 or psp | ||
+ | ffmpeg -i source_video.avi -acodec aac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4 | ||
+ | |||
+ | ffmpeg -i source_video.avi -b 300 -s 320x240 -vcodec xvid -ab 32 -ar 24000 -acodec aac final_video.mp4 | ||
+ | |||
+ | // save sound | ||
+ | ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 sound.mp3 | ||
+ | |||
+ | // wav to mp3 | ||
+ | ffmpeg -i son_origine.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 son_final.mp3 | ||
+ | |||
+ | // mpg to avi, avi to mpg | ||
+ | ffmpeg -i video_origine.avi video_finale.mpg | ||
+ | ffmpeg -i video_origine.mpg video_finale.avi | ||
+ | |||
+ | // video to gif | ||
+ | ffmpeg -i video_origine.avi gif_anime.gif | ||
+ | |||
+ | // add souble | ||
+ | ffmpeg -i son.wav -i video_origine.avi video_finale.mpg | ||
+ | |||
+ | // video to flv | ||
+ | ffmpeg -i video_origine.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv video_finale.flv | ||
+ | |||
+ | // video to dvd | ||
+ | ffmpeg -i source_video.avi -target pal-dvd -ps 2000000000 -aspect 16:9 finale_video.mpeg | ||
+ | |||
+ | // ogm to mpg | ||
+ | ffmpeg -i film_sortie_cinelerra.ogm -s 720x576 -vcodec mpeg2video -acodec mp3 film.mpg | ||
+ | </ | ||