Sunday, June 29, 2014

Unix Shell: Temporary Files(1)

1. 5 ways to create temporary files
terminal:
1) Use "cat /dev/null" to create temp1 file, truncate the file if necessary
2) Use "printf" to create temp2 file, truncated the file if necessary
3) Similar from point 1
4) Similar from point 3
5) Use "touch" command to create temp5 file

Touch is the best choice here. Since it is not error prone.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat /dev/null > temp1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ printf "" > temp2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat /dev/null >> temp3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ printf "" >> temp4  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch temp5  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -l  
 total 0  
 -rw-rw-r-- 1 aubinxia aubinxia 0 Jun 29 16:56 temp1  
 -rw-rw-r-- 1 aubinxia aubinxia 0 Jun 29 16:56 temp2  
 -rw-rw-r-- 1 aubinxia aubinxia 0 Jun 29 16:56 temp3  
 -rw-rw-r-- 1 aubinxia aubinxia 0 Jun 29 16:56 temp4  
 -rw-rw-r-- 1 aubinxia aubinxia 0 Jun 29 16:56 temp5  

2. Change Different Time with "touch" command
terminal:
1) List all times of temp5
2) Use touch command against temp5 without other parameters
3) List all times of temp5, you will find that all times get changed
4) Use touch command with -m option, only modification time and last change time get updated
5) List all times of temp5
6) Use touch command with -a option, only access time and last change time get updated
7) List all times of temp5
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ stat temp5  
  File: ‘temp5’  
  Size: 0         Blocks: 0     IO Block: 4096  regular empty file  
 Device: 801h/2049d    Inode: 134297   Links: 1  
 Access: (0664/-rw-rw-r--) Uid: ( 1000/aubinxia)  Gid: ( 1000/aubinxia)  
 Access: 2014-06-29 16:56:52.768351035 -0400  
 Modify: 2014-06-29 16:56:52.768351035 -0400  
 Change: 2014-06-29 16:56:52.768351035 -0400  
  Birth: -  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch temp5  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ stat temp5  
  File: ‘temp5’  
  Size: 0         Blocks: 0     IO Block: 4096  regular empty file  
 Device: 801h/2049d    Inode: 134297   Links: 1  
 Access: (0664/-rw-rw-r--) Uid: ( 1000/aubinxia)  Gid: ( 1000/aubinxia)  
 Access: 2014-06-29 17:05:56.020347187 -0400  
 Modify: 2014-06-29 17:05:56.020347187 -0400  
 Change: 2014-06-29 17:05:56.020347187 -0400  
  Birth: -  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch -m temp5  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ stat temp5  
  File: ‘temp5’  
  Size: 0         Blocks: 0     IO Block: 4096  regular empty file  
 Device: 801h/2049d    Inode: 134297   Links: 1  
 Access: (0664/-rw-rw-r--) Uid: ( 1000/aubinxia)  Gid: ( 1000/aubinxia)  
 Access: 2014-06-29 17:05:56.020347187 -0400  
 Modify: 2014-06-29 17:06:12.892347068 -0400  
 Change: 2014-06-29 17:06:12.892347068 -0400  
  Birth: -  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch -a temp5  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ stat temp5  
  File: ‘temp5’  
  Size: 0         Blocks: 0     IO Block: 4096  regular empty file  
 Device: 801h/2049d    Inode: 134297   Links: 1  
 Access: (0664/-rw-rw-r--) Uid: ( 1000/aubinxia)  Gid: ( 1000/aubinxia)  
 Access: 2014-06-29 17:06:29.168346952 -0400  
 Modify: 2014-06-29 17:06:12.892347068 -0400  
 Change: 2014-06-29 17:06:29.168346952 -0400  
  Birth: -  

3. Override time with "touch" command
-t option allow user to override the time stamp, but we have to make sure that we are using the right time format.
terminal:
1) Wrong format
2) Wrong format
3) Right format, meaning 2014, 06 01, 17:00
4) List all times of temp5, both access and modification time get changed, and last change time is the latest current time without getting affected
5) Right format, 53 in the end represents seconds
6) List all times of temp5,same, both access and modification time get changed, and last change time is not affected.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch -t 2014 temp5  
 touch: invalid date format ‘2014’  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch -t 20140601 temp5  
 touch: invalid date format ‘20140601’  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch -t 201406011700 temp5  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ stat temp5  
  File: ‘temp5’  
  Size: 0         Blocks: 0     IO Block: 4096  regular empty file  
 Device: 801h/2049d    Inode: 134297   Links: 1  
 Access: (0664/-rw-rw-r--) Uid: ( 1000/aubinxia)  Gid: ( 1000/aubinxia)  
 Access: 2014-06-01 17:00:00.000000000 -0400  
 Modify: 2014-06-01 17:00:00.000000000 -0400  
 Change: 2014-06-29 17:16:33.384342673 -0400  
  Birth: -  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch -t 201406011700.53 temp5  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ stat temp5  
  File: ‘temp5’  
  Size: 0         Blocks: 0     IO Block: 4096  regular empty file  
 Device: 801h/2049d    Inode: 134297   Links: 1  
 Access: (0664/-rw-rw-r--) Uid: ( 1000/aubinxia)  Gid: ( 1000/aubinxia)  
 Access: 2014-06-01 17:00:53.000000000 -0400  
 Modify: 2014-06-01 17:00:53.000000000 -0400  
 Change: 2014-06-29 17:17:32.532342254 -0400  
  Birth: -  

No comments:

Post a Comment