Wednesday, November 21, 2007

File Creation without File Opening in Linux

How to create new file without opening an editor or executing a copy / move command?
How to create new multiple files simultaneously?
How to create multiple files with the same timestamps?
How to update file date and timestamps?

Another overly used and unexplained command around this blog.

Touch basically touches and changes file timestamps. Touch updates time modifications of each file arguments to current time of the box. Touch command is part of linux coreutils package, part of GNU. Touch command is installed by default.

Here's how to create a file without actually opening an editor to start editing it or without making use of linux copy and move commands.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# touch newfile.txt
# ls -la newfile.txt
-rw-r--r-- 1 root root 0 2007-11-22 10:09 newfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If a filename is being passed as an argument to touch command, and the file does not exists, the file is then created with the latest time stamp. Now, if a file is already an existing file from current working folder location, the timestamp of that existing file is being updated by default touch argument. This touch behaviour can be avoided using a specific command line parameter.

Touch linux command accepts both date and time stamp argument.

How to create new multiple files simultaneously?
How to create multiple files with the same timestamps?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# touch file1.txt file2.txt file3.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ls -la
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-rw-r--r-- 1 root root 0 2007-11-22 10:15 file1.txt
-rw-r--r-- 1 root root 0 2007-11-22 10:15 file2.txt
-rw-r--r-- 1 root root 0 2007-11-22 10:15 file3.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Noticed from above that they have all the same time stamp of 10:15 and date stamp of 2007-11-22.

For more touch , man touch.

HTH

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy