Basics of Bash

Theory: Manipulating directories and files

Important note

Wildcards are building blocks for patterns that match files or directories. When you use ls or any other command that works with files and directories, you provide a path (recall relative and absolute paths from the previous lesson). When you refer to a path, you can also use wildcards that will possibly match multiple files or directories at once.

Basic wildcards are:

      • represents zero or more characters
  • ? - represents a single character
  • [] - represents a range of characters

Example:

ls docs/photos saturday.jpg sunday.jpg dog.jpg machine.jpg scan.tiff scan2.tiff ls docs/s* saturday.jpg sunday.jpg ls docs/*.jpg saturday.jpg sunday.jpg dog.jpg machine.jpg

Also, remember the shortcut for "home directory" — it's ~. You can use it in paths. For example, if your home directory is /home/michael, then ~/docs is the same as /home/michael/docs.

Lesson notes

  • mkdir to create directory
  • mkdir -p to create multiple levels of directories (e.g. mkdir -p dir1/dir2/dir3)
  • touch to change the date of a file or create a new file (e.g. touch newfile.txt)
  • mv to move or rename a file or a directory (e.g. mv old_name new_name)
  • rm to delete a file (e.g. rm readme.txt)
  • rm -r to delete a directory and all the directories inside it (e.g. rm -r photos)