JB

CLI Representation of Files

File Names

  • File names serve as identifiers within a directory for end-users.
  • File names must be unique within their directory.
  • Files in different directories can share the same name.

Paths

  • The path must be unique, not the file name.
  • File names are components of a longer string called the path.

File Name Length

  • POSIX file systems typically limit file names to 255 characters.
  • This limit applies only to the file name itself, not the entire path.

Allowed Characters in POSIX

  • Most characters are allowed except the null character and forward slash (/).
  • Forward slash is a directory separator in paths.
  • Avoid spaces and special characters like &, '', "", *, ?, [], and $.
  • These characters have other meanings in the shell and can cause issues.

Case Sensitivity

  • POSIX file names are case-sensitive (e.g., "Hello" and "hello" are different).
  • Windows file names are not case-sensitive.
  • Copying files between Windows and Linux can cause problems.

Hidden Files

  • In POSIX, file names starting with a dot (.) are hidden.
  • Hidden files are not shown by default in standard ls command output.
  • The ls -a option lists all files, including hidden ones.
  • Hidden files are often used for configuration files.

File Extensions

  • File extensions are not required in POSIX systems.
  • Using extensions is recommended for file identification.

Essential File and Directory Commands

  • ls: Lists files and directories.
    • ls -l: Provides a long listing with details like permissions, owner, size, and modification date.
    • ls -a: Lists all files, including hidden ones.
  • cd: Changes the current directory.
    • cd (without arguments): Returns to the home directory.
  • rm: Removes (deletes) files.
    • rm -r: Recursively removes directories and their contents (dangerous!).
  • rmdir: Removes empty directories.
  • cp: Copies files.
  • mv: Moves files.
    • Moving a file within the same directory renames it.
  • find: Finds files based on various criteria.
  • ln: Creates links (hard or symbolic).
    • Symbolic links are similar to shortcuts in Windows.

Demonstration of Commands

  • ls -l shows file type, permissions, owner, group, size, and date.
  • Colors in the terminal indicate file types and executables.
  • Executable files are often green and have an "x" in the permissions list.
  • pwd shows the current working directory.
  • mv bad.html good.html renames bad.html to good.html.
  • cp good.html bad.html creates a copy of good.html named bad.html.

Wildcards and Globing

  • Wildcards are special characters that match arbitrary characters or sequences of characters.
  • Used to manipulate groups of files.
  • *: Matches any sequence of zero or more characters.
  • ?: Matches a single character.
  • []: Matches any of the characters listed inside the brackets.
  • Wildcards do not match hidden files unless the pattern starts with a dot.

Examples of Wildcards

  • ls /usr/bin/*zip: Shows all files in /usr/bin/ that end with "zip".
  • ls /usr/bin/??zip: Shows files in /usr/bin/ that have two characters followed by "zip".

Path Names

  • Every file has a unique absolute path starting from the root.
  • Path names consist of file names separated by forward slashes.
  • The final component of a path is the base name (file name).
  • The preceding portion is the path prefix.
  • Absolute paths begin with a forward slash (root directory).

Relative Paths

  • The simplest relative path is just the file name itself.
  • . denotes the current directory.
  • .. denotes the parent directory.
  • ~ denotes the user's home directory.

Path Length

  • The total maximum length for an entire path is much more than 255 characters.

Absolute vs. Relative Paths

  • Given a file /home/u123456/file1.txt and the current directory /home/u123456/csc1030:
    • Absolute path: /home/u123456/file1.txt
    • Relative path: ../file1.txt
    • Relative path using tilde: ~/file1.txt (if the current user is u123456)

Viewing File Content

  • more and less: View file content page by page.
    • Spacebar to advance, b to go back, q to quit.
  • cat: Displays the entire file content at once.
  • head: Shows the first few lines of a file.
  • tail: Shows the last few lines of a file.
  • bat: An enhanced cat clone with syntax highlighting and line numbers.

File Creation

  • touch: Creates a new empty file.
  • Output redirection: Saving command outputs to a file.
    • >: Overwrites the file.
    • >>: Appends to the file.
  • Automation relies heavily on CLI options.

Examples of File Creation and Redirection

  • touch my_new_file.txt && ls my_new_*: Creates a file and lists files starting with my_new_.
  • whoami > my_new_file.txt && cat my_new_file.txt: Saves the username to a file and displays its contents.
  • date >> my_new_file.txt && cat my_new_file.txt: Appends the current date to the file.
  • echo "some text here" >> my_new_file.txt: Appends text to the file.

Searching File Content: grep

  • Searches for patterns in a file.
  • grep pattern filename: Returns lines containing the pattern.
  • Patterns can be literal strings or regular expressions.
  • Searching is powerful and is supported by most command line options. Automating searches speeds up file review and anlalysis.

Example of grep

  • grep free filename | wc -l: Counts the number of lines containing "free".

Stream Editor: sed

  • A non-interactive text editor for modifying files on the fly.
  • Uses commands to perform find and replace operations.
  • Can use regular expressions for sophisticated matching.

Cutting and Extracting Text: cut

  • Extracts specific parts of a text file based on character position or fields.

Options for cut

  • -c: Specifies the range of characters to cut.
  • -d: Specifies the delimiter between fields.
  • -f: Lists the fields or columns to extract.
  • -s: Suppresses lines without delimiters.

Examples of Cut

  • cut -d : -f 5 file.txt: Extracts the fifth field from file.txt, using : as the delimiter.
  • ls -l /usr | grep ^t | cut -c 2-10: Lists files in /usr, filters for those starting with t, and extracts characters 2-10 (permissions).

Joining Files: join

  • Joins two files based on a common field.
  • Example: join file1 file2, where file1 contains "1 deKaiser" and file2 contains "1 stem", results in "1 deKaiser stem".

Pip pelining

  • Output of one command becomes the input of the next.
  • command1 | command2 | command3

Pipelining Example

  • find /dev -name 't*' 2> /dev/null | grep tty | sort | head -5
  • Finds files in /dev starting with t, ignores errors, filters for tty, sorts, and shows the first 5 results.

Fun with Commands

  • Commands like fortune, cowsay, and lolcat can be combined for humorous output.
    • fortune | cowsay | lolcat: Displays a fortune cookie in a cow speech bubble with colors.
    • ls -l | lolcat adds color to the output of the long listing.
  • htop | lolcat adds color to the output of the htop (process monitoring) command.