Bash – Create a new file
To create a new file in Bash scripting, you can use touch
command. Call the touch
command and pass the file name as argument.
The syntax of the expression to create a new file using touch
command is given below.
</>
Copy
touch path/to/file
The touch command creates an empty file. Yeah a file already exists with the same name, then the modification time for the file is updated to the current time.
Example
In the following script, we create a new file named data.txt
, using touch
command.
example.sh
</>
Copy
#!/bin/bash
touch data.txt
echo "File created successfully."
Bash Version: GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin22.1.0)
Output
sh-3.2# bash example.sh
File created successfully.
Conclusion
In this Bash Tutorial, we learned how to create a new file using touch command.