How to Create File In Linux: Detailed Info with Examples

Whether you’re a seasoned system administrator or a beginner exploring the world of Linux, knowing how to create files is fundamental. Not only is it a routine task, but understanding the nuances can also save you time and effort in the long run.

In this comprehensive guide, we will dive deep into how to create a file in Linux using various commands and techniques. We’ll also explore the different types of files you may encounter and how to determine their types.

There are 101 Labs for the CompTIA Linux+ course on this website so please do check them out.

Types of Files in Linux

Before diving into the practical aspects of file creation in Linux, it’s imperative to grasp the different types of files you’ll come across. Understanding these types is not just academic; it’s fundamental to competent system management.

In Linux, files are categorized mainly into Regular Files, Directory Files, and Special Files. Knowing what these are and how they differ from each other will not only aid you in becoming a more proficient Linux user but will also help you understand the underlying system’s architecture better.

Regular Files

Regular files are the most common type of files you will deal with in a Linux environment. They contain either text or data in binary form. For example, source code files, text files, image files, and compiled binary files all fall under this category.

You can recognize them by a leading dash ‘-’ when you list them using the ‘ls -l’ command. It’s essential to note that Linux treats everything as a file, including regular files, so mastering their manipulation is foundational to system administration.

Directory Files

In contrast, directory files serve as containers that hold other files and directories. They function similarly to what you’d call folders in other operating systems like Windows or macOS. You can easily identify directory files through a leading ‘d’ when you use the ‘ls -l’ command. These directory files are crucial for the hierarchical file system structure that Linux employs, facilitating better organization and easier navigation.

Special Files

Special files serve specific functions and are usually not meant for storing data. These files are utilized to represent devices and other types of system information. They can include block devices like your hard drive, character devices like your keyboard, and even other unique file types for process communication and networking. Special files are what make the Linux ecosystem rich and powerful, enabling advanced operations and system-level functionalities.

Block File

Block files are a type of special file that facilitates buffered I/O interaction with devices like hard drives. They are particularly useful for devices that have a buffer and can read or write data in blocks. When you list files using the ‘ls -l’ command, block files are denoted with a leading ‘b’.

Character Device File

Character Device Files are another form of special files used for unbuffered I/O operations. These files usually correspond to hardware like keyboards and mice. Unlike block files, they handle data character by character, providing a straightforward way to read from or write to devices. In the ‘ls -l’ listing, they appear with a leading ‘c’.

Name Pipe File

Named Pipe Files, often referred to as FIFO (First In, First Out), serve as a mechanism for inter-process communication. Unlike regular pipes, named pipes exist as special files in the file system. They allow unrelated processes to communicate with each other, sharing data safely and efficiently. When you use the ‘ls -l’ command, these types of files are distinguishable by a leading ‘p’.

Symbolic Link File

Symbolic Link Files, or simply symlinks, are special files that point to another file or directory. They function as shortcuts, enabling easier access to resources located in different parts of the file system. They’re extremely useful for system administration tasks like configuration management. When listed using the ‘ls -l’ command, symbolic links are identified by a leading ‘l’.

Socket File

Socket Files are utilized for inter-process communication, similar to named pipe files, but they are more versatile. They facilitate both local and network communication between processes, making them incredibly powerful for a variety of applications, including web servers and databases. When you run the ‘ls -l’ command, socket files are marked by a leading ‘s’, setting them apart from other file types.

How to Find Out File Type Using Linux Command

Once you’re familiar with the diverse range of file types in Linux, the next step is to identify them efficiently using the command line. Accurate identification of file types is crucial, especially when you’re managing a system with numerous files and directories.

Fortunately, Linux provides a set of versatile commands to make this task easier. Commands like ‘touch’, ‘cat’, and ‘echo’ are not only used for creating and manipulating files but also for identifying what type of file you’re dealing with. In the following sections, we will delve into specific commands that assist you in determining file types.

Using the Touch Command

The ‘touch’ command in Linux is most commonly known for creating empty files, but it also serves as a utility for modifying file timestamps. When you’re working on the command line, you may often find the need to quickly generate a new file or update an existing one without opening it. The ‘touch’ command is perfect for this. For example, running ‘touch example.txt’ will create an empty text file named ‘example.txt’ if it doesn’t already exist.

Understanding how to use the ‘touch’ command efficiently is important for tasks that require rapid file creation or modification. However, did you know that you can also use ‘touch’ to subtly identify a file’s type? Simply run ‘file example.txt’ after using ‘touch’, and the system will inform you that it’s an empty text file.

Using the CAT Command

The ‘cat’ command, short for concatenate, is another versatile utility in the Linux toolkit. It’s most often used to display the content of files, concatenate files, and even create new ones. When you use ‘cat’ followed by the ‘>’ operator, you can easily create a new file or overwrite an existing one. For instance, executing ‘cat > newfile.txt’ will let you directly input content into ‘newfile.txt’ from the terminal.

Not only is the ‘cat’ command useful for file creation and manipulation, but it also serves an informative purpose. You can identify the file type by inspecting its contents. After creating a file using ‘cat’, you can employ the ‘file’ command to determine its type. For example, running ‘file newfile.txt’ will reveal whether it’s an ASCII text file or some other format.

Using the Redirection Operator

In Linux, the redirection operator ‘>’ is an incredibly useful tool for directing the output of a command into a file, effectively creating a new file or overwriting an existing one. It works seamlessly with various commands, not just ‘cat’. For instance, you could use ‘echo “Hello, World!” > hello.txt’ to create a new file named ‘hello.txt’ containing the string “Hello, World!”.

The redirection operator is more than just a tool for quick file creation; it’s also an instrument for file identification. After using the ‘>’ operator to generate a file, you can employ the ‘file’ command to inspect its type, similar to what you’d do after using ‘cat’ or ‘touch’. Executing ‘file hello.txt’ will indicate if the file is an ASCII text file, a binary file, or some other type.

Using the ECHO Command

The ‘echo’ command is another flexible utility commonly used in Linux for displaying a line of text or string. However, its utility extends beyond simple display functions. Coupled with the redirection operator, it can also be employed for quick file creation. For example, executing ‘echo “This is a test.” > testfile.txt’ will create a new file called ‘testfile.txt’ containing the specified text.

Like other commands and operators we’ve discussed, ‘echo’ has informational value when it comes to identifying file types. After using ‘echo’ to create or modify a file, you can use the file command to check its type. Running ‘file testfile.txt’ would tell you if it’s an ASCII text file or another type.

Using the Printf Command

The ‘printf’ command, though less commonly used than its counterparts like ‘echo’, is a highly customizable utility for formatted text output. It allows for advanced formatting options, which makes it an excellent choice for generating files with specific text layouts. For example, ‘printf “Name: %s\nAge: %d” “Bob” 30 > user.txt’ would create a file called ‘user.txt’ with formatted text.

Similar to the commands discussed earlier, ‘printf’ can be leveraged to identify file types. After generating a file using ‘printf’, you can use the file command to analyze its type. Simply execute ‘file user.txt’, and it will reveal the type of file you’ve created, be it an ASCII text, binary, or some other format. This flexibility and precision makes ‘printf’ a versatile tool, particularly useful when you require advanced text formatting or file manipulation.


Understanding how to create and identify file types in Linux is essential for effective system management. We’ve explored various commands like ‘touch’, ‘cat’, ‘echo’, and ‘printf’, among others, which not only facilitate file creation but also aid in file identification. Master commands like these, and you’ll navigate the Linux filesystem like a pro.

linux plus new

101 Labs Newsletter