← All commands
Topic hub PowerShell Equivalents
Windows Linux PowerShell Equivalents File System

Windows PowerShell Set-Content equivalent in Linux

Writes new content to a file, replacing the previous contents.

Windows PowerShell Set-Content
Linux equivalents
Debian/Ubuntu same command
printf "%s " "text" > file.txt
Fedora same command
printf "%s " "text" > file.txt
Arch same command
printf "%s " "text" > file.txt

Overview

What this command does

Writes new content to a file, replacing the previous contents.

Migration tip: Shell redirection with > replaces a file. tee is useful when the destination requires sudo permissions.

Practical tasks

Common use cases

Replace file contents

Writes new content to a file, replacing the previous contents.

printf "%s\n" "Hello" > file.txt

Replace a file with one line

printf "%s\n" "one" "two" > file.txt

Write multiple lines

printf "%s\n" "setting=value" | sudo tee /etc/example.conf

Write a root-owned file

Ready to run

Copyable Linux commands

Review paths, device names, package names and permissions before running any command.

printf "%s\n" "Hello" > file.txt
printf "%s\n" "one" "two" > file.txt
printf "%s\n" "setting=value" | sudo tee /etc/example.conf

Walkthrough

Examples with explanations

printf "%s\n" "Hello" > file.txt # replace the file contents
printf "%s\n" "one" "two" > file.txt # write several lines
printf "%s\n" "setting=value" | sudo tee /etc/example.conf # write through sudo

Reference

Common options and variations

Command or optionUse
printf "%s\n" "Hello" > file.txtReplace a file with one line
printf "%s\n" "one" "two" > file.txtWrite multiple lines
printf "%s\n" "setting=value" | sudo tee /etc/example.confWrite a root-owned file

Distro differences

Debian/Ubuntu vs Fedora vs Arch

This command is the same on Debian/Ubuntu, Fedora and Arch Linux when the relevant tool is installed.

DistributionPackage manager / baseEquivalent commandDifference to notice
Debian/Ubuntu same command printf "%s " "text" > file.txt Same command on this distribution.
Fedora same command printf "%s " "text" > file.txt Same command on this distribution.
Arch same command printf "%s " "text" > file.txt Same command on this distribution.

Important

What to watch out for

The > operator truncates the destination before writing. Use >> or tee -a when you intend to append.

Keep learning

Related PowerShell Equivalents commands