How to get the date on a directory entry using "ls" and not on the whole directory contents?

StackOverflow https://stackoverflow.com/questions/21686059

  •  09-10-2022
  •  | 
  •  

Question

I seem to want the opposite of everyone else - How may i use (in bash scripts) an ls -al /some/path/to/where/ever/. to get just the entry for ".", not for everything in "."? What I'm after is the dir's date, so. in other words, what's the date on the /some/path/to/where/ever/. directory?

Doesn't have to be "ls" that is just what seemed natural.

Was it helpful?

Solution

You can do stat command:

stat -c "%y %n" .

OR for EPOCH value:

stat -c "%Y %n" .

OTHER TIPS

You want to use the -d option to get the entry for the directory itself, not the contents of the directory.

ls -ld /some/path/to/where/ever

In this case, the -a option would be unnecessary, since you are not listing the contents of the given argument.

Instead of using ls, you can use stat to capture the date. This way, the date isn't in a shifting format, and you don't have to filter it out from the rest of the output:

$ stat -f "%Sm" $directory_name
Feb 10 14:19:47 2014

$ stat -f "%Dm" $directory_name
1392059987     # Number of seconds since the "Epoch" (Usually Jan 1, 1970).

The stat command varies from system to system, so read your manpage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top