Tuesday, April 24, 2018

9 Useful touch command examples in Linux

9 Useful touch command examples in Linux

Touch command is employed to make empty files and conjointly changes the timestamps of existing files on Unix & Linux System. dynamical timestamps here suggests that change the access and modification time of files and directories.

Let’s have a glance on the syntax and choices employed in touch command,

Syntax: # touch {options} {file}

Options employed in touch command,

9 Useful touch command examples in Linux

1. Create an empty file using touch

To create associate empty file victimization bit command on UNIX system systems, sort bit followed by the file name, example is shown below,

9 Useful touch command examples in Linux

2. Create empty files in bulk using touch

There is some state of affairs wherever we've got to make scores of empty files for a few testing, this could be simply achieved victimization bit command,

9 Useful touch command examples in Linux

In the on top of example we've got created twenty empty files with name sysadm-1.txt to sysadm-20.txt, you'll be able to amendment the name and numbers supported your needs.

3. Change / Update access time of a file and directory

Let’s assume we wish to alter interval of a file referred to as “devops.txt“, to try and do this use ‘-a‘ possibility connected command followed by file name, example is shown below,

9 Useful touch command examples in Linux

Now verify whether or not interval of a file has been updated or not victimization ‘stat’ command

9 Useful touch command examples in Linux

Change access time of a directory

Let’s assume we've got a ‘nfsshare’ folder underneath /mnt, Let’s amendment the interval of this folder victimization the below command,

9 Useful touch command examples in Linux

4. Change Access time without creating new file

There is some things wherever we wish to alter interval of a file if it exists and avoid making the file. victimization ‘-c‘ possibility connected command, we are able to amendment interval of a file if it exists and can not a produce a file, if it doesn’t exist.


9 Useful touch command examples in Linux

5. Change Modification time of a file and directory

Using ‘-m‘ possibility connected command, we are able to amendment the modification time of a file and directory,

Let’s amendment the modification time of a file referred to as “devops.txt”,

9 Useful touch command examples in Linux

Now verify whether or not modification time has been modified or not victimization stat command,

9 Useful touch command examples in Linux

Similarly, we are able to amendment modification time of a directory,

9 Useful touch command examples in Linux

6. Changing access and modification time in one go

Use “-am” possibility connected command to alter the access and modification along or in one go, example is shown below,

9 Useful touch command examples in Linux

Cross verify the access and modification time victimization stat,

9 Useful touch command examples in Linux


7. Set the Access & modification time to a specific date and time

Whenever we have a tendency to do amendment access and modification time of a file & directory victimization bit command, then it set this time as access & modification time of that file or directory,

Let’s assume we wish to line specific date and time as access & modification time of a file, this is can be achieved victimization ‘-c’ & ‘-t’ possibility connected command,

Date and Time can be specified in the format: {CCYY}MMDDhhmm.ss

Where:

CC – First two digits of a year
YY – Second two digits of a year
MM –  Month of the Year (01-12)
DD – Day of the Month (01-31)
hh –  Hour of the day (00-23)
mm – Minutes of the hour (00-59)

Let’s set the access & modification time of devops.txt file for future date and time( 2025 year, 10th Month, nineteenth day of month, eighteenth hours and twentieth minute)

9 Useful touch command examples in Linux

Use stat command to look at the update access & modification time,

9 Useful touch command examples in Linux


Set the Access and Modification time supported date string, Use ‘-d’ possibility connected command then specify the date string followed by the file name, example is shown below,

9 Useful touch command examples in Linux

Verify the standing victimization stat command,

9 Useful touch command examples in Linux

Note: In on top of commands, if we have a tendency to don’t specify ‘-c’ then bit command can produce a brand new get into case it doesn’t exist on the system and can set the timestamps no matter is mentioned within the command.

8. Set the timestamps to a file using a reference file (-r)

In touch command we will use a reference file for setting the timestamps of file or directory. Let’s assume i would like to line an equivalent timestamps of file “sysadm-20.txt” on “devops.txt” file. this will be simply achieved victimization ‘-r’ choice involved.

Syntax: # touch -r {reference-file} actual-file

9 Useful touch command examples in Linux

9. Change Access & Modification time on symbolic link file

By default, whenever we tend to attempt to modification timestamps of a symbolic link file victimization bit command then it'll modification the timestamps of original file solely, just in case you would like to alter timestamps of a symbolic link file then this will be achieved victimization ‘-h’ choice involved command,

Syntax: # touch -h {symbolic link file}

9 Useful touch command examples in Linux

NEXT ARTICLE Next Post
PREVIOUS ARTICLE Previous Post
NEXT ARTICLE Next Post
PREVIOUS ARTICLE Previous Post