Windows PowerShell Set-Content equivalent in Linux
Writes new content to a file, replacing the previous contents.
PowerShell Set-Content
printf "%s
" "text" > file.txt
printf "%s
" "text" > file.txt
printf "%s
" "text" > file.txt
Overview
What this command does
On Windows, "PowerShell Set-Content" is used to replace file contents. On Linux, the closest practical mapping shown here is "printf "%s " "text" > file.txt". Writes new content to a file, replacing the previous contents. This reference is grouped under PowerShell Equivalents → File System.
Windows → Linux context
What changes when you use this on Linux?
Equivalent task, different tool
Windows PowerShell Set-Content maps to printf "%s
" "text" > file.txt for this task. Treat this as a workflow mapping rather than a character-for-character syntax translation.
Portable across the compared distros
The Debian/Ubuntu, Fedora and Arch rows use the same command in this mapping. Package availability or defaults can still vary, so check the installed tool version when an option behaves differently.
Command-specific caution
The > operator truncates the destination before writing. Use >> or tee -a when you intend to append.
For the broader concept behind this mapping, read PowerShell vs Bash.
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 option | Use |
|---|---|
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 |
Distro differences
Debian/Ubuntu vs Fedora vs Arch
Debian/Ubuntu, Fedora and Arch all use "printf "%s " "text" > file.txt" for this mapping. The command syntax shown here is portable across the three rows, although installed versions and system defaults can still vary.
| Distribution | Package manager / base | Equivalent command | Difference 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
Keep learning