32

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.

3

5 Answers 5

40

Use touch command. touch filename.txt.

4
  • 16
    echo -n >file
    – Hannu
    Feb 8, 2017 at 21:48
  • 9
    take care - this will not empty your file if it already exists
    – johanvdw
    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
    Mar 5, 2020 at 21:07
  • 3
    echo -n "" > filename will empty the file if it exists or creates a file if it doesn't exist. Mar 18, 2020 at 9:09
11

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

3
  • 1
    If noclobber is set, use the redirection >| to clobber, so :>| filea -- documented in the manual 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
    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. Feb 19, 2017 at 9:22
7

The shortest way:

>filename
1
  • 1
    Best answer. Couldn't be shorter.
    – DevonDahon
    Dec 17, 2020 at 16:46
3

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.