Fluxtail
Log Management Guides

How to Create a New File in Linux

Create an empty Linux file with touch, write or append content, avoid overwriting existing files, and check permissions with practical Bash examples.

By Fluxtail Engineering Updated

To create an empty file in Linux, run touch notes.txt. To create a file containing text, use printf '%s\n' 'your text' > notes.txt. The difference matters: touch changes timestamps if the file already exists, while > replaces its contents.

Try the examples below in a new scratch directory, not in a service or configuration directory:

mkdir file-demo && cd file-demo

If file-demo already exists, mkdir fails and cd does not run. Choose a different name before continuing.

Create an empty file with touch

touch notes.txt

If notes.txt does not exist, this creates an empty file. If it already exists, touch leaves its contents alone but updates its access and modification timestamps. That behavior is useful, but it means touch is not an “only create if absent” command. See the GNU touch reference.

Use quotes around a name containing spaces:

touch 'session notes.txt'

The parent directory must exist and you need permission to create a file there. touch also does not acquire a lock. A marker named job.lock would still be an ordinary file; another process could create or touch it too. Use a real locking mechanism when two processes must exclude each other.

Write text to a new file

Write one line with printf

Use Bash’s printf builtin when the file needs content:

printf '%s\n' 'status=ready' > status.txt

%s\n writes the supplied text followed by a newline. The shell handles > before running printf: it creates status.txt if absent, but truncates it if it already exists. Check the path before running a command that uses >, especially outside a scratch directory. The Bash redirection reference documents this behavior.

Avoid using touch first when you intend to write content immediately. printf ... > file creates the file itself, so an extra step adds nothing.

Write multiple lines with a heredoc

A heredoc lets you write several lines without a chain of printf commands:

cat > worker.conf <<'EOF'
name=thumbnail-worker
enabled=true
retries=3
EOF

The quoted EOF makes Bash write characters such as $ literally instead of expanding variables or running command substitutions. The closing EOF must appear alone on its line. This example still uses > and will replace an existing worker.conf; a heredoc does not make overwriting safe. See Bash’s heredoc rules.

Use printf for short generated lines and a heredoc for a small block you want to read and review as a unit. For a long or frequently edited configuration file, an editor or your normal configuration-management process is easier to audit.

Append without replacing existing text

Use >> to add text to the end of a file:

printf '%s\n' 'status=complete' >> status.txt

Unlike >, >> does not truncate existing content. It also creates the file if it does not yet exist. That can hide a misspelled path: appending to stats.txt instead of status.txt silently creates a different file. Check the filename and inspect the result.

Appending is useful for a small local note. It is not a substitute for a managed log writer when multiple processes need ordering, rotation, or reliable collection.

Avoid overwriting an existing file

In Bash, set -C enables noclobber. With that option active, > refuses to replace an existing regular file:

(
  set -C
  printf '%s\n' 'release=staged' > release.txt
)

Run that block a second time and the redirection fails rather than replacing release.txt. The parentheses confine the setting to a subshell, so it does not change your interactive shell afterward. Bash also supports >| to deliberately override noclobber, so this is a guard against mistakes, not a locking or authorization boundary. See the Bash set documentation.

For a script that decides whether to create or reuse a file, an existence check can help with control flow. It is not an exclusive-create operation: another process can change the path after the check. See how to test whether a Bash file exists for the test syntax, but do not treat that test as a lock.

Create parent directories and limit permissions

A file command cannot create missing parent directories. Create the directory first:

mkdir -p output/reports
touch output/reports/summary.txt

mkdir -p creates missing parents and accepts an existing directory. It does not repair permissions on a directory that already exists. See the GNU mkdir reference.

For a newly created file that should not be readable by your group or other users, set a restrictive creation mask in a subshell:

(
  umask 077
  set -C
  printf '%s\n' 'visibility=private' > private-demo.txt
)

umask 077 removes group and other permission bits from newly created files in that subshell; it does not change an existing file’s permissions. set -C makes this example fail rather than overwrite an existing regular file. Check the resulting mode before putting sensitive data in a file. Bash documents umask as a file-creation mask, not a command that fixes old files.

Make a temporary file with mktemp

Use GNU mktemp when a script needs a new, unpredictably named temporary file:

mktemp -p . draft.XXXXXXXX

This creates a file in the current scratch directory and prints its generated name. Capture that name in a script rather than guessing it. GNU mktemp creates a file accessible to its owner, subject to any more restrictive mask, and avoids the predictable-name race of names assembled from a process ID. Do not use mktemp -u and then create the file later: that option only prints a name and leaves a race between choosing and creating it. See the GNU mktemp reference.

A temporary file is useful for intermediate work. Creating one alone does not guarantee that later replacement of another file is atomic or safe across filesystem boundaries.

Check the file you created

Inspect the filename, size, permissions, and the content you expect:

ls -l notes.txt status.txt
wc -c notes.txt status.txt
sed -n '1,10p' status.txt

notes.txt should have zero bytes until you write to it. The size and displayed content of status.txt depend on which examples you ran. Avoid printing a real secret-bearing file to a shared terminal, ticket, or log just to verify it.

When the file contains logs

Creating a local log file is different from collecting and searching logs. Fluxtail does not create files on your Linux host; its paid Starter and Pro plans receive and let you investigate logs. If your next task is to send host logs to a central service, start with syslog forwarding rather than adding more shell redirections to an application script.