How do I create a new empty file in a bash script?
Use touch
command. touch filename.txt
.
Use the null command (:
) redirect (> filename
) trick (:>
), as this will truncate to zero or create the named file.
$ echo foo > filea
$ :> filea
$ wc -c filea
0 filea
$ rm filea
$ :> filea
$ wc -c filea
0 filea
(This will fail if the shell sets a NOCLOBBER option.)
You could always use perl, too.
$ stat filename.txt
stat: cannot stat 'filename.txt': No such file or directory
$ perl -e 'open($fh,">","filename.txt") or die $!;close($fh)'
$ stat filename.txt
File: 'filename.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 280728 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ xieerqi) Gid: ( 1000/ xieerqi)
Access: 2017-02-08 13:51:01.479121995 -0700
Modify: 2017-02-08 13:51:01.479121995 -0700
Change: 2017-02-08 13:51:01.479121995 -0700
Birth: -