Linux Notes
- 4 minsAlias
- create
alias foo = "cd ..; ls; cd ..; ls" - delete
unalias foo - Alias will be gone if the terminal is closed. To make these alias persistent, store them in bashrc/zshrc.
nano .zshrc
History
- history of inputs
history history | grep ls control + rto enter query mode, andcontrol + cto exit.
Details of a command
- type of a command
type ls - manual page of a command
man ls - whatis
whatis ls
I/O Redirection
- store output, if there is error, error will show in terminal and output.txt will not be written
ls -l > output.txt >is used to overwrite a file and>>is used to append a filels -l >> output.txt
Error Redirection
Types of I/O:
- Standard Input - stdin - 0
- Standard Output - stdout - 1
- Standard Error - stderr - 2
Error messages are printed to standard error. > only redirects standard output, so standard error is still shown on the terminal. To redirect them:
- stdout to one file and stderr to another file
command >output.txt 2>error.txt - redirect stderr to stdout(&1)
command >output.txt 2>&1 - redirect both to a file
command &> output.txt
cat
- open a file
cat file.txt - create and write to a file, type
control+dtwice to exit.cat > test.txt
pipelines and grep
- the pipeline makes the last output as the input for next command.
stdin | stdout - uniq
ls | uniq - sort
ls | uniq | sort - wc - word count
wc -l : prints the number of lines in a file. wc -w : prints the number of words in a file. wc -c : prints the count of bytes in a file. wc -m : prints the count of characters from a file. wc -L : prints only the length of the longest line in a file. - grep
grep "pattern" sample.txt ls | grep txt - head/tail -n shows first/last n lines
head -n 5 tail -n 5
Echo
- Echo will print matching files in current dir
echo * : prints all files under current dir. echo D* : prints all files starting with D. echo [[:upper:]]* : all files starting with UpperCase. echo Five Divided By Two is $((5 / 2)) : Five Divided By Two is 2. - {}
echo here {1..9} echo hey {Z..A} $echo $(ls)More about Dollar Sign
$
Quotes and Exceptions
- Exceptions
` \ $ ""and''echo example ~/P* $(echo test) $((2 + 2)) $USER Output: example /Users/yiyang/Parallels /Users/yiyang/Pictures /Users/yiyang/Public test 4 yiyang echo "example ~/P* $(echo test) $((2 + 2)) $USER" Output: example ~/P* test 4 yiyang echo 'example ~/P* $(echo test) $((2 + 2)) $USER' Output: example ~/P* $(echo test) $((2 + 2)) $USER
Processes
- list all processes
ps : prints all processes running in the terminal ps x : prints all processes in the machine ps aux : prints all processes in the machine with detailed info - top lists top processes sorted by %CPU
top - kill the specified pid
kill 45461
Environment Variables
- printenv shows environment variables
- export
export intern=yy echo $intern - unset removes the env var
unset intern