How to use touch command on Linux to create empty files and update timestamps

11 set 2024 2 min di lettura
How to use touch command on Linux to create empty files and update timestamps
Indice dei contenuti

Introduction

In Linux, efficient file and folder management is essential to ensure data integrity and continuity. One of the most basic yet essential tools for this purpose is the touch command. This tutorial will walk you through the essential steps to use touch to create empty files and update timestamps of existing files. You will learn how to configure and use touch effectively, with practical examples and code snippets.

What is touch ?

touch is a Linux utility command that allows you to create empty files and update the timestamps (access and modification dates) of existing files. Unlike other tools, touch is known for its simplicity and its ability to handle both file creation and timestamp updating. This makes it ideal for basic tasks such as creating text files, managing timestamps, and preparing files for scripts or programs.

Using touch Base

The touch command has a relatively simple syntax. Here is a basic example to create an empty file:

touch nome_del_file

Practical Example: Creating an Empty File

Suppose we want to create an empty file called nuovo_file.txt in the current folder. Here's how to do it:

touch nuovo_file.txt

Update Timestamps of an Existing File

If the specified file already exists, touch updates the file's access and modification timestamps. This can be useful to indicate that the file has been recently accessed or modified.

Practical Example: Updating the Timestamps of an Existing File

Suppose we want to update the timestamps of an existing file called documento.txt. Here's how to do it:

touch documento.txt

Create Files in Different Folders

You can also use touch to create files in different folders by specifying the full path to the file.

Practical Example: Creating a File in a Different Folder

Suppose we want to create an empty file called nuovo_file.txt in the /home/utente/Documenti folder. Here's how to do it:

touch /home/utente/Documenti/nuovo_file.txt

Create Multiple Files at Once

touch also allows you to create multiple files at once by specifying file names separated by spaces.

Practical Example: Creating Multiple Files at Once

Suppose we want to create three empty files called file1.txt, file2.txt, and file3.txt. Here's how to do it:

touch file1.txt file2.txt file3.txt

Update Only the Edit Timestamp

If you want to update only the modification timestamp of an existing file, you can use the -m option.

Practical Example: Updating Only the Modification Timestamp

Suppose we want to update only the modification timestamp of an existing file called documento.txt. Here's how to do it:

touch -m documento.txt

Update Login Timestamp Only

Similarly, if you only want to update the access timestamp of an existing file, you can use the -a option.

Practical Example: Update Only the Access Timestamp

Suppose we want to update only the access timestamp of an existing file called documento.txt. Here's how to do it:

touch -a documento.txt

Conclusion

touch is an essential tool for anyone working with files and folders on Linux. Because of its simplicity and versatility, touch has become a standard for creating empty files and managing timestamps. By following this tutorial, you should be able to use touch to handle your file creation and update needs effectively and safely.

Always remember to test commands in a safe environment before applying them in production, and to be careful about paths and options used to avoid creating unwanted files. With touch, managing your files and folders becomes a simple and reliable operation.

Support us with a

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.