|
|
|
Shell provides several tools for manipulating file paths. The key commands are dirname, basename, and realpath (or readlink). |
|
|
dirname extracts the directory portion: |
|
|
Multiple dirname calls go up the tree: |
|
|
basename extracts the filename: |
|
|
basename can strip a suffix: |
|
|
Combine dirname and basename: |
|
|
Handle paths with spaces: |
|
|
Parameter expansion alternatives (faster, no subshell): |
|
|
Get absolute path with realpath (GNU) or readlink: |
|
|
PWD gives current directory: |
|
|
Canonical path (resolve symlinks): |
|
|
Check if path is absolute: |
|
|
Join paths safely: |
|
|
Get file extension: |
|
|
Change extension: |
|
|
Relative path from one location to another: (GNU realpath or Python needed for portable solution) |
|
|
Path normalization (remove . and ..): |
|
|
Check common path conditions: |
|
|
Script’s own directory: |
|
|
Split path into components: |
|
|
Find common prefix of paths: |
|
|
Practical example - process files with path manipulation: |
|