Linux Commands

Shell, Quotes, and Command Substitution

  • ls: lists directory contents; default behavior shows files and subdirectories in the current directory.
  • ls -a: shows all files including hidden files (those starting with a dot).
  • ls -R: recursive listing; shows files in a directory and all subdirectories.
  • Quotes:
    • Single quotes '…': the shell does not interpret any characters inside; metacharacters are treated literally.
    • Double quotes "…": the shell does interpret some characters (e.g., variable expansions, command substitutions) but still protects spaces and most shell metacharacters.
  • Backquotes ... (command substitution): the shell runs the command inside backquotes and replaces it with its output. Example: echo Today is date.
  • Shell can execute a command inside another command via command substitution, often using backticks ... or the modern form $(…).
  • Variables:
    • Create a variable: myVariable='Hello Everyone!'
    • Print a variable: echo $myVariable
    • Export to environment: export myVariable (makes it an environment variable for child processes)
    • View environment variables: env
    • Pipe environment search: env | grep myVariable
    • Escape a variable reference: echo \$hallo (prints $hallo literally, not the value of a variable named hallo)
    • Unset a variable: unset myVariable
  • Variables vs Aliases:
    • Aliases can be created to shorten commands: alias mV='env | grep mijnVariabele'
    • Show all aliases: alias
    • Show information about a specific alias: type
    • Aliases can be queried with typing or using alias to list; unalias can remove an alias (not shown in transcript but commonly used).
  • Semicolon ;
    • Use semicolon to run commands sequentially on one line: command1; command2
  • Command types and evaluation:
    • Type an alias to see what it expands to (or if it is a function, etc.): type
  • Wildcards and metacharacters in quotes:
    • Asterisks * and ? are interpreted as wildcards unless quoted; enclosing in "" or '' prevents expansion in many cases.
  • Help and manuals:
    • man : show manual page for a command.
    • man -f : show a short description of the command (whatis-like).
    • man -k : search manuals for the keyword (apropos).
  • Pattern matching with brackets:
    • [ ] matches any one of the characters inside the brackets. Example: [gu]* matches files starting with g or u, followed by anything.
    • [a-z]* matches files starting with any lowercase letter a through z.
  • Which/Whereis/Locate/Updatedb:
    • whereis : shows locations of the command and its manuals.
    • locate : quickly finds files by name in a database updated by updatedb.
    • locate -c : count matches for a pattern in locate output.
    • locate -b : search based on the basename (not the full path).
    • locate -n is not in transcript; always remember options like -c and -b for basename.
    • ! (exclamation) is used in some shells for history or negation in patterns; in locate, [!a-t] means files not starting with a through t.
    • updatedb: manually update the locate database (helps locate find recent files).
  • Quick command-line help:
    • --help: show basic usage and options for a command (often a concise aid).
  • Example output hints for ls -l:
    • drwxr-xr-x 1 root root 4096 Jul 19 06:52 linux.txt
    • The first column shows file type and permissions; next the link count; then owner; group; size in bytes; timestamp; name.
  • File types and ownership:
    • / is the root directory (highest level of the filesystem hierarchy).
    • ~ or home/username: user’s home directory (each user typically has their own home directory, e.g., /home/sysadmin).
    • Regular file: - (for permissions string example -rw-r--r--).
    • Directory: d in the permissions string (e.g., drwxr-xr-x).
    • Symbolic link: | is shown in some visualizations, but the file type in permissions is often l in the first character of the listing (e.g., lrwxrwxrwx).
  • pwd and cd:
    • pwd: print working directory (the path of the current directory).
    • cd : change to a directory; cd Documents moves to the Documents directory.
    • cd ..: move up one directory level.
    • Absolute path: starts from / (e.g., /home/sysadmin) — exact location.
    • Relative path: based on current directory (e.g., School/Art).
    • cd ../../Downloads: go up two levels then into Downloads.
  • Permissions overview:
    • Owner (user) permissions, Group permissions, Other permissions.
    • r (read), w (write), x (execute).
    • Example structure: -rwxr-xr-- (regular file with owner rwx, group rx, others r).
    • The transcript’s shorthand indicates owner, group, and others and what they can do (R, W, X as concepts).
    • Special cases include directories (d) and executable files (x).
  • Hard links vs symbolic links:
    • Hard link: multiple directory entries point to the same inode (same file, different name/location).
    • Symbolic link: a shortcut to another file (created with ln -s ).
  • Tree view and pagers:
    • tree: displays directory tree structure from the current directory.
    • tree | less or tree | more: page through the tree output.
  • Redirection and streams:
    • STDIN: input to commands.
    • STDOUT: standard output from commands.
    • STDERR: standard error from commands.
    • 2> errors.txt: redirect only errors to a file.
    • &> file.txt: redirect both stdout and stderr to a file.
  • Text processing basics:
    • tr 'a-z' 'A-Z' < file.txt: translate lowercase to uppercase using standard input.
    • sort : sort lines in a file by name (alphabetical order).
    • wc : report lines, words, and bytes of a file; use -l (lines), -w (words), -c (bytes).
    • cut : split lines into fields; -t sets the delimiter; -f n or -n to specify fields.
  • File content inspection:
    • head : read the first 10 lines by default.
    • tail : read the last 10 lines by default.
    • tail -n : read the first/last n lines depending on -n.
    • tail -n + : read from line N to the end (line-numbered start).
  • Creating and editing text files:
    • echo "text" > file.txt: write text to a file (overwrites).
    • echo "text2" >> file.txt: append text to the file.
    • cat : print the contents of a file.
    • mkdir: create a directory; mkdir -p Map1/Map2 creates nested directories in one command.
  • File and directory operations:
    • cp : copy files or directories (with -r to copy directories recursively).
    • mv or mv : move/rename files or directories.
    • rm : remove a file; rm -r : remove a directory and its contents recursively; rm -f: force removal without prompt; rm -i: prompt for confirmation.
  • Linking and file references:
    • ln : create a hard link.
    • ln -s : create a symbolic (soft) link.
  • Directory listing and metadata:
    • ls -l shows metadata like permissions, link count, owner, group, size, timestamp.
    • ls -a shows hidden files.
    • ls -R lists subdirectories as well.
  • Pattern matching, filtering, and searching:
    • grep : search for a pattern within a file or set of files.
    • egrep: grep extended with additional pattern features like ?, +, and | (alternation).
    • groupadd, useradd, and related commands manage groups and users:
    • groupadd creates a new group; groupadd -g to assign a specific GID.
    • groupmod -n or groupmod -g to modify group attributes.
    • groupdel deletes a group.
    • useradd creates a new user; useradd -D shows default values; usermod for modification; userdel to delete.
    • passwd : assign a password to a user.
    • whoami and w: display the current user or who is logged in.
    • su : switch user to another user; exit to leave.
    • usermod -aG : add user to a supplementary group; usermod -G to set groups; -a to append.
  • Permissions and ownership details:
    • chmod u/g/o/a +/-/= r/w/x : modify permissions for user (owner), group, others, or all, using symbolic notation.
    • chmod 754 : example of numeric (octal) permission specification. The octal digits map to rwx for user, group, and other respectively.
    • In the following mapping:
    • The octal digit 7 corresponds to rwx (read, write, execute).
    • The octal digit 5 corresponds to r-x (read, no write, execute).
    • The octal digit 4 corresponds to r-- (read only).
    • Therefore, the permission set for 754 is:
      ext{chmod 754} \Rightarrow rwx\; r-x\; r--.
    • Note: the first character of a permission string indicates file type (e.g., d for directory, - for regular file).
  • Real-world relevance and best practices:
    • Use ls -l to verify permissions and ownership when diagnosing access issues.
    • Use mkdir -p to safely create nested directory structures in one command.
    • When copying or moving, consider whether you need to preserve metadata (timestamps, permissions) and use appropriate flags (e.g., cp -p, mv, etc.).
    • Use quotes to prevent shell from misinterpreting filenames with spaces or special characters.
    • Use --help and man pages to learn command-specific options and safety cautions.
  • Connections to foundational principles:
    • Paths, working directory, and environment variables underpin how commands locate resources and how processes inherit settings.
    • Permissions model (read, write, execute for user/group/others) protects resources and governs access control.
    • The distinction between hard and soft links illustrates how the filesystem can reference the same data from multiple names without duplicating data, or via shortcuts.
    • Text processing pipelines (grep, sort, cut, tr, wc) enable modular, composable data workflows that are foundational to shell scripting.
  • Ethical, philosophical, and practical implications:
    • Proper permissions minimize risk of data leakage or unauthorized modification; misconfigurations can introduce security vulnerabilities.
    • Command history and aliases can speed workflows but may mask complex operations; documenting and auditing commands is good practice.
    • Hard links can complicate file deletion semantics since multiple names refer to the same data; symbolic links reduce risk of breaking references but depend on the target remaining available.
  • Quick reference cheat-sheet (key commands and concepts):
    • File listing: ls, ls -a, ls -R, ls -l, ls -S
    • Navigation: pwd, cd , cd .., cd -
    • Paths: absolute vs relative; ~ as home; / as root.
    • Redirection: output > file, append >> file, 2> errorfile, &> both to file.
    • Pipes: use | to connect commands (stdout of one to stdin of next).
    • Text processing: cat, head, tail, tr, sort, wc, cut
    • Searching: grep, egrep, locate, whereis, which, man, --help
    • Permissions: chmod (symbolic or numeric), chown, chgrp
    • Users/Groups: useradd, usermod, userdel, groupadd, groupmod, groupdel, passwd
    • Linking: ln -s (soft), ln (hard)
    • Viewing: tree, less, more, cat
    • Manual access: man , man -f , man -k
  • Numerical and symbolic references sprinkled through the notes utilize LaTeX formatting for clarity where relevant:
    • For example, the mapping of octal permissions to symbolic permissions is shown as:
      ext{chmod 754} \Rightarrow rwx\; r-x\; r--.
    • Basic permission components: read (r), write (w), execute (x).
  • Note on terminology:
    • Regular file: a standard data file.
    • Directory: a container for files.
    • Symbolic link: a shortcut to another file.
    • Hard link: another directory entry pointing to the same inode.
    • Wildcards/globbing: patterns like * and ? that expand to matching filenames, unless escaped or quoted.
  • Foundational commands and their purposes (quick mental map):
    • ls: listing; cd/pwd: navigation; cp/mv/rm: file operations; mkdir/rmdir: directory creation/removal; ln: linking; cat/head/tail: viewing; grep/egrep: searching; sort/tr/cut/wc: text processing; tr: character translation; locate/whereis/updatedb: locating files and resources; chmod/chown/chgrp: permissions and ownership; user/group management commands: create/modify/delete; man/--help: documentation.