And yes,
we're going to dive out to the command line now where we're going to be able
to understand a little bit more about our file metadata.
We're going to see how we can list files and sort
them by their last modified time.
This can be really useful to look at files that have been edited recently.
We're going to take a look at how we can view our metadata
using the stat command and how we can use the touch command
to modify some of our timestamps.
We'll also see in using the copy command how we can preserve our metadata,
important perhaps where we're archiving these files or creating backups.
And here we are again on the command line of Alma Linux 8. When I go
through and use the ls ‑l command, we know we can see our metadata, but
there are more tricks to this.
I can sort using the timestamp, the last modified timestamp of the file,
using r where I can reverse sorting, which is going to mean the most
recently edited files will show last. Let's go through and run this against
my server configuration directory, so /etc.
Now taking a look towards the bottom, these will be
the most recently edited files.
The option, ‑r, becomes really useful, in this case, so the
most recently edited files are the last files shown rather
than the first files shown.
I know, though, you aren't going to believe me. The
best is to see this yourself.
So if we look quickly at editing our hosts file,
I can use sudo and vi /etc/hosts. I just use uppercase G to move to the end
of the file and then lowercase o to insert a new line.
I'm just going to put an entry in for Google DNS server,
so 8.8.8, space, and then the hostname that I want to
assign Google. I can use escape,
colon, and X to save and exit, and now we should be
ready to go. If I rerun the listing,
we're going to be able to see the most recently edited file is
indeed our host file in the /etc directory.
So it is working, and I wasn't lying to you, and we can see how
easy this is to see the most recently edited files. Now
something you want to see all of the time is when people want to
create a new and empty file, they use the touch command.
So if I go through my home directory and touch file1, that
would have created the file. File1 will be empty, but if I
look at ls ‑l and file1,
we can go through and see it's a 0‑byte file, but I can also use the file
command against file1, and we can see that the file is empty.
But another way to view our metadata on the file is the stat command.
So if I use stat, and escape, and dot, it can bring up the last argument. So
as a quick way to fill out file1, I can go through and see the metadata, but
with a little bit more detail where the metadata is separated out and we
have, then a little title box against it.
We can go through when we look at our timestamps, and we can
see that we've got a last access time,
a last modified time, and a last change time.
There's also a birth time, the file creation.
This is supported on some filesystems, but not all filesystems. Now from
the command line, I can use Ctrl+L to clear my screen.
If I go back, then and look at stat and our good friend file1, we
can see, of course, then the access times, modified times, that
haven't changed. Everything is the same,
the birth time, all of it relates to exactly the same time because
we haven't really accessed this file separately.
However, even though there is no content in the file,
I can still access the file and try and read it, and I
can use the cat command for this.
So if I go through and use the cat command against file1,
we will have changed the last access time.
So I can then go back and look at stat file1, and we can see now it shows
at 12:59 where all of the other elements are at 12:56.
So we can start seeing the access time, then can be read independently
and shows when the file was last read. We're not making any changes to
the file, so we see all of the other times show the same. If I now write
to the file content, so echo hello, and append it to file1, now when I go
through and take a look,
we haven't accessed the file, so we haven't read it, but we have modified it.
We modified it at 13:00.
But in modifying the file though, we also changed the
file size, so our metadata changed.
So we can see that the change data also shows as being exactly the same time.
If I change the permissions, and the permissions of a file is known
as the mode of the file, so chmod 770 for file1.
Don't worry so much about the mode that we're setting at the moment, but
now go through and take a look at stat file1, we can see the metadata
changed at 13:01, but nothing else changed.
We only changed the file metadata, so the last changed timestamp was modified.
Now using the stat command, we are also able to go through and pull out
individual elements of our metadata, so it's stat ‑c.
And then we have various fields.
The last access time is %x, so when the file was last read. Using
%y, we can look at the last modified time, so when the file was
last changed. And then using %z, we see the last change time when
the metadata was last changed.
Now the metadata is all part of our file.
If I go through and make a copy though, we are making a new file.
So if I use cp file1 through to file2, file2 is a completely new file.
The metadata will not have been maintained.
So when I go through and take a look at file2, we can see all
of the timestamps are relating to 13:01.
If it's important that the metadata, the timestamps, are maintained,
then we can use cp ‑a. And when we look at file3 now, we can see that
the timestamps have been maintained. As we go through and take a look at
those individual elements, we have changed the metadata, so that shows
at 13:02. It is a new file at 13:02, but the access and modified times
are as previously set.
Now having used the cp command, that's making a copy.
The mv command is used to move, which is renaming or moving a file.
So if I rename file1 to be file4, mv file1 file4, when I
then go through and do an ls file*, we see I no longer have file1.
It doesn't exist anymore.
When I go through and take a look at the stat on file4, we can
go through and start seeing that some of the data has been
updated, the change, because again, the metadata has changed.
We've changed the file name, but none of the other
data has changed. Now, of course, working with this metadata,
it's not just files that we create.
If I go through a new cp ‑a, our archive mode, and take a look at copying
/etc/hosts through to dot my current directory, and then go through and
take a look at stat on hosts, we can go through that the access time is
12:55. The modified time is 12:55.
The metadata, of course, has changed now, and then the file
birth time now shows as being created now,
but we've maintained the original access and modified time.
So hopefully, this introduces to you a little bit about our metadata and some of the tools that we can use to view and manage our metadata.