09. Making Files and Directories touch, mkdir

Creating empty files

To make an empty file, use the touch command.

$ touch file1 file2 file3

Managing a file's timestamp

The touch command is built for much more than just creating empty files. It can be used to update timestamps. Specify a particular option to modify a certain time attribute.

-a
Modifiy the access time.
-m
Change the modification time.
-c
Do not create any files that already exist.
-t timestamp
Set the timestamp in the form MMDDhhmm[[CC]YY][.ss]. MM
MM is month, DD is day, hh is hour, mm is minute, CC[YY] is year, and ss is second.
-r reffile
Replicate the timestamps from another file.

This command can also be used to update the access date or modification date of a file or directory.

You can access the modification time with a simple ls -l command.

Making directories

To make a directory, simply use the mkdir command:

$ mkdir dir1 dir2 dir3

This makes a directory named dir1, dir2 and dir3.

Removing files and directories

To remove (aka delete) a file, use the rm command.

$ rm file1

To remove an entire directory, add the -r option, which means to delete recursively. You may also use the rmdir command, but this only works if the directory is empty.

$ rm -r dir1

Deleting without the -r option results in an error.

Be careful when deleting!

Be careful when you delete! There is no undelete command, although some distros implement some sort of recyling bin.

Options for removing

Here are some useful options to use for the rm command.

-f
Force. Ignore nonexistent files (for use with wildcards)
-i
Interactively. Prompts user for permission for each file to be deleted.
-v
Verbose. Displays informative messages as deletions are being performed.

Great work! Now let's turn to more intermediate commands.

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Ad