Featured image

Table of Contents Link to heading

Globbing Link to heading

the process of using wildcards (glob characters) to specify patterns that match sets of filenames.

Instead of executing a command on a single file at a time, sets of files whose names matches a pattern can be affected at once.

The Asterisk * Link to heading

represents zero or more occurrences of any character in a filename.

For example, to list contents in the /usr/bin directory that begin with the word “vim”.

Asterisk

The Question Mark ? Link to heading

represents a placeholder to exactly any one character.

For example, to list contents in the /usr/bin directory that have 5 characters in total and begin with the word “ip”.

QuestionMark

The Square Brackets [] Link to heading

match a single character by representing a range of characters that are possible matched characters.

Set of Distinct Characters Link to heading

For example, to list contents in the /usr/sbin directory that:

  1. Have the first character being either “c” or “s”
  2. Have the second character being “f” or “g”
  3. End with the word “disk”

Square Brackets1

Set of Consecutive Characters Link to heading

Brackets can also be used to a represent a range of characters by using the dash - character (e.g. any number between and including “1” and “5”).

Square Brackets2

Set of Members of a Class Link to heading

Within the square brackets, it is possible to specify a character class expression by enclosing its name within square brackets and colons.

For example, to list contents in the /usr/sbin directory that:

  1. Have the first character not (!) being a capital letter
  2. Followed by any number of characters
  3. Have the next character being a punctuation
  4. Followed by exactly four characters
  5. End with a digit

Square Brackets3

The following character class expressions are supported:

  • [:alpha:]
    • Any alphabetic character.
  • [:lower:]
    • Any lowercase alphabetic character.
  • [:upper:]
    • Any uppercase alphabetic character.
  • [:digit:]
    • Any digit character.
  • [:alnum:]
    • Any alphanumeric character (alphabetic or digit).
  • [:space:]
    • Any white-space character (blank, horizontal tab, vertical tab).
  • [:graph:]
    • Any printable character, except the blank character.
  • [:print:]
    • Any printable character, including the blank character.
  • [:punct:]
    • Any printable character that is not white space or alphanumeric.
  • [:cntrl:]
    • Any nonprintable character.

These constructs are used for internationalisation and handle the different collation sequences as required by POSIX.

The Exclamation Mark ! Link to heading

used in conjunction with the square brackets to negate a range.

For example, to list contents in the /usr/sbin directory that ends with a “2” but do not begin with an “a” thorough “s”.

ExclamationMark

Copying Link to heading

The cp command copys files and directories and thus requires a source (the file to be copied) and a destination (where the copy is to be located).

When the destination is a directory, the resulting new file will have the same name as the original file.

If you want the new file to have a different name, you must provide the new name as the last part of the destination (after path/to/directory/).

  • Copy a file to another location:

    cp path/to/source_file.ext path/to/target_file.ext

  • Copy a file into another directory, keeping the filename:

    cp path/to/source_file.ext path/to/target_parent_directory

  • Recursively copy a directory’s contents to another location (if the destination exists, the directory is copied inside it):

    cp -R path/to/source_directory path/to/target_directory

  • Copy a directory recursively, in verbose mode (shows files as they are copied):

    cp -vR path/to/source_directory path/to/target_directory

  • Copy text files to another location, in interactive mode (prompts user before overwriting):

    cp -i \*.txt path/to/target_directory

    • This option should be used to avoid destruction of existing data if the destination file already exists.
  • Copy text files to another location but never overwrite an existing file. overwriting):

    cp -n \*.txt path/to/target_directory

  • Follow symbolic links before copying:

    cp -L link path/to/target_directory

  • Use the first argument as the destination directory (useful for xargs … | cp -t <DEST_DIR>):

    `cp -t path/to/target_directory path/to/file_or_directory1 path/to/file_or_directory2 …

Moving Link to heading

The mv command moves or renames files and directories.

  • When a file is moved, the file is removed from the original location and placed in a new location.

  • If you do not have the right permissions, you will receive a “Permission denied” error message.

  • Move a file or directory into an existing directory:

    mv source existing_directory

  • Rename a file or directory when the target is not an existing directory:

    mv source target

  • Move multiple files into an existing directory, keeping the filenames unchanged:

    mv source1 source2 source3 existing_directory

  • Do not prompt for confirmation before overwriting existing files:

    mv -f source target

  • Prompt for confirmation before overwriting existing files, regardless of file permissions:

    mv -i source target

    • This option should be used to avoid destruction of existing data if the destination file already exists.
  • Do not overwrite existing files at the target:

    mv -n source target

  • Move files in verbose mode, showing files after they are moved:

    mv -v source target

Creating Link to heading

The touch command creates files and set access/modification times. The mkdir command creates directories and set their permissions.

Creating Files Link to heading

  • Create specific files:

    touch path/to/file1 path/to/file2 ...

  • Set the file [a]ccess or [m]odification times to the current one and don’t [c]reate file if it doesn’t exist:

    touch -c -a|m path/to/file1 path/to/file2 ...

  • Set the file [t]ime to a specific value and don’t [c]reate file if it doesn’t exist:

    touch -c -t YYYYMMDDHHMM.SS path/to/file1 path/to/file2 ...

  • Set the file time of a specific file to the time of anothe[r] file and don’t [c]reate file if it doesn’t exist:

    touch -c -r ~/.vim path/to/file1 path/to/file2 ...

Creating Directories Link to heading

  • Create specific directories:

    mkdir path/to/directory1 path/to/directory2 ...

  • Create specific directories and their [p]arents if needed:

    mkdir path/to/directory1 path/to/directory2 ...

  • Create directories with specific permissions:

    mkdir -m rwxrw-r-- path/to/directory1 path/to/directory2 ...

Removing Link to heading

The rm command removes files or directories. The rmdir command removes empty directories (without files).

  • The -i option should be used when deleting multiple files or directories by using wild cards.

Removing Files Link to heading

  • Remove specific files:

    rm path/to/file1 path/to/file2 ...

  • Remove specific files ignoring nonexistent ones:

    rm -f path/to/file1 path/to/file2 ...

  • Remove specific files [i]nteractively prompting before each removal:

    rm -i path/to/file1 path/to/file2 ...

  • Remove specific files printing info about each removal:

    rm -v path/to/file1 path/to/file2 ...

Remove Directories Link to heading

  • Remove specific directories:

    rmdir path/to/directory1 path/to/directory2 ...

  • Remove specific nested directories recursively:

    rmdir -p path/to/directory1 path/to/directory2 ...

  • Remove specific files and directories [r]ecursively:

    rm -r path/to/file_or_directory1 path/to/file_or_directory2 ...