LearnLinux FoundationsManipulation

touch

touch - a hands-on Linux lab on a real virtual machine.

Make a file exist from nothing with touch. It creates empty files, builds several at once, and updates the timestamp on files that already exist. touch is silent on success, so every drill proves the result with ls or ls -l. Learn -c to avoid creating by accident and -r to copy another file's time, plus the two errors you will meet.

You are about to write a script, keep some notes, or save a bit of config. Every one of those needs a file to write into. But right now that file does not exist. So how do you make an empty file appear, out of nothing, before you have a single word to put in it?

You do not open an editor. You do not type any content. There is one command whose only job is to make a file spring into being, empty and ready. It is called touch, and it is the first move of almost everything you will build on a Linux machine.

The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. Here is the twist with touch: on success it prints nothing at all. A blank line and a fresh prompt is the command working perfectly. To see what it did, you run a second command right after, ls or ls -l, and read the file it left behind.

touch has two jobs. If the file you name does not exist, touch creates it, empty. If the file already exists, touch leaves the contents alone and just updates its timestamp, the record of when the file was last changed. One command, two outcomes, decided by whether the file is already there.

The name is the clue. You are not filling the file or editing it. You are just reaching out and touching it, the lightest possible action: bring it into existence, or mark it as touched just now. Nothing inside ever changes.

The name comes from that second job. Long before it was a handy way to make empty files, engineers used touch to update a file's modified time. That made build tools treat the file as fresh and rebuild it. Making empty files was almost a side effect that turned out to be the part everyone uses today.

Start with the whole point of the command. You will name a file that does not exist, notes.txt, and touch will create it. Remember: touch says nothing when it works. So the first line looks like nothing happened.

The proof comes on the second line. ls -l lists a file the long way, with its size and details, so you can see the empty file touch just made. Before you run it, decide what you expect: a brand new file, and because you put nothing in it, a size of 0.

touch notes.txt

ls -l notes.txt

prompt: student@linuxcamp:~$ answer: touch notes.txt output: hint: Type touch, a space, then the filename: touch notes.txt. It prints nothing, and that is success.

prompt: student@linuxcamp:~$ answer: ls -l notes.txt output: -rw-r--r-- 1 student student 0 Jul 5 10:46 notes.txt hint: Type ls, a space, the -l flag, then the same filename: ls -l notes.txt

The first command printed nothing, and that silence WAS the success. The second command is where you see the result: a file named notes.txt, and right before its name, a single 0. That 0 is the size in bytes, and it confirms the file is completely empty, exactly what touch promises. The Jul 5 10:46 is the timestamp, the moment the file was created. On your machine that date and clock will read differently, because it stamps the real time you ran it. What is identical everywhere is the shape: the file exists, and its size is 0.

touch does not stop at one. Hand it several names in a row, separated by spaces, and it creates every one of them in a single command. This is the everyday way engineers rough out a set of files before filling any of them.

Create three at once, then list the folder with a plain ls to see all of them. ls on its own prints just the names, which is perfect when you only want to confirm they are all there.

touch a.txt b.txt c.txt

ls

prompt: student@linuxcamp:~$ answer: touch a.txt b.txt c.txt output: hint: List all three names after touch, each separated by a space: touch a.txt b.txt c.txt

prompt: student@linuxcamp:~$ answer: ls output: a.txt b.txt c.txt notes.txt hint: Just the two letters and Enter: ls

One command, three new files. touch walked the list of names left to right and created each one, all empty, all in a single line. The ls afterward shows all four files now in the folder: the three you just made, plus the notes.txt from before. This is the pattern to remember: touch takes as many names as you give it, so scaffolding out a batch of files is one command, not one command per file.

So far every file was new. Now aim touch at a file that already exists. This time it does NOT create anything and it does NOT touch the contents. It only updates the timestamp to right now, marking the file as freshly touched.

You cannot see a timestamp change with a plain ls, so ls -l is the tool again: it prints the time, so you can watch it move. Re-touch the notes.txt you made earlier, then list it long.

touch notes.txt

ls -l notes.txt

prompt: student@linuxcamp:~$ answer: touch notes.txt output: hint: Same command as when you created it, aimed at the file that is already there: touch notes.txt

prompt: student@linuxcamp:~$ answer: ls -l notes.txt output: -rw-r--r-- 1 student student 0 Jul 5 10:52 notes.txt hint: List it the long way to read the new time: ls -l notes.txt

Same file, same 0 bytes, but a later time than when you first made it. touch did not create a second notes.txt and it did not add a single byte inside. It only reached out and updated the modified time to the moment you ran it. The size stayed 0 because the contents never changed. Your exact clock will differ, but the lesson holds everywhere: on a file that already exists, touch moves the timestamp and leaves everything else alone.

Sometimes you only want to update a file's time IF it already exists, and you do not want touch quietly creating it when it does not. The -c flag does exactly that. -c stands for no-create: if the file is missing, touch -c does nothing and stays silent, instead of making a new empty file.

