Command Line (CLI)
- CLI: Command Line Interface
- Terminal: the application window you type into
- Shell: the program inside the terminal that interprets your commands
- Bash: Bourne-again shell β UNIX shell
- Console
And basically, it is a text-based way to interact with your computer.
β user interacts with Operating System (OS), the computer, through a Graphical-User Interface (GUI) β that is this CLI
Essential cmds
- some cmds
opt+arrow: move cursor one wordctrl+aandctrl+e: go to beginning, end of the line
- some options :
.(or./): current directory..: parent directory~: user home directory*: all files in current directory-R: content of a folder, recursively-hor--helporinfoorman: help-vor--version: version-u: user action instead of a root action>: send output to a file, overwrite>>: send output to a file, append*.extension
open: open a file, or a finder window to the dirls: lists files in current directorypwd: path to working dir(ectory)cd: change directory, or go home if no paramcd -: previous directory visited
mkdir: make directory (i.e. create folder)mkdir -p folder1/folder2: nested folders
touch: create new filemanfor manual / documentationcp&mv[ori_path] [target_path]: copy & move file from a path to another path (or rename)cp -R: for folders, add the option β-Rβ for recursivelycp file.txt .: copy file.txt to current directory
rm: remove file or directoryrm -r: remove directoryrm -i,rm -f: remove interactive, forcefully
find [dir_path] -name ["file"]: search for files in dir_path whose name is filegrep ['text'] [file]: search βtextβ in file-i: case-INsensitive-n: line-c: count the number of appearances-r: recursively does the search in the entire dir-w: entire word only-v: inverse search--color: color display
cat: output/print the content of a filehead/tail: output the first/last 10 lines of a file
echoorprintf: write textecho 'hey' >> [file]: append βheyβ to file (same with printf)echo 'hey' > [file]: overwrite βheyβ to file
- Chaining & piping :
[cmd_a];[cmd_b]: run A, then B[cmd_a]&&[cmd_b]: run A, then B if A passed[cmd_a]||[cmd_b]: run A, then B if A failed[cmd_a]&: run A in background[cmd_a]|[cmd_b]: run A, then pass the output of A to B
clear: clear terminal displayreset: proper reset + clean
- list the available serial ports
ls /dev/tty.*
ls /dev/cu.*
sudo [cmd]: run cmd as a superuser (SUper user DO)top: display live info about current running processesdu -h: memory used by files- brew: package manager to install other tools on macOS
brew install [sth]: install a packagebrew uninstall [sth]: uninstall a packagebrew doctor: check brew for potential problems
- Exit/Quit/Less in commands:
:q!q:qactrl+dorctrl+z: stop operationctrl+c: terminate operationctrl+\\\\: SIGQUIT sig, i.e. exterminate operation
- load bash profile in terminal
source ~/.bash_profile
bin : subdirectory containing executable programs
sudo apt-get install xxx
sudo apt-get update
sudo apt-get upgrade
network specifics
ipconfig getifaddr en0- get local ip address
ipconfig getoption en0 domain_name_serveripconfig getsummary en0- get summary
ifconfig | grep broadcast- my wifi info
curl ifconfig.me- get public ip address
hostname- get machine hostname
arp -a- list all devices connected to local network
ping -c 1 192.168.86.241
http://localhost:8080/can be accessed by devices on the same network, with the use of local IP addresses,
http://[YOUR-IP-ADDRESS]:8080/
http://192.168.1.15:8080/note: sometimes, itβs also worth modifying / removing the βhttp(s)://β part.
python specifics
python -V: which versionwhich python: what installation is used for what commandwhich python3which -a python python3: show full paths of all exe named python or python3
misc
sudo pkill coreaudiod β gently kill all audio deamon process
- GET https://example.com/api/end-point-name?key=your-api-key
- to request
Bash script 101
- create a bash script:
#!/bin/bash= a βshebangβ which tells the system to use Bash shell to exe this scriptchmod u+x: change moderator rights/permissions & convert file into an executable filechmod 700is equivalent
./in this current directory, + name of the exe β run the script
touch bashcmd
echo '#!/bin/bash' > bashcmd
echo 'echo HelloWorld!' >> bashcmd
chmod u+x bashcmd
./bashcmd
another example of bash file
#!/bin/bash
var = 'world'
echo Hello var!
echo "Hello $var"
current_date=$(date)
echo "Today is $current_date"
- declare a variable :
var = "hey" - read a variable :
echo $var read var: user input for the value of var
echo "What is your favorite color?"
read color # The user's input is stored in the variable 'color'
echo "You said your favorite color is $color. Nice!"- conditional statements
echo "Enter a number:"
read number
# Note the spaces! They are important.
if [ $number -gt 10 ]; then
echo "That's a large number."
elif [ $number -eq 10 ]; then
echo "That's exactly ten."
else
echo "That's a small number."
fi-eq: Equal to-ne: Not equal to-gt: Greater than-lt: Less than-ge: Greater than or equal to
and for strings:
=: Equal to!=: Not equal to
can also loop through a list:
for fruit in apple banana orange; do
echo "I like $fruit"
done
# This will list all .txt files in the current directory
for file in *.txt; do
echo "Found text file: $file"
done