stat command is useful to get details like file type, size, inode, permissions, last accessed and modified timestamps, etc. You'll get all of these details by default. The
--printf options can be used to display only the required details in a particular format.
Here's an example to get accessed and modified timestamps of a file:
# sample directory and sample file $ mkdir stat_examples && cd $_ $ printf 'long\nshot\n' > ip.txt # %x gives the last accessed timestamp $ stat -c '%x' ip.txt 2023-03-27 20:20:55.217530670 +0530 # modify the file $ printf 'apple\nbanana\n' >> ip.txt # %y gives the last modified timestamp $ stat -c '%y' ip.txt 2023-03-27 20:21:50.298964283 +0530
Here's an example with some more file properties:
# %s gives file size in bytes # \n is used to insert a newline # %i gives the inode value # same as: stat --printf='%s\n%i\n' ip.txt $ stat -c $'%s\n%i' ip.txt 23 6438890
Here's an example for a linked file:
$ ln -s /usr/share/dict/words words.txt # %N gives quoted filenames # if input is a link, path it points to is also displayed $ stat -c '%N' words.txt 'words.txt' -> '/usr/share/dict/words'
You can also pass multiple file arguments:
$ printf '#!/bin/bash\n\necho hi\n' > hi.sh # %s gives file size in bytes # %n gives filenames $ stat -c '%s %n' ip.txt hi.sh 23 ip.txt 21 hi.sh
stat command should be preferred instead of parsing
ls -l output for file details. See mywiki.wooledge: avoid parsing output of ls and unix.stackexchange: why not parse ls? for explanation and other alternatives.
See also my Computing from the Command Line ebook.