I'm running some third-party Perl script written such that it requires an output file for the output flag, -o.

Unfortunately, the script appears to require an actual file, that is, users must create an empty file filename.txt with 0 bytes and then input this empty file on the script command line

perl script1.pl -o filename.txt

Question: How would I create an empty file within a bash script? If one simply tries perl script1.pl -o filename.txt, the script gives an error that the file doesn't exist.


5 Answers 5


Use touch command. touch filename.txt.

  • 22
    echo -n >file
    – Hannu
    Commented Feb 8, 2017 at 21:48
  • 14
    take care - this will not empty your file if it already exists
    – johanvdw
    Commented Jun 25, 2018 at 11:30
  • 1
    Actually I tried touch and "echo -n > file" inside my bash script but I get an error when I execute the script: "No such file or directory" on that line. Can someone help please?
    – andrewz
    Commented Mar 5, 2020 at 21:07
  • 5
    echo -n "" > filename will empty the file if it exists or creates a file if it doesn't exist.
    – Vituvo
    Commented Mar 18, 2020 at 9:09

The shortest way:

  • 2
    Best answer. Couldn't be shorter.
    – DevonDahon
    Commented Dec 17, 2020 at 16:46
  • Could you provide a reference to an explanation, please?
    – Alexey
    Commented Oct 8, 2022 at 12:52
  • 1
    it's related to redirecting output on bash[n]>[|]word
    – manus
    Commented Apr 18, 2023 at 3:32

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.)

  • 1
    If noclobber is set, use the redirection >| to clobber, so :>| filea -- documented in the manual Commented Feb 8, 2017 at 20:21
  • 4
    Just > filename.txt would do, no need for the :. Or >> filename.txt if we don't want to trash it if it does happen to exist.
    – ilkkachu
    Commented Feb 8, 2017 at 21:57
  • 3
    @ilkkachu although the question does specify Bash, using : does make the script more likely to accomplish its task with other shells e.g. Zsh. Commented Feb 19, 2017 at 9:22

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: -

dd can be used to create an empty file as follows:

dd if=/dev/null of=filename.txt count=0

One case where dd is more useful than the other approaches is creating an EFI variable (PK) through the efivars filesystem (/sys/firmware/efi/efivars).

One some platforms and Linux distributions, the file is created with the "immutible" attribute set (shows up as the "i" flag with "lasattr"). So, any subsequent operations on the file fail.

For example, touch not only creates the file, but sets the time stamp on the file. Similarly, piping to the file with > not only creates the file, but performs additional operations that fail. The dd command does not appear to fail the same way.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .