how to create file in terminal

Mastering the Terminal: A Step-by-Step Guide to Create Files Instantly

By Thomas Martin
AD

Creating files using the terminal in Linux or macOS might initially seem daunting, but with a little practice, it becomes a quick and efficient way to manage your files. Here's a step-by-step guide on how to create files in the terminal.

Before we dive into creating files, let's ensure you're familiar with the basic terminal commands. The pwd command displays your current working directory, ls lists files and directories, and cd changes your current directory.

Creating a New File

To create a new file, use the touch command followed by the filename. Here's the syntax:

AD
How to create a file in Linux using Terminal | ION HowTo
How to create a file in Linux using Terminal | ION HowTo
How to Create a Text File in Linux Terminal [4 Methods]
How to Create a Text File in Linux Terminal [4 Methods]
COLLEGE WON’T TEACH YOU THIS — PART 2 If you’re a beginner developer, Terminal basics are a must-have skill. 💻⚡ You don’t need to memorize 100 commands. Start with these 👇 📂 pwd → See your current location 📁 ls → List files & folders ➡️ cd → Move between folders ↩️ cd .. → Go back 📦 mkdir → Create a folder 📄 touch → Create a file 🗑️ rm → Remove a file 🧹 clear → Clear the terminal ❓ --help → Get command help The more comfortable you become with the terminal, the more confident you'll feel as a... Leetcode Study Guide For Beginners, Terminal Commands Basics Command Line Cheat Sheet, Beginner Programming Study Guide, Terminal Vs Command Line Guide, Programming Basics For Beginners, Beginner Guide To Programming Methods, Terminal Shortcut Keys Guide, Programming Fundamentals For Beginners, How To Use Terminal For Beginners
COLLEGE WON’T TEACH YOU THIS — PART 2 If you’re a beginner developer, Terminal basics are a must-have skill. 💻⚡ You don’t need to memorize 100 commands. Start with these 👇 📂 pwd → See your current location 📁 ls → List files & folders ➡️ cd → Move between folders ↩️ cd .. → Go back 📦 mkdir → Create a folder 📄 touch → Create a file 🗑️ rm → Remove a file 🧹 clear → Clear the terminal ❓ --help → Get command help The more comfortable you become with the terminal, the more confident you'll feel as a... Leetcode Study Guide For Beginners, Terminal Commands Basics Command Line Cheat Sheet, Beginner Programming Study Guide, Terminal Vs Command Line Guide, Programming Basics For Beginners, Beginner Guide To Programming Methods, Terminal Shortcut Keys Guide, Programming Fundamentals For Beginners, How To Use Terminal For Beginners

touch filename extensions

For example, to create a new file named example.txt, you would type:

How To Open And Run Commands In The Terminal (Beginner Friendly)
How To Open And Run Commands In The Terminal (Beginner Friendly)
Learn Linux from Beginner to Advanced (Part-1) | Essential Terminal Commands Cheat Sheet
Learn Linux from Beginner to Advanced (Part-1) | Essential Terminal Commands Cheat Sheet
25 Terminal Commands Every Developer Should Know 💻⚡
25 Terminal Commands Every Developer Should Know 💻⚡

touch example.txt

Defining File Permissions

By default, a file created with touch has read and write permissions for the owner and read permissions for the group and others. You can define initial permissions with the -m option, followed by the permissions in octal format. For example:

AD

touch -m 755 filename

a computer screen with the words terminal on it and an arrow pointing to different locations
a computer screen with the words terminal on it and an arrow pointing to different locations
How to use Terminal for productivity
How to use Terminal for productivity
7 Terminal Commands Every Beginner Developer Should Know 💻 | Linux & macOS Cheat Sheet
7 Terminal Commands Every Beginner Developer Should Know 💻 | Linux & macOS Cheat Sheet
How to format and mount a USB drive in the Linux terminal - PragmaticLinux
How to format and mount a USB drive in the Linux terminal - PragmaticLinux
FILE AND DIRECTORY MANAGEMENT
FILE AND DIRECTORY MANAGEMENT
How To Create Files in Linux System: An Ultimate Guide
How To Create Files in Linux System: An Ultimate Guide

This sets the file permissions to read, write, and execute for the owner and only read and execute for the group and others.

Creating an Empty File

If you need an empty file, you can use the touch command with the -e option to force the file's timestamp to the current time, effectively creating an empty file:

AD

touch -e filename

How to Make a File in Linux from the Command Line – Create a File in the Terminal
How to Make a File in Linux from the Command Line – Create a File in the Terminal
How to Use the Cat Command in Linux [22 Useful Examples]
How to Use the Cat Command in Linux [22 Useful Examples]
How to Navigate the File Directory in Linux Terminal - Make Tech Easier
How to Navigate the File Directory in Linux Terminal - Make Tech Easier

Creating a New File with Content

If you want to create a new file and insert content, use the echo command. Here's how:

echo "Content goes here" > filename

AD

This command will replace any existing content in the file with the new text. If you want to append text instead of replacing, use the >> operator:

echo "Appended content" >> filename

Inserting Content at a Specific Line

If you want to insert content at a specific line in the file, use the sed command:

echo "Inserted content" | sed -i '' 'LINE_NUMBER a' filename

Replace LINE_NUMBER with the desired line number. This command will append the content after the specified line.

Creating a File with Multi-line Content

To create a file with multi-line content, use a here document:

cat > filename << EOF Content line 1 Content line 2 Content line 3 EOF

Each line of content follows the opening EOF, and the file is closed with the same keyword on a new line.

That's all you need to know about creating files in the terminal! With a bit of practice, you'll be navigating and managing your files like a pro. Now, go forth and organise your digital world line by line.

AD