Wildcards

The shell uses special characters called wildcards to help you select multiple files at once based on patterns of characters. This is also known as globbing. Here are the most important wildcards and what they match:

  • *: Matches any number of characters. For example, *.txt matches all files ending with .txt.
  • ?: Matches any single characterData??? matches files starting with “Data” followed by exactly three characters.
  • [characters]: Matches any single character from the set inside the brackets. [abc]* matches files starting with ‘a’, ‘b’, or ‘c’.
  • [!characters] or [^characters]: Matches any single character not in the set.
  • [[:class:]]: Matches characters belonging to a specific group, like [:upper:] for any uppercase letter or [:digit:] for any numeral.

Files beginning with a period (.) are hidden and are not matched by default wildcards like *. You must explicitly use a pattern like .* to include them. However, .* will also match the . (current directory) and .. (parent directory) entries, so you can use more specific patterns like .[!.]* to avoid them.

File and Directory Manipulation Commands

The following are five core commands for managing files:

  • mkdir: Creates directories. You can create multiple directories at once, for example, mkdir dir1 dir2.
  • cpCopies files and directories.
    • cp item1 item2: Copies a single file or directory to a new name or location.
    • cp item... directory: Copies one or more items into a specified directory.
    • Important options:
      • -r or --recursiveRequired to copy entire directories, including all their contents.
      • -a or --archive: A more comprehensive copy that preserves all file attributes, like permissions and ownership.
      • -i or --interactive: Prompts you for confirmation before overwriting an existing file, which is a good safety measure.
      • -u or --update: Only copies files if they are newer than the existing ones in the destination, useful for syncing files.
    • cp examples:
  • mvMoves or renames files and directories. The original file no longer exists after the operation. It uses the same syntax and many of the same options (-i-u-v) as cp.
    • mv example:
  • rmRemoves or deletes files and directories.
    • WARNING: There is no undelete command in Linux. Once a file is deleted with rm, it’s gone for good.
    • -i or --interactive: Before deleting an existing file, prompt the user for confirmation. If this option is not specified, rm will silently delete files.
    • -r or --recursiveRequired to delete a directory and everything inside it.
    • -f or --force: Deletes without prompting and ignores non-existent files.
    • Safety Tip: Before using rm with wildcards, you can test the pattern with ls first to see which files will be affected.

Understanding Links (ln)

The ln command creates links, which are a way to give a file or directory an additional name. There are two types:

  • Hard Links:
    • Created with ln file link.
    • A hard link is another directory entry that points to the same inode as the original file. An inode is a data structure that contains all information about a file, including its content and attributes.
    • Key points:
      • You can’t tell a hard link from the original file; they are treated as identical. The ls -i command shows the inode number, which confirms if two names point to the same data.
      • A file’s data is only deleted from the disk when all of its hard links are removed.
      • Limitations: Hard links cannot link to directories or files on different disk partitions.
  • Symbolic Links (Symlinks):
    • Created with ln -s item link.
    • A symlink is a special file that contains the text path to the target file or directory. This is similar to a Windows shortcut.
    • Key points:
      • Symlinks can point to directories and files on different partitions, overcoming the limitations of hard links.
      • If the original file is deleted, the symlink becomes a broken link. The ls command often displays broken links in a different color.
      • Most commands operate on the file that the symlink points to, but rm is an exception—it deletes the link itself, not the target file.
      • Using relative paths when creating symlinks is recommended because it allows you to move the entire directory tree without breaking the links.

For practice, you can create a temporary “playground” directory, add files, and experiment with moving, copying, and linking them to get comfortable with the concepts. Then, you can use rm -r to delete the entire playground directory when you’re done.