Posted on May 5, 2023 by nexonhost
How To Create A File In Linux.
In this tutorial, we’ll show you various ways to quickly create a new file in Linux using the command line.
Knowing how to create a new file is an important skill for anyone using Linux on a regular basis. You can create a new file either from the command line or from the desktop file manager.
Before you Begin
To create a new file you need to have write permissions on the parent directory. Otherwise, you will receive a permission denied error.
If you want to display the contents of a directory use the ls command .
Creating a File with touch Command
The touch command allows us to update the timestamps on existing files and directories as well as creating new, empty files.
The easiest and most memorable way to create new, empty files is by using the touch command.
To create a new file simply run the touch command followed by the name of file you want to create:
If the file file1.txt doesn’t exist the command above will create it, otherwise, it will change its timestamps.
To create multiple files at once, specify the file names separated by space:
touch file1.txt file2.txt file3.txt
Creating a File with the Redirection Operator
Redirection allows you to capture the output from a command and send it as input to another command or file. There are two ways to redirect output to a file. The > operator will overwrite an existing file, while the >> operator will append the output to the file.
To create an empty zero-length file simply specify the name of the file you want to create after the redirection operator:
This is the shortest command to create a new file in Linux.
When creating a file using a redirection, be careful not to overwrite an important existing file.
Creating a File with cat Command
The cat command is mainly used to read and concatenate files, but it can also be used for creating new files.
To create a new file run the cat command followed by the redirection operator > and the name of the file you want to create. Press Enter type the text and once you are done press the CRTL+D to save the files.
cat > file1.txt
Creating a File with echo Command
The echo command prints the strings that are passed as arguments to the standard output, which can be redirected to a file.
To create a new file run the echo command followed by the text you want to print and use the redirection operator > to write the output to the file you want to create.
echo "Some line" > file1.txt
If you want to create an empty simply use:
echo > file1.txt
Creating a File using Heredoc
Here document or Heredoc is a type of redirection that allows you to pass multiple lines of input to a command.
This method is mostly used when you want to create a file containing multiple lines of text from a shell script.
For example, to create a new file file1.txt you would use the following code:
cat << EOF > file1.txtSome lineSome other lineEOF
The body of the heredoc can contain variables, special characters, and commands.
Creating a Large File
Sometimes, for testing purposes, you might want to create a large data file. This is useful when you want to test the write speed of your drive or to test the download speed of your connection.
Using dd command
The dd command is primarily used to convert and files.
To create a file named 1G.test with a size of 1GB you would run:
dd if=/dev/zero of=1G.test bs=1 count=0 seek=1G
Using fallocate command
fallocate a command-line utility for allocating real disk space for files.
The following command will create a new file named 1G.test with a size of 1GB:
fallocate -l 1G 1G.test
In this tutorial, you learned how to create a new file in Linux from the command line using various commands and redirection.