Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn Linux Quickly

You're reading from   Learn Linux Quickly A beginner-friendly guide to getting up and running with the world's most powerful operating system

Arrow left icon
Product type Paperback
Published in Aug 2020
Publisher Packt
ISBN-13 9781800566002
Length 338 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Ahmed AlKabary Ahmed AlKabary
Author Profile Icon Ahmed AlKabary
Ahmed AlKabary
Arrow right icon
View More author details
Toc

Table of Contents (24) Chapters Close

Preface 1. Your First Keystrokes 2. Climbing the Tree FREE CHAPTER 3. Meet the Editors 4. Copying, Moving, and Deleting Files 5. Read Your Manuals! 6. Hard versus Soft Links 7. Who Is Root? 8. Controlling the Population 9. Piping and I/O Redirection 10. Analyzing and Manipulating Files 11. Let's Play Find and Seek 12. You Got a Package 13. Kill the Process 14. The Power of Sudo 15. What's Wrong with the Network? 16. Bash Scripting Is Fun 17. You Need a Cron Job 18. Archiving and Compressing Files 19. Create Your Own Commands 20. Everyone Needs Disk Space 21. echo "Goodbye My Friend" 22. Assessments 23. Other Books You May Enjoy

The touch command

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
Table 3: Last Two Columns of ls -a -l /home/elliot

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:

  1. You can update the last modification and access times of existing files.
  2. 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
You have been reading a chapter from
Learn Linux Quickly
Published in: Aug 2020
Publisher: Packt
ISBN-13: 9781800566002
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image