Reputation: 65
I have a directory called A , and I would like to know the modification date of one file/directory inside. Here is my code
#!/bin/bash
find A | sort -d
var="$(head -2 | tail -1)"
echo "$var"
date -r $var '+%S'
That's basically how I want my code to be , when I do that, it only shows what's inside my directory A ( the find command), the echo doesn't work , date neither. I have a message saying : date +%s no file or directory of this type.
I've seen some questions about using a variable as a file, but in my case I don't see anything that could cause a problem (like a use of slash). So I wanted to know if there is something wrong.
Upvotes: 1
Views: 139
Reputation: 2868
find A | sort -d
print sorted list of files to the standard output.
head -2 | tail -1
makes head
wait for data from the standard input.
With nothing coming from the standard input, I think the script should hang forever (?)
var
is probably an empty string. So echo
print an empty string (nothing), and a new line.
Whatever is stored in var
, the filename is not found on the disk. This is why date
generated this error.
Give a try to this:
#!/bin/bash --
var="$(find A | sort -d | head -2 | tail -1)"
printf "%s " "$var"
date -r "$var" '+%s'
find
can print last modification time of the files with -printf
and %T
.
Give a try to this:
find A -printf "%p %T@\n" | sort -d | awk 'NR==2{print ; quit}'
-printf "%p %T@\n"
: prints the filename + (space char) +
last modification time of the file as seconds since Jan. 1, 1970, 00:00 GMT, with fractional part.
awk 'NR==2{print ; quit}
prints only the second line.
Upvotes: 6