Let's do a long listing for all the files in /home/elliot one more time to discuss something very important:
elliot@ubuntu-linux:~$ ls -a -l /home/elliot
total 28
drwxr-xr-x 3 elliot elliot 4096 Jan 20 16:26 .
drwxr-xr-x 9 root root 4096 Jan 17 04:37 ..
-rw------- 1 elliot elliot 90 Jan 20 13:43 .bash_history
-rw-r--r-- 1 elliot elliot 220 Dec 26 23:47 .bash_logout
-rw-r--r-- 1 elliot elliot 3771 Dec 26 23:47 .bashrc
drwxr-xr-x 2 elliot elliot 4096 Jan 19 14:20 Desktop
-rw-r--r-- 1 elliot elliot 807 Apr 4 2018 .profile
Focus on the last two columns of the output:
Jan 20 16:26 | . |
Jan 17 04:37 | .. |
Jan 20 13:43 | .bash_history |
Dec 26 23:47 | .bash_logout |
Dec 26 23:47 | .bashrc |
Jan 19 14:20 | Desktop |
Apr 4 2018 | .profile |
You already know that the last column of the output (2nd column of Table 3) shows the filenames, but what about all these dates that are displayed in the preceding column (1st column of Table 3)?
The dates in the first column of Table 3 represent the last modification time of each file, which is the last time a file was modified (edited).
You can use the touch command to change the modification time of a file.
To demonstrate, let's first get the modification time on elliot's Desktop directory, you can do that by running the ls -l -d /home/elliot/Desktop command:
elliot@ubuntu-linux:~$ ls -l -d /home/elliot/Desktop
drwxr-xr-x 2 elliot elliot 4096 Jan 19 14:20 /home/elliot/Desktop
Notice we used the -d option, so it does a long listing on the directory /home/elliot/Desktop instead of listing the contents of the directory.
The last modification time is shown to be: Jan 19 14:20.
Now if you run the touch /home/elliot/Desktop command:
elliot@ubuntu-linux:~$ touch /home/elliot/Desktop
elliot@ubuntu-linux:~$ ls -l -d /home/elliot/Desktop
drwxr-xr-x 2 elliot elliot 4096 Jan 20 19:42 /home/elliot/Desktop
elliot@ubuntu-linux:~$ date
Sun Jan 20 19:42:08 CST 2020
You will see that the last modification time of the directory /home/elliot/Desktop has now changed to Jan 20 19:42, which reflects the current time.
Of course, you will get a different result on your system because you will not be running the command at the same time as me.
Ok, great, so now we understand that the touch command can be used to update a file's modification time. Can it do something else? Hmmm, let's see.
What if we try to update the modification time of a file that doesn't exist? What will happen? The only way to know is to try it. Notice that user elliot has only one visible (not hidden) file in his home directory, which happens to be the Desktop directory:
elliot@ubuntu-linux:~$ pwd
/home/elliot
elliot@ubuntu-linux:~$ ls -l
total 4
drwxr-xr-x 2 elliot elliot 4096 Jan 20 19:42 Desktop
Now watch what will happen when user elliot runs the touch blabla command:
elliot@ubuntu-linux:~$ touch blabla
elliot@ubuntu-linux:~$ ls -l
total 4
-rw-r--r-- 1 elliot elliot 0 Jan 20 20:00 blabla
drwxr-xr-x 2 elliot elliot 4096 Jan 20 19:42 Desktop
It created an empty file named blabla.
You can do two things with the touch command:
- You can update the last modification and access times of existing files.
- You can create new empty files.
The touch command can only create regular files; it cannot create directories. Also, notice that it updates modification and access times, so what is the difference?
- Modification Time > Last time a file was changed or modified.
- Access Time > Last time a file was accessed (read).
By default, the touch command changes both the modification and access times of a file. I have created three files in elliot's home directory: file1, file2, and file3:
elliot@ubuntu-linux:~$ ls -l
total 8
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
drwxr-xr-x 3 elliot elliot 4096 Jan 25 22:18 dir1
-rw-r--r-- 1 elliot elliot 0 Feb 29 2004 file1
-rw-r--r-- 1 elliot elliot 0 Apr 11 2010 file2
-rw-r--r-- 1 elliot elliot 0 Oct 3 1998 file3
Now to change only the modification time of file1. We pass the -m option to the touch command:
elliot@ubuntu-linux:~$ touch -m file1
elliot@ubuntu-linux:~$ ls -l
total 8
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
drwxr-xr-x 3 elliot elliot 4096 Jan 25 22:18 dir1
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:08 file1
-rw-r--r-- 1 elliot elliot 0 Apr 11 2010 file2
-rw-r--r-- 1 elliot elliot 0 Oct 3 1998 file3
elliot@ubuntu-linux:~$
As you can see, the modification time of file1 has now changed. I promised you I would only change the modification time, right? If you pass the -u option along with the -l option to the ls command, you will get the last access times instead of the modification times:
elliot@ubuntu-linux:~$ ls -l
total 8
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
drwxr-xr-x 3 elliot elliot 4096 Jan 25 22:18 dir1
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:08 file1
-rw-r--r-- 1 elliot elliot 0 Apr 11 2010 file2
-rw-r--r-- 1 elliot elliot 0 Oct 3 1998 file3
elliot@ubuntu-linux:~$ ls -l -u
total 8
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
drwxr-xr-x 3 elliot elliot 4096 Jan 25 22:18 dir1
-rw-r--r-- 1 elliot elliot 0 Feb 29 2004 file1
-rw-r--r-- 1 elliot elliot 0 Apr 11 2010 file2
-rw-r--r-- 1 elliot elliot 0 Oct 3 1998 file3
As you can see, the last modification time of file1 is changed to Jan 25 23:08, but the access time is left unchanged: Feb 29 2004. Now this time around, let's only change the access time of file2. To do this, we pass the -a option to the touch command:
elliot@ubuntu-linux:~$ touch -a file2
elliot@ubuntu-linux:~$ ls -l
total 8
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
drwxr-xr-x 3 elliot elliot 4096 Jan 25 22:18 dir1
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:08 file1
-rw-r--r-- 1 elliot elliot 0 Apr 11 2010 file2
-rw-r--r-- 1 elliot elliot 0 Oct 3 1998 file3
elliot@ubuntu-linux:~$ ls -l -u
total 8
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
drwxr-xr-x 3 elliot elliot 4096 Jan 25 22:18 dir1
-rw-r--r-- 1 elliot elliot 0 Feb 29 2004 file1
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:20 file2
-rw-r--r-- 1 elliot elliot 0 Oct 3 1998 file3
elliot@ubuntu-linux:~$
As you can see, the modification time of file2 was left unchanged, but the access time is changed to the current time. Now to change both the modification and access times of file3, you can run the touch command with no options:
elliot@ubuntu-linux:~$ ls -l file3
-rw-r--r-- 1 elliot elliot 0 Oct 3 1998 file3
elliot@ubuntu-linux:~$ touch file3
elliot@ubuntu-linux:~$ ls -l file3
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:27 file3
elliot@ubuntu-linux:~$ ls -l -u file3
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:27 file3
Awesome! You can also pass the -t option to the ls command to list the files sorted by modification times, newest first:
elliot@ubuntu-linux:~$ ls -l -t
total 8
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:27 file3
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:08 file1
drwxr-xr-x 3 elliot elliot 4096 Jan 25 22:18 dir1
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
-rw-r--r-- 1 elliot elliot 0 Apr 11 2010 file2
You can add the -u option to sort by access times instead:
elliot@ubuntu-linux:~$ ls -l -t -u
total 8
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:27 file3
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:20 file2
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:20 file1
drwxr-xr-x 3 elliot elliot 4096 Jan 25 22:18 dir1
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
You can also pass the -r option to reverse the sorting:
elliot@ubuntu-linux:~$ ls -l -t -r
total 8
-rw-r--r-- 1 elliot elliot 0 Apr 11 2010 file2
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
drwxr-xr-x 3 elliot elliot 4096 Jan 25 22:18 dir1
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:08 file1
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:27 file3