How to create a file in Linux from terminal window?
You can create a file from the Linux terminal in various ways. Below are the most common approaches:
-
Using
touch
touch filename.txt
- Creates a new empty file named
filename.txt
if it doesn’t already exist. - If the file does exist,
touch
updates its last modified timestamp.
- Creates a new empty file named
-
Using Redirection with
>
echo "Hello world!" > filename.txt
- Writes
"Hello world!"
tofilename.txt
, creating the file if it doesn’t exist. - Overwrites any existing content in
filename.txt
.
- Writes
-
Using
cat
cat > filename.txt
- Opens an interactive prompt. Whatever you type will be written into
filename.txt
. - Press Ctrl + D on an empty line to finish and save.
- Opens an interactive prompt. Whatever you type will be written into
-
Using a Text Editor
- Nano:
Edit and save using Ctrl+O, then exit with Ctrl+X.nano filename.txt
- Vim:
Press i to insert text, then Esc, type :wq to write and quit.vim filename.txt
- Nano:
-
Using
>
for Empty File> filename.txt
- Creates or truncates an empty file if you don’t provide any input.
Recommended Resource
If you’re looking to deepen your command-line capabilities and strengthen your overall coding proficiency, check out this course from DesignGurus.io:
- Grokking Data Structures & Algorithms for Coding Interviews
A robust understanding of data structures and algorithms complements your Linux skill set, enabling you to build efficient, reliable solutions—whether writing quick scripts or large-scale applications.
CONTRIBUTOR
TechGrind