Basic description and guide on bash and zsh usage.
Documentation based on materials from MIT The Missing Semester of Your CS Education course
When you open the server, you should see something like this:
user.name@server-name-ip-11-222-333-444:~$
user.name@server-name-ip-11-222-333-444
indicates the machine you are on, the ~
symbol means that you are in the home directory (aka the path that you will reach when you type cd
) and $
means that you are not the root user.sudo
command (short for SuperUser DO). sudo
. This will affect a lot of people if you are sharing a server with others. In short, do NOT sudo
into a shared server if you’re not sure!Common shell commands can be found under this Bash Scripting Cheatsheet. It works for Z shell too!
When you are uncertain, -h
or --help
flag will print out the short explanation for the command. Try ls -h
and see.
Common commands to note for dummies:
pwd
: print your current working directory.cd
: take you to home directory.cd
+ path: take to the specific path.cd .
: current directory.cd ..
: go up one level, i.e. parent directory.cd -
: go back one level.ls
: list files inside your current directory.ls -lah -t
: list all files, including hidden file (-a
flag), in long format (-l
flag), in human readable size (-h
flag), ordered by recency (-t
flag).ls
+ path: list all files inside specific pathcat
+ filename: print file, i.e. shows the contents of the file in your terminal. Works for multiple files too.vim
+ filename: open vim editor to view or edit the file. See section 4 for more information on vim editor.mkdir
: makes a new directory. E.g. mkdir ~/shell-usage/dummies
touch
: creates a new file. E.g. touch filename.txt
cp
: copy files. E.g. cp [source directory] [destination directory]
. Works for multiple files too.mv
: move files or change file name. Same usage as cp
.rm
: remove fileshead -n [N] filename
, tail -n [N] filename
: prints first (head) or last (tail) N lines of a filersync
: to sync your local files to the server and vice versa. See Rsync Cheat Sheet.How to keep your scripts running on the server even though you lost connection: use tmux
(see Tmux Cheat Sheet) or screen
(Screen Cheat Sheet).
man
: For each command there are certain flags, for example typing ls
will give you the list of files inside your current directory. However to see all hidden files, you need to use the a
flag, i.e. by typing ls -a
. To see all the details about the flags, use the man
command. E.g. man ls
.
>
(rewire input) and <
(rewire output).root@machine:~/playground/demo
hello
* As you see from the above, output of `echo 'hello'`, which is the text 'hello', became the input to be written in file1. You can also use `>>` to append a file.
* Another method is to use pipes with the `|` operator. The output of the command before `|` will be the input of the command after `|`. For example:
```shell
root@machine:~/playground/demo
# cat file1 | head -n1 20-09-03 - 8:25:54
hello
vimtutor
in your terminal (that has already have vim inside) and follow the instructionsvim filename
esc
: Escape key to go back to normal modei
: insert mode (to insert words)v
: visual mode (for highlighting)hjkl
: navigating, left, down, up, right respectively:q!
: force quite, discard changes:x
or :wq
: write and quitvimtutor
for more commands.ssh -N -f -L [port]
[port] username@servername
every time you want to tunnel to server, you can edit it in your ~/.zshrc file.vim ~/.zshrc
function tunnel() {
ssh -N -f -L localhost:$1\
$1 $2@$3
}
tunnel [port] username servername