How to create a file in Linux Terminal [Quick Guide]
The ability to create files is a basic and essential function in any operating system. However, those who are new to Linux – especially its command-line – may struggle. Today's quick guide will remedy this, showing you how to quickly create a file in Linux using its terminal. We'll also be covering some common issues that may be preventing you from creating files.
Common errors
If you already know how to create a file but can't, the first thing to check are your permissions. You will need write permissions on a file's parent directory to be able to create a file there. If you do not have the relevant authority, you will receive a 'permission denied' error. Some directories require root privileges to create files in.
The permissions of a folder can be modified with the chmod
command.
Other possible issues include:
- Mistyping the name of the directory: This will return a 'No such file or directory error'
- A hardware issue: A physical fault in a drive may prevent new content from being written to it.
- Dual booting: You are dual booting but the Windows OS hasn't been completely shut down. Perform a full shutdown by holding the shift key while pressing the shutdown button in your start menu.
- The file system of your hard drive is incompatible with Linux: Linux prefers Ext4, XFS, Btrfs, and ZFS file systems.
How to create a file in Linux Terminal
There are several ways to create files in the Linux command line, but we're just going to focus on the two most popular methods: touch
and cat
.
Using touch
touch
is a versatile command that allows you to both create files and modify the timestamps on existing files. Creating a single file with touch is simple:
touch yourfilename.txt
You can, of course, change the file name and extension to whatever you like.
You can also create multiple files in one command:
touch filename1.txt filename2.txt filename3.txt
Using cat
The cat command is primarily designed for reading and combining files, but you can use it to create them, too. For example:
cat > filename.txt
You can then press Ctrl + D to save the file.