How to use the command mdls (with examples)
- Osx
- December 25, 2023
The mdls
command is used to display the metadata attributes for a file on macOS. It provides detailed information about a file, such as the file name, size, creation date, modification date, and more.
Use case 1: Display the list of metadata attributes for a file
Code:
mdls path/to/file
Motivation: This use case is helpful when you want to quickly view all the available metadata attributes for a specific file. It allows you to get comprehensive information about the file without opening it or looking at its properties individually.
Explanation: In this command, mdls
is followed by the path to the file for which you want to display metadata attributes. The command will then list all the available attributes along with their corresponding values.
Example output:
kMDItemDisplayName = "example.txt"
kMDItemContentType = "public.plain-text"
kMDItemContentTypeTree = (
"public.plain-text",
"public.text",
"public.data",
"public.item",
"public.content"
)
kMDItemDateAdded = 2022-01-01 12:34:56 +0000
kMDItemDateCreated = 2022-01-01 11:22:33 +0000
kMDItemDateModified = 2022-02-02 10:20:30 +0000
kMDItemFSContentChangeDate = 2022-02-02 10:20:30 +0000
kMDItemFSCreationDate = 2022-01-01 11:22:33 +0000
...
Use case 2: Display a specific metadata attribute
Code:
mdls -name attribute path/to/file
Motivation: Sometimes, you may only be interested in a specific metadata attribute of a file. Using this use case allows you to directly retrieve the value of that specific attribute without having to go through the entire list of attributes.
Explanation: In this command, mdls
is followed by the -name
flag, which specifies that you want to display a specific attribute. attribute
should be replaced with the name of the attribute you want to retrieve. The path to the file is provided at the end.
Example output:
kMDItemContentType = "public.plain-text"
Conclusion:
The mdls
command is a useful tool for accessing and retrieving the metadata attributes of files on macOS. It allows you to view a comprehensive list of attributes or retrieve specific attributes as needed. Whether you want to explore the metadata of a file or grab a specific attribute, mdls
provides a convenient way to do so.