Conquer The Command Line: The touch Command

Jim Homme
3 min readDec 18, 2020

How to use the Linux touch command plus essential options

In this installment of “Conquer The Command Line” we talk about how to use the Linux touch command.

When ever you want to change the time stamp on an existing file or create an empty file, use the touch command.

Memory Aid: Think “wake up”

OK. I need to explain that one. Thinking of waking up works for me because if your sleeping and someone touches you, you might wake up if you are a light sleeper. In my mind, waking up in the morning starts a new day. My mind jumps to touching a file creates it. I wonder how touch meant create. Oh well. There it is. A look into my crazy mind.

To use the touch command, type touch <file> where is the name of any file, including path.

Touch does one of two things. If the file hasn’t been created, it creates it and makes it empty. If the file already exists, it changes the time stamp to the current time.

Let’s say you have a file named recipe.txt that doesn’t exist that you want to create. To do that, type touch recipe.txt.

If recipe.txt already exists, and you want to change the time stamp to the current tyme, you would also type touch recipe.txt.

Note, you can also type more than one file on the command line after typing touch. That means that if you type touch recipe.txt cake.txt, touch would act on those two files. It would either create one or both of them or change the time stamp on one or both of them.

Essential Options

-t set the time stamp on a file to any time you want. For example: touch -t <YYYYMMDDHHMM.SS> recipe.txt.

YYYY sets the year. MM sets the month. DD sets the day. HH sets the hour from 1 to 23. MM.SS sets the minute from 1 to 59 and the second from 1 to 59.

-r makes touch use the time stamp on one file to set the time stamp on a second file.

Let’s say that you want to set the time stamp on the cake.txt file to the time stamp on the recipe.txt file. type touch -r recipe.txt cake.txt.

Related Commands

To print the working directory, the one you are currently pointing to, use the pwd command.

To change to directories, use the cd command.

To list the contents of a directory, use the ls command.

To make directories, use the mkdir command.

To remove directories, use the rmdir command.

To move or rename files and directories, use the mv command.

To copy files and directories, use the cp command.

Remember to use the man command to find all the options for any command on your system.

And remember that for a less technical explanation of commands to check out tldr pages.

What Now?

Hopefully this post has helped you take one more step in conquering the command line. I welcome any feedback.

This post was originally written at https://www.jimhomme.com

--

--

Jim Homme

Musician, father of three, husband. Information Technology veteran. Bookworm. Chess player. Accessibility consultant. Plain language advocate