Skip to content

Instantly share code, notes, and snippets.

@dmaynor
Created August 18, 2024 21:55
Show Gist options
  • Save dmaynor/6d8663af70d25556232cc06744264ba7 to your computer and use it in GitHub Desktop.
Save dmaynor/6d8663af70d25556232cc06744264ba7 to your computer and use it in GitHub Desktop.
OSX inode resolution.
#!/bin/bash
# Check if a file or directory is passed as an argument
if [ -z "$1" ]; then
echo "Usage: $0 <file-or-directory-path>"
exit 1
fi
# Get the path of the file or directory
TARGET_PATH="$1"
# Function to get attributes using inode
get_file_attributes_by_inode() {
local parent_dir="$1"
local inode="$2"
# Find the file with the given inode
local file_path=$(find "$parent_dir" -inum "$inode" 2>/dev/null)
if [ -n "$file_path" ]; then
echo "File: $file_path"
echo "Inode: $inode"
echo "Attributes:"
stat -x "$file_path"
echo "------------------------"
else
echo "No file found with inode: $inode"
fi
}
# Get the inode of the target
inode=$(stat -f "%i" "$TARGET_PATH")
parent_dir=$(dirname "$TARGET_PATH")
# If the target is a file, get attributes using its inode
if [ -f "$TARGET_PATH" ]; then
get_file_attributes_by_inode "$parent_dir" "$inode"
# If the target is a directory, iterate over its contents
elif [ -d "$TARGET_PATH" ]; then
echo "Directory: $TARGET_PATH"
echo "------------------------"
# Iterate over all files in the directory
find "$TARGET_PATH" -type f | while read -r file; do
inode=$(stat -f "%i" "$file")
get_file_attributes_by_inode "$(dirname "$file")" "$inode"
done
else
echo "The provided path is not a valid file or directory."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment