You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ls # lists your files in current directory, ls <dir> to print files in a specific directory
ls -l # lists your files in 'long format', which contains the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified
ls -a # lists all files in 'long format', including hidden files (name beginning with '.')
ln -s <filename><link># creates symbolic link to file
readlink <filename># shows where a symbolic links points to
tree # show directories and subdirectories in easilly readable file tree
mc # terminal file explorer (alternative to ncdu)
touch <filename># creates or updates (edit) your file
mktemp -t <filename># make a temp file in /tmp/ which is deleted at next boot (-d to make directory)
cat <filename># displays file raw content (will not be interpreted)
cat -n <filename># shows number of lines
nl <file.sh># shows number of lines in file
cat filename1 > filename2 # Copy filename1 to filename2
cat filename1 >> filename2 # merge two files texts together
any_command ><filename># '>' is used to perform redirections, it will set any_command's stdout to file instead of "real stdout" (generally /dev/stdout)
more <filename># shows the first part of a file (move with space and type q to quit)
head <filename># outputs the first lines of file (default: 10 lines)
tail <filename># outputs the last lines of file (useful with -f option) (default: 10 lines)
vim <filename># opens a file in VIM (VI iMproved) text editor, will create it if it doesn't exist
mv <filename1><dest># moves a file to destination, behavior will change based on 'dest' type (dir: file is placed into dir; file: file will replace dest (tip: useful for renaming))
cp <filename1><dest># copies a file
rm <filename># removes a file
find . -name <name><type># searches for a file or a directory in the current directory and all its sub-directories by its name
diff <filename1><filename2># compares files, and shows where they differ
wc <filename># tells you how many lines, words and characters there are in a file. Use -lwc (lines, word, character) to ouput only 1 of those informations
sort <filename># sorts the contents of a text file line by line in alphabetical order, use -n for numeric sort and -r for reversing order.
sort -t -k <filename># sorts the contents on specific sort key field starting from 1, using the field separator t.
varname=value command# defines a variable to be in the environment of a particular subprocess
echo$varname# checks a variable's value
echo$$# prints process ID of the current shell
echo$!# prints process ID of the most recently invoked background job
echo$?# displays the exit status of the last command
read<varname># reads a string from the input and assigns it to a variable
read -p "prompt"<varname># same as above but outputs a prompt to ask user for value
column -t <filename># display info in pretty columns (often used with pipe)
let<varname> = <equation># performs mathematical calculation using operators like +, -, *, /, %
export VARNAME=value # defines an environment variable (will be available in subprocesses)
export -f <funcname># Exports function 'funcname'
export var1="var1 value"# Export and assign in the same statement
export<varname># Copy Bash variable
declare -x <varname># Copy Bash variable
array[0]=valA # how to define an array
array[1]=valB
array[2]=valC
array=([2]=valC [0]=valA [1]=valB) # another way
array=(valA valB valC) # and another
${array[i]}# displays array's value for this index. If no index is supplied, array element 0 is assumed
${#array[i]}# to find out the length of any element in the array
${#array[@]}# to find out how many values there are in the array
declare -a # the variables are treated as arrays
declare -f # uses function names only
declare -F # displays function names without definitions
declare -i # the variables are treated as integers
declare -r # makes the variables read-only
declare -x # marks the variables for export via the environment
declare -l # uppercase values in the variable are converted to lowercase
declare -A # makes it an associative array
${varname:-word}# if varname exists and isn't null, return its value; otherwise return word
${varname:word}# if varname exists and isn't null, return its value; otherwise return word
${varname:=word}# if varname exists and isn't null, return its value; otherwise set it word and then return its value
${varname:?message}# if varname exists and isn't null, return its value; otherwise print varname, followed by message and abort the current command or script
${varname:+word}# if varname exists and isn't null, return word; otherwise return null
${varname:offset:length}# performs substring expansion. It returns the substring of $varname starting at offset and up to length characters
${variable#pattern}# if the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest
${variable##pattern}# if the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest
${variable%pattern}# if the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest
${variable%%pattern}# if the pattern matches the end of the variable's value, delete the longest part that matches and return the rest
${variable/pattern/string}# the longest match to pattern in variable is replaced by string. Only the first match is replaced
${variable//pattern/string}# the longest match to pattern in variable is replaced by string. All matches are replaced
${#varname}# returns the length of the value of the variable as a character string
*(patternlist) # matches zero or more occurrences of the given patterns
+(patternlist) # matches one or more occurrences of the given patterns
?(patternlist) # matches zero or one occurrence of the given patterns
@(patternlist) # matches exactly one of the given patterns
!(patternlist) # matches anything except one of the given patterns
$(UNIX command)# command substitution: runs the command and returns standard output
typeset -l <x># makes variable local - <x> must be an interger