bash) /
|
-----------------------------------------
| | | | |
bin usr home etc tmp
| |
----- -------------------------------------
| | | | | | | | |
ls cat john alice bill carl sam jane lisa
ls and cat, which are files./ character. /
|
-----------------------------------------
| | | | |
bin usr home etc tmp
| |
----- -------------------------------------
| | | | | | | | |
ls cat john alice bill carl sam jane lisa
. character... denotes your parent directory. It is the directory containing your current directory, unless you are at the root of the file system, in which case your parent directory is the root itself. E.g., home is the parent of alice.~ character denotes your home directory. It is where you are put when you start a terminal session. In general ~user denotes the home directory of user user. So if sam is my home directory, then ~ refers to the sam directory, while ~alice refers to alice’s home directory. /
|
-----------------------------------------
| | | | |
bin usr home etc tmp
| |
----- -------------------------------------
| | | | | | | | |
ls cat john alice bill carl sam jane lisa
pathname of the target./ separates the nodes along the pathname.absolute pathname, e.g., /home/bill is an absolute pathname to billrelative pathname, e.g. ../../bin/ls is a relative pathname from my current directory sam to ls/ as its first character; no relative pathname has / as its first character.sh — the Bourne shellcsh — the C shelltcsh — the Tenex C Shellksh — the Korn Shellbash — the Bourne-again shellzsh — the Z shellbash) since it is the default shell on the macs, linux machines, and WSL.The format of a simple bash command is
command [flags] [arguments]
where white spaces (blanks or tabs) separate the components. e.g.
$ ls
$ cp *.txt novelHowever, bash is a full-blown scripting language as well as an interpreter. It has regular expressions, conditionals, loops, and functions, e.g.,
$ for f in ch[1-3].ps
do
ps2pdf $f
doneenter key.A valid command is either internal or external. The shell understands an internal command and is able to execute it immediately. Examples of internal commands are
$ pwd
$ alias l=“ls -l”
If the shell does not understand a command, it assumes that it is an external command, and will search all directories specified by the PATH variable for a file having that command as name. If the file is found and is executable, the shell loads it into memory and executes it. Examples of external commands are
$ ls
$ head file
ls — list files and directoriescd — change directorypwd — print working directorymkdir — make directoryiesrmdir — remove directoriesrm — remove files and/or directoriesmv — move (or rename) files and/or directoriescp — copy files and/or directoriescmp — check if two files are the samediff — list all differences between two filesecho — echo command line argumentscat — concatenate filesless — browse text filesman — read manual page for external commands, system calls, and C APIinfo — similar to manhelp — ask for help with internal commandsnano on a Unix machine, TextEdit on a Mac (in plain text mode), and Notepad++ on Windows machine.TextWrangler, Sublime Text, and atom.emacs and vim.terminal or console)The command
runsprog < infile
prog with its stardard input coming from infile.The command
prog > outfile
runs prog with its stardard output redirected to outfile. If outfile does not exist, it will be created with content of the output from prog. If outfile exists, its old content will be overwritten by the output from prog.
The command
prog >> outfile
runs prog with its stardard output redirected to outfile. If outfile does not exist, it will be created with content of the output from prog. If outfile exists, its content will have the output from prog appended to its old content.
The command
prog1 | prog2
has the effect of running both prog1 and prog2 in parallel, but with the stardard output of prog1 being the stardard input of prog2.
The command
prog 2> errfile
runs prog and redirects the standard error output to the file errfile.
It’s possible to redirect the standard error output to the standard output. E.g.,
prog > outfile 2>&1
runs prog and redirects both the standard output and the standard error output to the file outfile. In bash, you can get the same effect by typing
prog &> outfile