Mar 042015
 

In this brief post, we’ll look at a few ways to quickly create files via the command line.

1. Use Redirection Operator >

Tammy@JacobPC $ > Test-File.txt

The above example will create a file called Test-File.txt in your current working directory.

What if the file already existed? In that case, the file would be truncated to zero bytes, which means it’ll be overwritten silently.

Important: So be careful and ensure that you don’t already have this file before you try this command.

Tammy@JacobPC $ >> Test-File.txt

2. Use the Append Command >>

Usually, the >> command is used when you wish to append the output to an existing file instead of overwriting it.

But if the file does not already exist, a new file is created.

Tammy@JacobPC $ >> Test-File.txt

3. Create Files with Content

Using the command line, you can also create a file with some content.

This is useful for creating short files when you’re in a hurry and works like a rudimentary word processor.

Tammy@Jacob $ cat > Test-File.txt
Jack and Jill went up the hill

Once you’re done, type Ctrl-D to signal cat the end of standard input

You can test the file you just created with the following command:

Tammy@JacobPC $ cat Test-File.txt
Jack and Jill went up the hill

4. Touch a File

Another way to create an empty file is via the touch command.

Here’s an example:

$Tammy@JacobPC touch Test-File.txt

The above command will create a file called Test-File.txt in your current working directory.

Alternatively, you can specify the directory where you want the new file to created.

$Tammy@JacobPC touch /home/Jackie/Documents/Test-File.txt

The marvels of Linux and the command line never cease to amaze me.

Sorry, the comment form is closed at this time.