Creating files with touch

Another skill you'll use frequently is creating files. You might think of creating files as getting an empty bucket that you can later fill with water...or sand...or rocks...or whatever. When you create a file, you designate an empty space that you can fill with programs, activity logs, your resume, or configurations—practically anything you want, or nothing at all.

Of course, you can always create a file by writing something in an editor and saving it, as described in Chapter 4, but you will likely encounter situations where you just need an empty file. You create empty files using the touch command, as shown in Code Listing 2.2.

To create a file:

1.
touch file.to.create

To create a file, type touch followed by the name of the file. This creates an empty file.

2.
ls -l file*

Optionally, verify that the file was created by typing ls -l plus file*. As shown in Code Listing 2.2, you'll see the name of the new file as well as its length (0) and the date and time of its creation (likely seconds before the current time, if you're following along).

Code Listing 2.2. Use the touch command to create files, update their modification times, or both.
$ ls
$ touch file.to.create
$ ls -l file*
-rw-rw-r--  1 ejr   users      0 Jun 29 11:53 file.to.create
$ touch -t 12312359 oldfile
$ ls -l
total 0
-rw-rw-r--  1 ejr   users      0 Jun 29 11:53 file.to.create
-rw-rw-r--  1 ejr   users      0 Dec 31 1998 oldfile
$ touch -t 123123592001 new.years.eve
$ ls -l
total 0
-rw-rw-r--  1 ejr   users      0 Jun 29 11:53 file.to.create
-rw-rw-r--  1 ejr   users      0 Dec 31 2001 new.years.eve
-rw-rw-r--  1 ejr   users      0 Dec 31 1998 oldfile
$

Tip

You can also use touch to update a file's date and time. For example, typing touch -t 12312359 oldfile at the prompt would update oldfile with a date of December 31, 23 hours, and 59 minutes in the current year. Or, typing touch -t 123123592001 new.years.eve would update the file called new.years.eve to the same time in the year 2001.


Tip

Each time you save changes in a file, the system automatically updates the date and time. See Chapter 4 for details about editing and saving files.


Tip

Refer to the sidebar called Naming Directories (and Files) on page 25 in this chapter for file-naming guidelines.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.243.184