Using the touch command in Linux

Using the touch command in Linux

As you know, Linux operating systems have a large number of built-in terminal commands that perform a wide variety of actions. Some of them allow you to install programs, others are to manage logical volumes and hard drives. Among them are those that are designed to interact with files. One of these commands is called touch , and that's what we want to talk about in this tutorial.

Using the touch command in Linux

To use the command touch On Linux, you must learn its syntax and understand the principles of writing. There should be no problem with this, as the utility itself is quite simple and the available options can be handled in just a few minutes. Let's start with this.

Syntax

Let's pay attention to the standard shape of the line when entering the command tactile . It looks like this: touch + [Опции] + Файл. If the action is to be done on behalf of the super, you will need to add sudo at the beginning of the line and, after activating it, enter a that confirms the . As for the additional options, the following should be highlighted:

  • -Aid and self they are rarely used. The first option will give the opportunity to read the official documentation and the second will show the current version of the utility.
  • -a is responsible for changing the access time to the specified file.
  • -m change the modification time.
  • -c specifies that no object with the specified name will be created.
  • -r will use the access and modification times of the specified file.
  • -t is to change the date and time by manual input.
  • -d uses the specified date and time as a string.

Now you know absolutely all the available options of the command that we are considering today. Let's move on to examine the parameters to understand all the basic actions performed with this utility.

Generating empty files

First, let's find out the action of the command tactile without using any arguments; this is how it creates an empty 0 byte file with the specified name.

    1. Open the "Terminal" in a way convenient for you, for example, via the icon in the application menu or the Ctrl+T+Alt key combination.
    1. Enter here touch testfile, where replace test file with the desired name.
    2. After activating this command, if it ed without errors, a new input line will appear and the corresponding object will be created at the current location.
    3. You can add multiple files at once, taking turns typing the name of each one, to get something like this line: touch testfile1 testfile2 testfile3.
    4. There is a feature that must also be taken into . If there was a need to create multiple files with the same name but with different numbers at the end, as demonstrated above, it is easier to use this type of script: touch testfile{1..6}.

More command tactile without using arguments it is not able to do anything, so let's go directly to the analysis of examples of interaction with options.

Last access time settings

As you already know, one of the options of the command in question allows you to change the access time to the file to the current one. This is done by entering just one line, which looks like touch -a fileWhere archive is the name of the required object. There is no limit to the number of items listed for a line. In this case, the time of the last change is not set, unless se add additional option -m to this line, we will talk more about it.

Last modification time settings

The argument -m above works the same way. Th remaps the last changes to the stream and the line looks like this: touch -m file. Any changes you make will take effect immediately, which means you can move on to checking them or performing other tasks for which the command was called tactile with the option -m .

Prohibition of the creation of objects

Utility tactile Simple sometimes allows you to achieve a complex goal just by typing a line to the console. After running the command touch -c fileWhere archive is the exact name of the required file, a normal cannot create an item with the specified name. This option is disabled only after the privileged creates an empty object with the same name through the same command. Also, nothing prevents you from creating a list of names to place restrictions on them simultaneously.

Configuration of access and change times

The options -a y -m above They only allowed to change the parameters of the file by setting the current time, but it is possible to set absolutely any time with a precision of one second. In this case, the main thing is to follow the entry rule: [[ВВ]ГГ]ММДДччммм[.сс]where BB - the first two digits of the year, YY - the second, MM - month, DD - date, hh - hours, mm - minutes, ss - seconds. Equipment needed to achieve this: touch -c -t 01261036 file.

If you are interested in seeing the final result, type in the console ls -land press Enter . The only thing left on the list is to find the required file and see when it was modified.

Transfer the timestamps of the selected file

If you read the information above, you will know that the option -r will allow you to transfer timestamps from one object to another. This is done through the line:, touch -r file1 file2where file1 is an existing file with certain timestamps and file2 is a new object to which they will be applied.

Create file with specified time

At the end of this material, we note that by default touch creates files with current time, however this can be changed by applying only one option:, touch -t 201912101830.55 filewhere 201912101830.55 is the exact time of your choice, and archive is the name of the same object or objects, if any, presented as a list.

You are now familiar with the command touch , which is widely used in Linux to create files. These can be separate test items or objects added for specific purposes. The himself decides in which direction to use the utility's capabilities. If you are interested in the topic of the main commands of this operating system, we suggest you study the materials presented below.