阅读笔记 - 你可能并不需要 GUI

68 阅读2分钟

For practicing English

list

ls my_folder        # Simple
ls -la my_folder    # -l: show in list format. -a: show all files, including hidden. -la combines those options.
ls -alrth my_folder # -r: reverse output. -t: sort by time (modified). -h: output human-readable sizes.

tree                                                        # on Linux
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'      # on MacOS
# Note: install homebrew (https://brew.sh) to be able to use (some) Linux utilities such as tree.
# brew install tree

create

touch 'new file'    # updates the file's access and modification timestamp if it already exists
# or
> 'new file'        # note: erases the content if it already exists

mkdir 'untitled folder'
# or
mkdir -p 'path/may/not/exist/untitled folder'

remove

# remove permanently
rm my_useless_file

rm -r my_useless_folder

find . -name "*.bak" -type f -delete

copy

cp readme.txt documents/

cp -a myMusic myMedia/
# or
cp -a myMusic/ myMedia/myMusic/

duplicate

cp readme.txt readme.bak.txt

More advanced:

cp readme{,.bak}.txt
# Note: learn how the {} works with touch foo{1,2,3}.txt and see what happens.

cp -a myMusic/ myMedia/
# or if `myMedia` folder doesn't exist
cp -a myMusic myMedia/

move

# Always use a trailing slash when moving files
mv readme.txt documents/

mv myMedia myMusic/
# or
mv myMedia/ myMusic/myMedia

rename

mv readme.txt README.md

mv myMedia/ myMusic/

merge directories

rsync -a /images/ /images2/	# note: may over-write files with the same name, so be careful!

view

cat apps/settings.py
# if the file is too big to fit on one page, you can use a 'pager' (less) which shows you one page at a time.
less apps/settings.py

imgcat image.png
# Note: requires iTerm2 terminal.

search

grep -i "Query" file.txt

# search all files in current working directory, quickly (entire disk in less than 15 minutes)
ripgrep -i "Query"
# brew install ripgrep

show info

du -sh node_modules/

df -h  # show disk size

stat -x readme.md   # on macOS
stat readme.md      # on Linux

# check cpu usage
top
htop

# know whether your computer is under load
glances
# brew install glances

open

xdg-open file   # on Linux
open file       # on MacOS
start file      # on Windows

open -a appName file

zip

zip -r archive_name.zip folder_to_compress

unzip archive_name.zip

unar archive_name.zip
unar archive_name.7z
unar archive_name.rar
unar archive_name.ISO
unar archive_name.tar.gz

peek

zipinfo archive_name.zip
# or
unzip -l archive_name.zip

lsar -l archive_name.zip
lsar -l archive_name.7z
lsar -l archive_name.ISO
lsar -l archive_name.rar
lsar -l archive_name.tar.gz

find

find my_folder -mtime +5 # find all files modified more than 5 days ago

calendar

cal

cal 11 2018

date

date +%m/%d/%Y

date -d "+7 days"   # on Linux
date -j -v+7d       # on MacOS

calculator

bc -l

force quit

killall -9 program_name

poweroff or reboot

# poweroff
sudo shutdown -h now
# reboot
sudo shutdown -r now

USB

df                     # locate USB drives

sudo umount /dev/sdb1  # unmount USB drives

# format USB drives
# FAT32
sudo mkfs.vfat /dev/sdb1
# NTFS
sudo mkfs.ntfs /dev/sdb1
# exFAT
sudo mkfs.exfat /dev/sdb1

sudo fsck /dev/sdb1   # check USB format

run command on all files of a directory

for FILE in *; do echo $FILE; done

check

# check server response
curl -i umair.surge.sh
# curl's -i (--include) option includes HTTP response headers in its output.


# check network connectivity to a remote address and port
nc -vz www.google.com 443
nc -vz 1.1.1.1 53

# check DNS config of a domain
dig www.google.com

# check the ownership and registration of a domain
whois www.google.com

Hotkeys

Ctrl + A  Go to the beginning of the line you are currently typing on
Ctrl + E  Go to the end of the line you are currently typing on
Ctrl + L  Clears the Screen, similar to the clear command
Ctrl + U  Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H  Same as backspace
Ctrl + R  Lets you search through previously used commands
Ctrl + C  Kill whatever you are running
Ctrl + D  Exit the current shell
Ctrl + Z  Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W  Delete the word before the cursor
Ctrl + K  Clear the line after the cursor
Ctrl + T  Swap the last two characters before the cursor
Ctrl + F  Move cursor forward one character
Ctrl + B  Move cursor backward one character
Esc + T   Swap the last two words before the cursor
Alt + T   Same as Esc + T
Alt + F   Move cursor forward one word on the current line
Alt + B   Move cursor backward one word on the current line
Esc + F   Same as Alt + F
Esc + B   Same as Alt + B
Alt + .   Paste the last word of the most recently command
Tab       Auto-complete files and directory names