Prove it on a name that does not exist, ghost.txt. Run touch -c on it, which will do nothing, then try to list that name. The ls will fail, and that failure is the proof no file was created.

touch -c ghost.txt

ls ghost.txt

prompt: student@linuxcamp:~$ answer: touch -c ghost.txt output: hint: Add the -c flag between touch and the name: touch -c ghost.txt

prompt: student@linuxcamp:~$ answer: ls ghost.txt output: ls: cannot access 'ghost.txt': No such file or directory hint: Try to list the name you just touched with -c: ls ghost.txt

The touch -c printed nothing, just like always. But this time the silence means it deliberately did nothing: the file did not exist, and -c told it not to create one. The ls proves it, complaining No such file or directory, because there is no ghost.txt to list. Without -c, that first command would have created an empty ghost.txt and the ls would have succeeded. Reach for -c when creating the file by accident would be worse than doing nothing.

One more flag worth knowing. Sometimes you want a file to carry the EXACT same timestamp as another file, not the current time. The -r flag does that. -r stands for reference: touch -r reffile target sets target's time to match reffile's time, whatever that is.

You will use two files you already have. Set the time of a.txt to match notes.txt, then list both the long way to compare their times side by side.

touch -r notes.txt a.txt

ls -l notes.txt a.txt

prompt: student@linuxcamp:~$ answer: touch -r notes.txt a.txt output: hint: -r takes the reference file first, then the file to change: touch -r notes.txt a.txt

prompt: student@linuxcamp:~$ answer: ls -l notes.txt a.txt output: -rw-r--r-- 1 student student 0 Jul 5 10:52 a.txt -rw-r--r-- 1 student student 0 Jul 5 10:52 notes.txt hint: List both names after ls -l, separated by a space: ls -l notes.txt a.txt

Both files now show the same time. touch -r notes.txt a.txt read the timestamp off notes.txt, the reference, and stamped it onto a.txt, so the two now match to the minute. The order matters: the reference file comes right after -r, and the file being changed comes last. Your exact clock will differ from this capture, but the two lines will always share the same time, because you copied one onto the other.

Scaffolding off. No command is printed this time. You have every piece you need.

You need two brand new empty files, draft.txt and final.txt, and you want both created in a single command, not one at a time. Make them both exist.

prompt: student@linuxcamp:~$ answer: touch draft.txt final.txt output: hint: The command that makes empty files takes more than one name. List both, separated by a space, after it.

prompt: student@linuxcamp:~$ answer: ls output: a.txt b.txt c.txt draft.txt final.txt notes.txt hint: List the folder to confirm both new names are there: ls

Both files appear in the listing, made in one command. You recalled that touch accepts a whole list of names and creates every one, so touch draft.txt final.txt built both at once, empty and ready. That is the move a working engineer makes to rough out a set of files before writing a line into any of them. You typed it from memory, which is exactly the recall the real machine will ask of you.

touch is simple, but two failures come up often. Each prints a clear message. Learn to read them and you will fix them in seconds.

Permission denied. Try to create a file in a folder you do not own, like the system's /etc, and touch refuses. The message reads:

touch: cannot touch '/etc/x': Permission denied

It means your user is not allowed to write in that folder. Check first WHERE you are trying to create the file. System folders like /etc are owned by the administrator. Make your file somewhere you own, such as your home folder. If the task genuinely needs a system folder, that calls for sudo, a later lesson.

No such file or directory. Name a file inside a folder that does not exist, and touch cannot make it. The message reads:

touch: setting times of 'x': No such file or directory

It means the PATH leading to your file is broken. A folder in the middle is missing, so there is nowhere to put the file. touch creates files, never the folders above them. Check first that every folder in the path already exists. You make folders with mkdir, a lesson of its own.

Both messages start with touch: so you know which command spoke, then name the exact file that failed inside quotes. When touch fails, read the file name it prints back: it is almost always a wrong path or a folder you cannot write to, not a problem with touch itself.

You earned this cheat sheet. Every row is a form of touch you just ran:

The one idea under all of it: touch is silent on success, so you never read its result from touch itself. You run ls or ls -l right after and read the file it left behind. A new name, a 0 size, an updated time: that is touch working.

Two more flags exist for finer control. -a updates only the access time, -m updates only the modified time, and -t lets you set an explicit time by hand instead of using now. You will rarely need them as a beginner, but now you know they are there when a task calls for that precision.

The practice terminal has shown you the whole of touch. It makes empty files from nothing, creates many at once, updates the time on files that already exist, and stays silent every time it works. You proved each one with ls. The -c flag keeps it from creating by accident, and -r copies a time from another file. Every one of those you typed yourself.

The Manipulation module ends with one real Linux machine, the module capstone mission, and that is where you make files for real. A VM boots just for you, with a live shell and a real disk. It hands you objectives that use exactly what you practiced across this module: making, copying, moving, and linking files. One difference from here: the mission shows no commands. You read the objective, you recall the command, you type it. That recall is what makes it stick.

Finish the other Manipulation lessons, then go make a file exist on a real machine.

Practice touch in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.