How To Use touch Command in Linux with Examples?

Posted in

How To Use touch Command in Linux with Examples?
sangeeta.gulia

Sangeeta Gulia
Last updated on April 20, 2024

    Using the command line to create files and manage their timestamps is a common task in Linux. To do this, Linux provides a number of command-line utilities. We'll go over how to accomplish this task with the touch command in this article.

    touch Command in Linux

    The touch command is a typical command in the UNIX/Linux operating system for creating, changing, and modifying file timestamps. The touch command is a typical command in the UNIX/Linux operating system for creating, changing, and modifying file timestamps. The pwd command may be used to verify that we are in the home directory. The command ls is used to check for existent files, and then the long listing command (ll) is used to get further information about such files.

    The syntax for the touch command is:

    touch <options> <file or directory name>

    1. touch -a

    This command is only for changing the access time. The touch - a command is used to alter or update a file's last access or modification times. The syntax is:

    $ touch -a fileName

    2. touch -c

    This command is used to determine whether or not a file has been created. If it hasn't been created, don't make it. This command prevents the creation of files. The syntax is:

    $ touch -c fileName

    3. touch -c-d

    This is used to update access and modification time.The syntax is:

    $ touch -c-d fileName

    4. touch -m

    This is solely used to modify the modification time. It just updates the time of the most recent change. The syntax is:

    $ touch -m fileName

    5. touch -r

    The timestamp of one file can be changed with respect to the other file’s timestamp using this option. The syntax is:

    $ touch -r reference_file file

    6. touch -t

    This command is used to set a file with a certain timestamp. The syntax is:

    $ touch -t <timestamp> fileName

    The timestamp format adheres to a set of rules:

    [[CC]YY]MMDDhhmm[.ss]

    CC - the first two digits for a year

    YY - the last two digits for a year

    MM - the month

    DD - the day

    hh - the hour

    mm - the minutes

    ss - the seconds

    7. touch -at

    By using the - a and - t arguments, you can change the access time to a given timestamp. The syntax is:

    $ touch -at <timestamp> <filename>

    8. touch -mt

    Combine the - m and - t options to provide the modification timestamp explicitly. The syntax is:

    $ touch -mt <timestamp> <filename>

    9. touch -h

    The touch command allows you to change the timestamp of symbolic links without affecting the timestamp of the linked file. To change the time for a symbolic link, use the - h option. The syntax is:

    $ touch -h <filename>

    Conclusion

    In this article, we went through various options available with the touch command of Linux. We saw how it could be used to perform tasks like file creation, checking file creation, changing and setting timestamps and modification times, etc.

    People are also reading:

    Leave a Comment on this Post

    0 Comments