5 Linux Touch Command Examples (How to Change File Timestamp)

Every file in Linux is associated with timestamps, which specifies the last access time, last modification time and last change time.

Whenever we create a new file, or modify an existing file or its attributes, these timestamps will be updated automatically.

Touch command is used to change these timestamps (access time, modification time, and change time of a file).

1. Create an Empty File using touch

You can create an empty file using touch command. The following example will create a zero byte new file named tgs.txt.

$ touch tgs.txt

You can also use -c option to avoid creating new files. If you use -c option, and if a file doesn’t exists, touch will not create the file.

$ touch -c a.txt

Commands like ls command and find command uses these timestamp information for listing and finding files.

You can also create more than 1 files from a single touch command. The following example will create 4 files named a, b, c, and d.

$ touch a b c d

2. Change File’s Access Time using -a

We can change the access time of a file using -a option. By default it will take the current system time and update the atime field.

Before touch command is executed:

$ stat tgs.txt

  File: `tgs.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 394283      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/lakshmanan)   Gid: ( 1000/lakshmanan)
Access: 2012-10-18 23:58:21.663514407 +0530
Modify: 2012-10-18 23:58:21.663514407 +0530
Change: 2012-10-18 23:58:21.663514407 +0530
$ touch -a tgs.txt

After the above touch command (Please note that the access time is changed):

$ stat tgs.txt

  File: `tgs.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 394283      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/lakshmanan)   Gid: ( 1000/lakshmanan)
Access: 2012-10-19 00:08:23.559514525 +0530
Modify: 2012-10-18 23:58:21.663514407 +0530
Change: 2012-10-19 00:08:23.559514525 +0530

3. Change File’s Modification Time using -m

You can change the modification time of a file using -m option.

$ touch -m *.o

The above method can be used to change the mtime of all obj files, when using make utility.

NOTE: It is not possible to change the ctime using touch command

4. Explicitly Setting Access and Modification time using -t and -d

Instead of taking the current time-stamp, you can explicitly specify the time using -t and -d options.

The format for specifying -t is [[CC]YY]MMDDhhmm[.SS]

$ touch -t [[CC]YY]MMDDhhmm[.SS]

The following explains the above format:

  • CC – Specifies the first two digits of the year
  • YY – Specifies the last two digits of the year. If the value of the YY is between 70 and 99, the value of the CC digits is assumed to be 19. If the value of the YY is between 00 and 37, the value of the CC digits is assumed to be 20. It is not possible to set the date beyond January 18, 2038.
  • MM – Specifies the month
  • DD – Specifies the date
  • hh – Specifies the hour
  • mm – Specifies the minute
  • SS – Specifies the seconds

For example:

$ touch -a -m -t 203801181205.09 tgs.txt

Verify the above change using stat command:

$ stat tgs.txt
  File: `tgs.txt'
  Size: 3         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 394283      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/lakshmanan)   Gid: ( 1000/lakshmanan)
Access: 2038-01-18 12:05:09.000000000 +0530
Modify: 2038-01-18 12:05:09.000000000 +0530
Change: 2012-10-19 00:40:58.763514502 +0530

You can also use a string to change the time

Another example:

$ touch -d "2012-10-19 12:12:12.000000000 +0530" tgs.txt

For developers, touch command will be really helpful when you are working with Makefiles

5. Copy the Time-stamp from Another File using -r

You can also take a file as a reference, and update the time for other files, so that both file will hold the same time.

The following touch command example will update the time-stamp of file a.txt with the time-stamp of tgs.txt file.

$ touch a.txt -r tgs.txt
To calculate the phase change energy from bcc to fcc using LAMMPS, you can use the lattice orientation relationship method. This method involves creating a bcc and fcc crystal in LAMMPS, aligning their lattice orientations, and then calculating the energy difference between the two structures. Here is an example LAMMPS input script that demonstrates this method: ``` # Create bcc crystal units metal atom_style atomic lattice bcc 2.855 region box block 0 10 0 10 0 10 create_box 1 box create_atoms 1 box mass 1 55.85 # Relax bcc structure pair_style eam pair_coeff * * Fe_u3.eam neighbor 2.0 bin neigh_modify every 20 delay 0 check no fix 1 all box/relax iso 0.0 vmax 0.001 thermo 10 thermo_style custom step pe lx ly lz press pxx pyy pzz minimize 1e-8 1e-8 1000 10000 # Create fcc crystal clear units metal atom_style atomic lattice fcc 3.615 region box block 0 10 0 10 0 10 create_box 1 box create_atoms 1 box mass 1 55.85 # Align lattice orientations clear units metal atom_style atomic lattice fcc 3.615 orient x 1 0 0 orient y 0 1 0 orient z 0 0 1 region box block 0 10 0 10 0 10 create_box 1 box create_atoms 1 box mass 1 55.85 group fcc type 1 group bcc type 2 fix 1 fcc setforce 0.0 0.0 0.0 fix 2 bcc setforce 0.0 0.0 0.0 minimize 1e-8 1e-8 1000 10000 variable dx equal lx/10.0 variable dy equal ly/10.0 variable dz equal lz/10.0 displace_atoms bcc move ${dx} 0.0 0.0 # Calculate energy difference compute bccpe bcc pe compute fccpe fcc pe variable dE equal c_fccpe-c_bccpe print "Energy difference: ${dE} eV/atom" ``` This script first creates a bcc crystal and relaxes its structure using the EAM potential for iron. It then creates an fcc crystal and aligns its lattice orientation with the bcc crystal. The two structures are then minimized to their equilibrium positions. Finally, the energy difference between the fcc and bcc structures is calculated using the compute command in LAMMPS. Note that you will need to replace the EAM potential file `Fe_u3.eam` with the appropriate potential for your system. I hope this helps!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值