bash Cheat Sheet by gregcheater via cheatography.com/26582/cs/7469/ basic bash commands bash redire ctions (cont) control structure (case) pwd : print working directory command &>> file : redirect stdout and # patterns are file globing regex stderr to file. (creates the file if it doesn't exist cd /path/ to/dir : change direcotry case <ex pre ssi on> in and append to the end it if it does exist) pat tern1) ls /dir/t o/list : list directory content (default is command < file : redirect stdin to file. [st ate ments] .) command1 | command 2 : uses the output ;; -1 : display the content on one column of command1 as the input of command2 pat tern2) -l : display the content with long listing format [st ate ments] -a : display the content of the directory file globbing regex ;; (including hidden files) \ : escape character. It deletes the *) -R : Display the content of the directory and signif ication of a special character [st ate ments] the content of subdirectories ? : Any character, once. ;; mv /path/ to/file /path/ whe re/ to/move : move esac * : Any character, 0, 1 or many time. or rename a file or a directory [...] : Any character that is in the class. ex: cp /path/ to/file /path/ whe re/ to/copy : copy a function definition [abc], [a-z], [0-9] file [^...]: Any character that is not in the class. function functionName { -r : copy recurs ively (used to copy directory) ex: [^abc], [^a-z], [^0-9] [st ate ments] rm /path/ to/file : remove a file {s1, s2, sN} : match s1 or s2 or sN [return X] -r : remove recurs ively (used to remove } direct ories) control structure (if) -f : force remove condit ional expres sions if <expression>; then mkdir /path/ dirName : create an empty && : logical and operator [st ate ments] directory elif <ex pre ssi on>; then || : logical or operator rmdir /path/ to/dir : remove a directory (works [st ate ments] only if the direcotry is empty) [[ string ]] : return 0 if string is not empty else [[ -z string ]] : return 0 if the string is empty [st ate ments] bash redire ctions [[ string1 == string2 ]] : return 0 if the string fi command > file : redirect stdout to file. are equivalent (creates the file if it doesn't exist and overwrite [[ string1 != string2 ]] : return 0 if the string control structure (while) it if it does exist) are not equivalent command >> file : redirect stdout to file. while <expression>; do [[ string =~ pattern ]] : return 0 if the string (creates the file if it doesn't exist and append to [st ate ments] matches the pattern (extended regex) the end it if it does exist) done [[ -e file ]] : return 0 if the file exists command 2> file : redirect stderr to file [[ -d file ]] : return 0 if file is a directory (creates the file if it doesn't exist and overwrite control structure (for) [[ -f file ]] : return 0 if file is a file it if it does exist) for var in <expression>; do command 2>> file : redirect stdout to file. [[ -x file ]] : return 0 if file is executable echo $var (creates the file if it doesn't exist and append to [[ $n1 -eq $n2 ]] : return 0 if $n1 == $n2 [st ate ments] the end it if it does exist) [[ $n1 -lt $n2 ]] : return 0 if $n1 < $n2 done command &> file : redirect stdout and stderr [[ $n1 -gt $n2 ]] : return 0 if $n1 > $n2 to file (creates the file if it doesn't exist and [[ $n1 -ge $n2 ]] : return 0 if $n1 >= $n2 overwrite it if it does exist) [[ $n1 -le $n2 ]] : return 0 if $n1 <= $n2 [[ $n1 -ne $n2 ]] : return 0 if $n1 != $n2 By gregcheater Published 14th March, 2016. Sponsored by Readability-Score.com Last updated 12th April, 2016. cheatography.com/gregcheater/ Measure your website readability! Page 1 of 3. https://readability-score.com
bash Cheat Sheet by gregcheater via cheatography.com/26582/cs/7469/ more basic bash commands more basic bash commands (cont) variables (cont) passwd : change your password man comman dName : Display the manual ${VAR%%pattern} : return a substring of for command comman dName VAR where the longest string (starting from the history : consult the history of your command end) matching “pattern” will be cut sudo command : run the command as jobs : list of your pending proccesses $? : the exit status of the last command / superuser cat file1 file2 ... : concat enate files and print function executed. usually 0 when everything command1 | xargs -i command2 : uses the to stdout went right. output of the command1 as the input of the more / less file1 file2 .. : diplsay a file page $# : the number of args passed to the script / command2. output will be accessible via {} in by page on stdout command2 function tail / head number : display the " num ber " first $0 : the name of the script or last line of a file on stdout grep (simple regex) $n : the nth argument passed to the script / touch file1 file2 ... : change the modifi cation grep " pat ter n" file1 file2 ... : print the lines function date of the files that matched the pattern $@ : the list of all the argument passde to chmod : change the privileges of a file / the script / function -v : print lines that didn't match the pattern directory Arrays -i : ignore the character case echo " tex t" : display a line of text to stdout myA rra y=( value1 value2 value3): declare an -l : print the name of the files that have at sort file1 file2 ... : sort the file (combine files if array least one match many are specified) and print the result to declare -a myArra y=( value1 value2 value3): -o : print only the piece of line that matched stdout (files aren't impacted) declare an array the pattern -r : sort in reverse order ${m yAr ray [in dex]} : access an element -E : uses the extended regex -n : numerical sort (index starts at 0) -q : quiet. returns 0 in $? if at least one line -u : delete duplicated lines myA rra y[i ndex]= : add or modify the element has been matched. 1 if no line matched wc file1 file2 ... : print to stdout the number of at index charac ters, words and lines of files variables ${# myA rra y[*]} : return the lenght of the array -l : number of lines only VAR=VARVALUE : create a variable VAR. ${m yAr ray [*]}: all the elements of the array -w : number of words only the variable can be accessed like so: $VAR or ${VAR} simple regex -w : number of characters only VAR="$VAR2" : $VAR will contains the value diff file1 file1 : compare file1 and file 2 for \ : escape character. It deletes the of $VAR2 differ ences signif ication of a special character VAR='$VAR2' : $VAR will contains $VAR2 -i : ignore the character case . : joker. It represents any characters VAR=$(command) : $VAR will contains the -B : ignore empty lines * : 0, 1 or many repetition of the last output of the command character / sequence of character -w : ignore whites paces (( VAR = $VAR + 1 )): the double ^ : The beginning of the line -c : add context to the output (good for parent heses must be used when doing readability) $ : The end of the line arithm etics which comman dName : print the path of a [...] : Any character that is in the class. ex: ${VAR#pattern} : return a substring of VAR command [abc], [a-z], [0-9] where the smallest string (starting from the pushd / popd /path/ to/dir : change directory [^...]: Any character that is not in the class. beginning) matching “pattern” will be cut using the directory stack ex: [^abc], [^a-z], [^0-9] ${VAR##pattern} : return a substring of VAR dirs : print the directory stack \(...\) : Capture the pattern. The pattern can where the longest string (starting from the find /path/ to/dir -name pattern : find every then be accessed with \1, \2 ... \n depending on beginning) matching “pattern” will be cut the number of capture in the regex files and directory that have a name that ${VAR%pattern} : return a substring of VAR matches " pat ter n" in the directory specified and \{n\} : n repeti tions of the last character / where the smallest string (starting from the end) its subdir ect ories sequence of character matching “pattern” will be cut By gregcheater Published 14th March, 2016. Sponsored by Readability-Score.com Last updated 12th April, 2016. cheatography.com/gregcheater/ Measure your website readability! Page 2 of 3. https://readability-score.com
Recommend
More recommend