Create a File in Bash
Pick up the different ways to create files via terminal!
This chapter will give you a brief introduction about File Manipulation commands which you must know before moving on to Shell Programming. File Manipulation commands lets you perform necessary operations on your files through terminal. Some of the most powerful commands are given below:
touch
Definition:
touch
command is considered one of the easiest ways to create new files because it does not overwrite the existing files unlike cp
, rm
etc. It is also used to change the timestamps of files. A timestamp can be of three types: Access Date & Time, Modification Date & Time and the Time & Date when meta information associated with file was changed. By default, the behavior of this command is set to create empty files only. You can use different options to change file’s timestamp information.
Syntax:
touch [option] [file_name(s)]
Options:
Several options can be used together to simultaneously change the timestamp information associated to the file.
Option | Meaning |
---|---|
-am | A is for Access Time and M is for Modification Time. You can change both together by using this option |
-r | Means reference. It makes a reference to another file’s timestamps and use them for your file |
-B | Means Back. This options changes time by going a few seconds back, specified by the user |
-F | Means Forward. This options changes time by going a few seconds forward, specified by the user |
-d or -t | These two options are used if the user wants to add his/her own access time in a specific format |
Example:
- Create a file named “my_file”
touch my_file
Get hands-on with 1400+ tech skills courses.