• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Create Files In Linux

Emmanuel Oyibo

Published: 4/25/2024

About Terminus

The short answer

On Unix-like operating systems such as macOS and Linux, you can create one or more empty files using the touch command as follows:

$ touch <file ...>

Run in Warp

Where:

  • file ... is a list of paths to the file you want to create.

For example, this command will create a new empty file named test_script.sh in the current working directory:

$ touch test_script.sh

Run in Warp

And this command will create two new empty files named index.js and package.json in the website directory located in the home directory:

$ touch ~/website/index.js ~/website/package.json

Run in Warp

Note that if the destination file already exists, only the file’s access and modification time will be updated.

Should you encounter “permission denied” errors while trying to create a file, check that you have the necessary permissions to create files in the target folder.

You can learn more about this in our other articles, Linux File Permissions Explained and Linux Chmod Command.

Writing content in a new file using the output redirection operator

In Unix-like operating systems, the output redirection operator (>) is used to redirect the output of a command so that it writes into a file instead of the standard output of the terminal.

Writing a string in a new file using echo

To write an arbitrary string into a new file, you can use the echo command combined with the output redirection operator as follows:

$ echo <string> > <file>

Run in Warp

Where:

  • string is a string of characters.
  • > is the output redirection operator.
  • file is the path to the file you want the echo command to write the string into.

For example, this command will write the string "NODE_ENV=development” into the file named .env.development located in the current working directory:

$ echo "NODE_ENV=development" >.env.development

Run in Warp

Note that if the destination file already exists, the output redirection operator will overwrite its content with the specified string.

You can learn more about appending content to an existing file by reading our other article on how to edit files in Linux.

Easily retrieve this command using Warp’s AI Command Suggestions

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Thumbnail for

Entering write strings into a file in the AI Command Suggestions will prompt an echo command that can then quickly be inserted into your shell by doing CMD+ENTER.

Writing multiple strings in a new file using cat

Although the cat command is typically used to display the content of files, you can also use it to create text files interactively using the following syntax:

$ cat > <file>

Run in Warp

Where:

  • file is the path to the file you want the cat command to write into.

When executed, the cat command will read from the standard input until it encounters an EOF (end-of-file) sequence, which can be triggered by pressing the CTRL + D key combination:

Thumbnail for

Note that when using the cat command, the newlines will automatically be preserved.

Writing a formatted string in a new file using printf

To write a formatted string into a new file, you can use the printf command as follows:

$ printf <format> <value ...>

Run in Warp

Where:

  • format is a format string that includes characters and specifiers such as %s to specify a string of characters, %d to specify an integer, %f to specify a float, and so on.
  • value ... represents a list of values that will replace the specifier in the format string, in the order of declaration.

For example, this command will create a new file named project_info.txt in the current working directory:

$ printf "Project Name: %s\nTeam Size: %d\n" "Lorem Ipsum" 5 >project_info.txt

Run in Warp

With the following content:

Project Name: Lorem Ipsum
Team Size: 5

Run in Warp

Creating a new file using Vim

When you need to work with larger and more complex texts, you can use in-terminal Vim text editor, which is by default installed on most Linux distributions.

To create a new file with Vim, you can use the vim command as follows:

$ vim <file>

Run in Warp

Where:

  • file is the path to the file you want to create.

Once launched, you can:

  1. 1. Press the i key to enter the INSERT mode.
  2. 2. Type your text, code, etc.
  3. 3. Press the ESCAPE key to come back to the default NORMAL mode.
  4. 4. Type :wq command to save your changes and quit the editor.

For example:

Thumbnail for

You can learn more about how Vim works by reading our other articles on Vim modes and how to edit files in Linux.

Creating a new file with spaces in the filename

To create a new file with space characters in the filename, you can either escape the spaces using a backslash character (\) as follows:

$ touch my\ file.txt

Run in Warp

Or enclose the filename in single or double quotes as follows:

$ touch "my file.txt"

Run in Warp

Note that including space characters in filenames is usually not recommended as it usually makes it harder to work with files.

Creating a new file with a predefined size

To create a new file of a predefined size, rather than filling it will zeros, you can use the fallocate command with the -l flag (short for --length) as follows:

$ fallocate -l <size> <file>

Run in Warp

Where:

  • size is the size of the file suffixed with a letter such as K for kilobytes, M for megabytes, G for gigabytes, and so on.
  • file is the path to the file you want to create.

For example, this command will create a new empty file named data.txt in the current working directory with a size of 500 kilobytes:

$ fallocate -l 500K data.txt

Run in Warp

Automating the creation of new files using a Bash script

To automate the creation of a new file and prevent the overwriting of existing ones, you can use a Bash script as follows:

#!/bin/bash

FILEPATH="path/to/file"

if [[ ! -f $FILEPATH ]]; then
 echo "Lorem ipsum">$FILEPATH
fi

Run in Warp

Where:

  • #!/bin/bash is a directive used to indicate that the script must be executed using the Bash binary.
  • FILEPATH="path/to/file" is a variable used to store the path to the file you want to create.
  • [[ ! -f $FILEPATH ]] is a condition used to check if the file already exists.

Note that to execute this script, you must first give it execution permission using the chmod command:

$ chmod +x script.sh

Run in Warp

Then execute it using the following command:

$ ./script.sh

Run in Warp

Written by

Emmanuel Oyibo

Filed Under

Related Articles

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

UnixLinux
Thumbnail for Razvan LudosanuRazvan Ludosanu

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

Linux Chmod Command

Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.

Linux
Thumbnail for Razvan LudosanuRazvan Ludosanu

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Linux

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

Linux

Switch Users In Linux

Learn how to switch between users, log in as another user, and execute commands as another user in Linux.

Linux

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.

Linux

Delete Files In Linux

Learn how to selectively delete files in Linux based on patterns and properties using the rm command.

Linux

Find Files In Linux

Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.

Linux

Copy Files In Linux

Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.

Linux

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Request demo
Thumbnail for null