UNIX

Some helpful bash commands

  • Search for either of two sets of characters in a file: grep -e word1 -e word2 file or grep "word1\|word2" file or grep -E "word1|word2" file
  • List outdated pip packages: pip3 list -o or pip2 list --outdated
  • Print out the second column in a text file: cat file.txt | awk '{print $2}'
  • Send both stdout and stderr to the same file while running something in the background: ls -lR > output 2>&1 &. Note that if you start a process running in the background on ssh and then abruptly terminate the session without explicitly typing 'exit', then the background job will be terminated. In order to ensure a network disconnect does not terminate your job, exit the ssh session after beginning the background job and start a new ssh session to monitor the output. See also the Redirections section of the bash manual.
  • List largest files: du -a directory_name | sort -n -r
  • Summarize the total storage which a directory requires: du -s directory
  • Change permissions to 644 for files and 755 for directories, recursively, use chmod -R u+rwX,go+rX,go-w /path/to/dir
  • Find all files ending with .txt in a directory and all of its subdirectories: find . -name "*.txt"
  • Show two files side by side and use more to paginate the output paste 1.txt 2.txt | more This is particularly useful for comparing the output of two versions of a code.
  • Download a file from the internet (installing curl or wget may be required) wget http://host.com/dir/file.html or curl http://host.com/dir/file.html
  • Show the first or last 10 lines of a file: head -n 10 file.txt, tail -n 10 file.txt. Or to watch ("follow") a text file as it is being created: tail -f file.txt.
  • Here is a command that will list all formulas that are not dependent of any other formulas (leaves) and in front of them list all its dependencies. brew leaves | xargs brew deps --installed --for-each | sed "s/^.*:/$(tput setaf 4)&$(tput sgr0)/"
  • Use imagemagick to combine 4 images into one: montage image1.jpg image2.jpg image3.jpg image4.jpg -geometry +2+2 output.jpg
  • List processes owned by you ps ux, and owned by everyone ps aux.

Suggested UNIX Background

Here is a list of UNIX commands and concepts which I recommend everyone know:

  • How to use ls to list the files in a directory, and how to use ls -a and ls -l
  • How to use cat and more (or less) to view a file
  • How to use cd to change directory and what cd . and cd ~ mean.
  • How to use cp to copy a file, mv to move (or rename) a file, and rm to delete a file. What the -i flag means for these commands.
  • What an environment variable is, and how to set new environment variables and change ones that are already set.
  • How to use ssh to connect securely to different computers and scp to transfer files securely. When you starting out (or if you are having problems), you should try ssh -v so that you see what SSH is doing.
  • How to use grep to search in files
  • How to use man <command> and <command> --help to learn more about a command.
  • How to run commands in the background using &
  • How to use top to examine currently running processes and how to use kill to stop a process


Back to Andrew W. Steiner at the University of Tennessee.