Tech notes

Javascript, HTML, CSS

Jan 16, 2020 | productivity command line

Command line: basic knowledge


Cheet sheets from the book "Learn Enough Command Line to Be Dangerous" by Michael Hartl.

Basic
Command Description Example
man <command> Display manual page for command
which Locate a program $ which curl
curl Interact with URLs $ curl -O https://example.com
ping <url> Ping a server URL $ ping google.com
echo <string> Print string to screen
⌃C Get out of hanging terminal
⌃A Move to beginning of line
⌃E Move to end of line
⌃U Delete to beginning of line
⌥ (option) + click Move cursor to location clicked
Up & down arrow Scroll through previous commands
clear or ⌃L Clear screen
exit or ⌃D Exit terminal

Manipulating Files
Command Description Example
> Redirect output to filename $ echo foo > foo.txt
>> Append output to filename $ echo bar >> foo.txt
cat <filename> Print content of file to screen $ cat hello.txt
diff <f1> <f2> Difference between two files
ls List directory
list -l List long form
list -rtl List long, by reverse modification time
ls -a List all, including hidden
touch <filename> Create new file
mv <old> <new> Rename(move) old file to new
cp <old> <new> Copy old to new
rm <filename> Remove (delete) file
rm -f <filename> Force remove file

Interesting fact: Why Unix command are so short? (rm, cp,ls instead of remove,copy,list). "The answer is that Unix dates from a time when most computer users logged on to centralized servers over slow connections, and there could be a noticeable delay between the time users pressed a key and the time it appeared on the terminal. For frequently used commands like listing files, the difference between list and ls or remove and rm could be significant. As a result, the most commonly used Unix commands tend to be only two or three letters long."

Inspecting files
Command Description Example
head <file> Display first 10 lines of the file
tail <file> Display last 10 lines of the file
wc <file> Count lines, words, bytes in the file
cmd1 | cmd2 Pipe cmd1 to cmd2 $ head foo.txt | wc
less <file> View file contents interactively
grep <string> <file> Find string in file $ grep foo bar.txt
grep -i <string> <file> Find case-insensitively $ grep -i foo bar.txt
Manipulating processes
Command Description Example
ps Show processes $ ps aux
$ ps aux | grep server
top Show processes(sorted) $ top
kill -<level> <pid> Kill a process $ kill -15 24601
pkill -<level> -f <name> Kill matching processes
E.g.:kill all the processes with name spring
$ pkill -15 -f spring
Directories

In some cases you need to add 'sudo' before command to execute it as superuser.

Command Description Example
mkdir <name> Make directory with name $ mkdir foo
pwd Print current directory
cd <name> Switch to another directory
find Find files & directories $ find . -name foo*.*
cp -r <old> <new> Copy recursively $ cp -r ~/foo .
rmdir <dir> Remove (empty) dir $ rmdir foo/
rm -rf <dir> Remove dir & contents $ rm -rf foo/
grep -ri <string> <dir> Grep recursively(-r) and case-insensitive(-i) $ grep -ri foo bar/

Back to blog