Terminal and Shell

CBE 263

Resources

Notes

  • The terminal is the graphical window, and the shell is the program running inside it.
  • We are using Z shell, but most of the commands derive from Bash, which has the best tutorials.
  • This is also called the Command Line Interface (CLI)
  • It is common for computers to only have a CLI (I run three different servers like this).

Moving around

pwd

  • pwd to show where you are (print working directory)

Paths

  • /home/userName/cbe263/homework/hw05/hw05.ipynb
  • All files start at the root directory /
  • This is an absolute path.
  • Directories (folders) are separated by /
  • Relative paths specify the path relative to the current directory.
  • Special “directories”
    • . means the current directory
    • .. means the directory above (previous, or parent)
      • so, ../../ refers to your grandparent directory two levels up
  • ~ is a shortcut for your home directory

ls

  • ls to list files in current directory
    • ls -l to show a “long” view including permissions, user, file size, timestamp, filename
    • ls -a to show “all” files, including hidden files that start with a dot, like .zshrc
    • ls -t to show files sorted from new to old (-rt to reverse that order)
    • ls -1 to show the output in one column
    • Chain flags: ls -la for a long view of all files.
    • man ls to see the “manual” of the ls command with lots of options.
    • ls some/path lists files and directories in some/path
      • Suppose we are in homework folder
      • ls .. will show contents of cbe263
      • ls ../lectures will show the contents of cbe263/lectures assuming it exists
      • ls hw05 will show the contents of the hw05 folder below us.
      • All these are using relative paths. Can also use absolute paths:
        • ls /home/userName/cbe263 will show the contents of that folder.
        • ls ~/cbe263 will also show the contents of /home/userName/cbe263

cd

  • cd somePath will move to somePath (cd for connect directory).
    • cd ~ goes home, or just cd will go home
    • if we are in ~/cbe263:
      • cd homework/hw05 will take us to that folder
      • cd ../.. will take us back
      • cd /home/userName/cbe263/homework will take use to that folder
      • cd ~/cbe263/homework does the same thing

Create, copy, move, delete files and folders

mkdir

  • mkdir folderName creates folder folderName
  • mkdir hw05/folderName creates folderName inside of hw05 which is inside your currect directory
  • Absolute paths work too: mkdir /home/userName/cbe263/homework/hw05
    • or, mkdir ~/cbe263/homework/hw05

touch

  • touch filename creates file (not directory)
  • touch path/to/filename also

rm

  • rm file1 file2 file2 to remove a list of files
  • rm -r folder1 folder2 to delete folders; -r means recursive, for the folders
    • or use rmdir folder1 folder2
  • rm -f file to force the removal, otherwise you get asked if you are sure on each file. (Be careful!)
  • rm -rf to force removal of a folder

cp

  • cp file copiedFile copies file to copiedFile in the same (current) directory
  • cp -r folder copiedFolder uses the -r flag (as in rm) for directories
  • cp path/to/file path/to/copied/file will move something anywhere on your system to anywhere else.
    • can use relative paths
    • if you leave off the copied filename, then the filenme is the same, but just in a different path
  • cp ../../file . copies ../../file to the current directory, with the same filename.
  • cp ../../file newName copies ../../file to the current director but with name newName

mv

  • mv is like cp but moves instead of copies.
    • mv file fileNewName moves in the directory (changes file name)
    • mv ../file . moves “../file” to current directory
    • mv ../file fileNewName moves “../file” to fileNewName in current directory

Wildcards

  • ls *.py will list any file (*) that ends in .py
  • rm -rf * will delete all files or folders in this directory without asking first. (Dangerous!)
  • cp data*.py ~ will copy files that start with data and end with .py to your home folder.
  • Google “bash wildcards” for way more options. But * is used 95% of the time.

Show, search files

  • cat filename displays the contents of a file
  • head filename shows the first few lines
    • head -n 5 shors the first 5 lines
  • tail filename shows the last few lines
    • tail -n 5 shors the last 5 lines
  • grep searchWord fileName searches fileName for searchWord
    • flags:
      • -i to ignore case
      • -r to search in this folder and those below
      • -l just show the file searchWord is in, not matching lines
    • grep -irl numpy *.py will list all python files that contain the word numpy, ignoring case, in this directory or below.
  • find . -name filename will search for files named filename in the current directory and its children.
    • This is a very powerful command. Type man find to learn more.
    • You can control the search depth, what files to find, and execute commands on the result of what is found.

Permissions

  • ls -l shows file groups:
    • drwxrwxrwx - dol4 9 Oct 16:34 Downloads
    • the d indicates a directory
    • three groups of rwx for user (owner), group, others to read, write, and execute
    • execute means to enter a folder, or run an executable file (like a program file or script)
    • if a permission is off, one of the rwx will be replaced with a -
    • Change permissions: chmod u-x file will turn off (-) the execute permission for the user.
      • chmod u+x filename will turn it on
      • chmod ugo+rwx filename will turn on all permissions for all three user types (user, group, others)
      • chmod u+rw * will give the user read and write permissions for all files in this directory.
        • chmod -R u+rw * will include all subdirectories.

Redirection

  • Sometimes programs print output to the screen.
  • You can redirect this to a file with >
  • Suppose test.py contains print('hi there)
  • Then python test.py outputs “hi there” to the screen.
  • python test.py > delme results in file delme having the text hi there
  • If you use > on an existing file, it will rewrite the file.
  • To append the output to an existing file, use >>
    • ls >> out will append the output of the ls command to whatever is in the existing file out

.zshrc file

  • The .zshrc file is loaded whenever you start a terminal running the Z Shell.
    • You can tell if your prompt character is %
    • the Bash shell uses $
  • You can include customization, aliases, and set environment variables in your .zshrc file.
    • type env to show your current environment variables
    • type export VARNAME=VALUE to set an environment variable
    • The most important is probably PATH which holds the paths that are searched on your computer when running commands.

Miscellaneous

  • clear to clear the screen, or ctrl-l
  • Avoid characters like / * & % in filenames. Stick to numbers, letters, dashes, and underscores.
  • Avoid using spaces in filenames, use _ unstead (underscore).
  • Typing directory and filenames is slow. Hit Tab after typing part of the name and it will complete as much of the word as it can.
  • filename extensions help indicate what kind of file it is, and what program should open it (when double clicking)
    • file.sh bash script
    • file.zsh z shell script
    • file.py python file
    • file.md markdown file
    • file.ipynb jupyter notebook file
    • file.txt python file
    • file.x executable file
    • file.dat data file
    • file.inp input file
    • file.cpp c++ file
    • file.h header file
    • Etc. etc.
  • All of the file types shown above (except binary .x files) are simple text files and can be edited in a text editor (like Text Edit, or Notepad, or VScode, or vim, etc.). But don’t use MS word for this!
  • You can run executable files like myProgram.x by running ./myProgram.x.
    • You need the ./ so that the shell knows where to find the file.
  • To modify system files, or anything you don’t have permission to, you need to run as the root user.