In this article we are going to shows how to create a file in Linux terminal. If you want to create a file in Linux via command-line then this post is ideal for you.
In the Linux, you can create a new file using the command line or via the desktop file manager. If you are a Linux user then creating a new file is a basic requirement for Linux users.
Linux is an operating system, like Windows OS, iOS, and Mac OS. It is one of the most popular operating system on the planet, Android is also powered by Linux OS(operating system).
Linux OS(operating system) is also distributed under an open source license. Open source following these points :
- Freely to run the program, for any purpose.
- Free to study how the program works.
- It is free to re-distribute copies so you can help your friends.
- Free to distribute copies of your modified versions to others.
How to create a new File in Linux
Before creating new files, make sure you have write permissions on the parent directory. Without write permissions, you will get a permission denied error. To display the existing files or contents of a directory use the ls
command.
Create a File Using touch Command
The touch command used to create a new empty files and update the timestamps of existing files and directories.
To create a new file, run the touch
command with name of the file that you want to create:
touch filename.txt
If the given file does not exist then touch command will create a new file. And if the given file is already exists then it will change the file last access and modification times with the current time, means it will change the timestamps of file.
You can also create multiple files at once using touch command , simply pass the file names separated by space as show below:
touch filename1.txt filename2.txt filename3.txt
Create a File Using cat Command
The cat command is very frequently used commands in Linux operating system. The name of the cat
command discover from its functionality to concatenate files. It can reads data from the file and display the file content as output. You can also create, view, concatenate files using cat
command.
To create a new file, run the cat command with the redirection operator >
and the name of the file that you want to create as show below:
cat > filename.txt
Next, type the text in the file and press the CRTL+D
to save the files.
Create a File using the Redirection Operator
To create an empty file using the Redirection Operator, run the command below:
> filename.txt
Creating a File with echo Command
The echo command used to display the passed arguments to the standard output.
To create a new empty file using echo
command, run the command below:
echo > filename.txt
To create a new file with the text you want to print in the file, run the echo
command with the redirection operator >
as show below:
echo "Type some line here" > filename.txt
Create a Large File Using dd
command
If you want to create a file named filename.test
with a size of 1GB, run the the dd
command as show below:
dd if=/dev/zero of=filename.test bs=1 count=0 seek=1G
Create a Large File Using fallocate
command
fallocate
Run the below fallocate command to create a file named filename.test
with a size of 1GB:
fallocate -l 1G filename.test
That’s all
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.