Unix Bash Commands I Always Forget

For my reference on the go, and perhaps yours too.

Command line keyboard shortcuts

  • [ctrl] + [a] move cursor to beginning
  • [ctrl] + [e] move cursor to end
  • [ctrl] + [u] delete to beginning of line
  • [ctrl] + [k] delete to end of line
  • [ctrl] + [w] delete backwards to beginning of word
  • [opt] + [d] delete forwards to end of word
  • [fn] + [delete] forward delete

Adding directory to binaries to $PATH

export PATH=$HOME/path/to/binaries/bin:$PATH

Side note: $PATH and $HOME are environment variables.

ln -s /path/to/original/ /path/to/link

File compression

  • gzip compresses file
  • gunzip decompresses it

Alternatively, this command compresses an entire directory or a single file on Linux, working recursively to do so.

tar -czvf archive.tar.gz path-to-dir/to-compress/

This decompresses/extracts an archive instead.

tar -xzvf archive.tar.gz extractedstuff
  • -c for create, -x for extract
  • -z use gzip (use -j for bzip)
  • -v verbose mode
  • -f allows us to specify input/output

Processes and Jobs

  • ps shows execeuting processes.
  • jobs does a similar thing and also states its status.

  • [ctrl] + [c] kills a foreground process.
  • [ctrl] + [z] suspends a foreground process.

fg and bg puts processes in the foreground and background respectively. Can be used on suspended processes.

fg %jobnumber

kill terminates a suspended/background process. Syntax can be as above for jobnumber, or with process numbers (PID):

kill pid    #use the -9 option for stubborn ones

less: keyboard shortcuts

  • [k] moves up, [j] moves down
  • [space-bar] next page

  • [ctrl] + [f] forwards one window
  • [ctrl] + [b] backwards one window
  • [ctrl] + [d] forwards half window
  • [ctrl] + [u] backwards half window

  • / forward search
  • ? backward search
  • n and N to move between matches
  • &pattern displays only matching lines

  • [q] exit
  • [h] shows these shortcuts (and more)

grep

  • -i ignores upper/lower case in the search query
  • -v displays lines that do not match
  • -n preceeds the matching lines with the line number
  • -c prints the total count of matched lines

These can be combined. For example, grep -ivnc fxquah test.txt returns the number of lines which do not have fxquah or Fxquah. Note that the -n option can be omitted in this case.

wc

  • -w word count
  • -l line count
  • -c character count

This counts the numbers of users which are logged on.

who | wc -l

cat & sort

This joins two files into a single file.

cat list1.txt list2.txt > largelist.txt

This sorts a file and outputs it into another.

sort < largelist.txt > sortedlist.txt

ls

This lists all the files ending with …fxquah

ls  *fxquah

This lists all the files starting with one character following fxquah

ls fxquah?

find

find recursively descends the directory tree for each path listed to find a query. Some examples:

chmod

  • u user, g group, o other, a all
  • r read and copy files (or list files in a dir)
  • w change a file (or delete/move files from dir)
  • x execute (or access directory to read files)
  • + and - adds and takes away permission respectively

Example: this removes read write and execute permissions on the file for the group and others.

chmod go-rwx file.txt
  • quota checks amount of space allocated, and one’s usage.
  • df reports space left. -h option for human readability.
  • du reports usage. -h as before, and -s lists subdirectories.
  • file * classifies the chosen files by type.

Getting help: man, whatis & apropros

  • man full manual page.
  • whatis one-line description of comment
  • apropos keyword returns commands with keyword in their manual page header