LWM-Linux/06 - Linux File Operations/Directories and Symlinks (Shortcuts).md
2024-09-07 09:29:54 -06:00

3.5 KiB

Directories and Symlinks

Directories:

1. Definition:

A directory (also called a folder) is a container in a file system that can hold files and other directories. It organizes files and provides a hierarchical structure.

2. Types of directories:

  • Root directory: The top-level directory in the file system hierarchy, typically represented by "/" on Unix-like systems.
  • Home directory: The default directory for a user, usually containing personal files and configurations. "~"
  • Parent directory: The directory one level above the current directory, represented by "..".
  • Current directory: The directory you're currently in, represented by ".".
  • Subdirectory: Any directory contained within another directory. "/usr/bin" - bin is a subdirectory of /usr.

3. Directory operations:

  • Create: mkdir (make directory)
  • Delete: rmdir (remove directory - must be empty) or rm -rf ("f"orce remove "r"ecursively, even if not empty)
  • Change: cd (change directory)
  • List contents: ls (list)
  • View path: pwd ("p"rint "w"orking "d"irectory)

4. Directory permissions:

On Unix-like systems, directories have read (r), write (w), and execute (x) permissions. The execute permission allows users to enter the directory.

5. Hidden directories:

In Unix-like systems, directories starting with a dot (.) are hidden by default.

1. Definition:

A symlink is a special type of file that points to another file or directory. It's similar to a shortcut in Windows or an alias in macOS.

  • Soft links (symbolic links): Point to a file or directory by name. They can span file systems and can link to non-existent targets.
  • Hard links: Point directly to the inode of a file. They can't span file systems or link to directories.

Use the ln command with the -s option: ln -s target_path link_path # Create a symbolic link at link_path to the target_path

  • Save space by avoiding duplicate files
  • Create shortcuts to frequently accessed files or directories
  • Maintain multiple versions of files or configurations
  • Facilitate easier software updates and management
  • When you access a symlink, the system automatically redirects to the target.
  • Deleting a symlink doesn't affect the target file or directory.
  • If the target is deleted, the symlink becomes a "dangling" or "broken" link.
  • Use ls -l to see detailed file information. Symlinks are indicated by an "l" at the start of the permissions string.
  • The file command can also identify symlinks.
  • Some commands (like cp) don't follow symlinks by default. Use the -L or --follow options to change this behavior.
  • Unix-like systems (Linux, macOS): Native support for symlinks.
  • Windows: Limited support in older versions, full support since Windows Vista.

9. Security considerations:

  • Symlinks can potentially be used in symlink attacks, where an attacker creates a link to a sensitive file.
  • Many systems implement symlink protections to mitigate these risks.

10. Use cases:

- Creating shortcuts in the file system
- Managing shared libraries
- Facilitating easier software updates