mdls - macOS


Overview

mdls (macOS Dictionary Lookup Service) is a command-line tool that provides detailed information about files, including their metadata, attributes, and technical details. It is commonly used for inspecting files, extracting specific information, or troubleshooting file-related issues.

Syntax

mdls [options] <files...>

Options/Flags

  • -a, –all: Display all metadata attributes.
  • -n, –name: Display only the file name.
  • -s, –size: Display the file size in bytes.
  • -d, –date: Display the file’s creation, modification, and access dates.
  • -f, –format: Specify an output format: xml, json, plist, or custom.
  • -c, –count: Count the number of attributes and display the count only.
  • -r, –recurse: Recursively examine files within directories.
  • –hidden: Include hidden files in the results.
  • –noisHidden: Display files without the hidden attribute.
  • –resolveAlias: Resolve aliases to the actual files.
  • -k : Filter results based on a specific metadata key.
  • -i : Filter results based on a specific metadata value.
  • **-O **: Display only the requested attributes.

Examples

  • Get basic information about a file:
mdls myfile.txt
  • Display all metadata attributes in XML format:
mdls -a myfile.txt -f xml
  • Filter results for all files with a specific creation date:
mdls -d -k com.apple.metadata:_kMDItemDateAdded | grep 2023-03-08
  • Extract the modification date and store it in a variable:
modDate=$(mdls -d myfile.txt | grep 'kMDItemContentModificationDate' | cut -d = -f 2 | tr -d '"')

Common Issues

  • Incorrect syntax: Ensure you use the correct flags and separators.
  • No output: Verify that the file path is correct and the file exists.
  • Unknown key or value: Confirm that the specified key or value is a valid metadata attribute.

Integration

mdls can be combined with other commands for advanced tasks:

  • Find files with specific metadata:
find . -exec mdls -i value {} +
  • Extract metadata from a directory into a CSV file:
mdls -r -f csv myDir | csvtool convert csv2tsv
  • ls: List files and directories.
  • stat: Display file attributes.
  • sips: Process and modify image files.
  • exiftool: Read, write, and edit metadata.