Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Emptying a text file in bash

44 views
Skip to first unread message

wke...@gmail.com

unread,
Nov 7, 2016, 5:52:13 AM11/7/16
to
Hi,
How can I empty a text file in a bash script or from command line ?

To avoid misunderstanding, what I mean is that if I have a file name myFile.txt,
which contains 3 lines like

aaa
bbb
ccc

Then after running this script/command, then running
cat myFile.txt will show nothing, and ls -al myFile.txt will show size of 0.

I of course know about the possibility of "rm myFile.txt; touch myFile.txt", but is there a way to do it in a single command ?

Regards,
Kevin

paul

unread,
Nov 7, 2016, 6:09:47 AM11/7/16
to
One way is

> myFile.txt

(exactly as written) or

truncate -s 0 myFile.txt

Ben Bacarisse

unread,
Nov 7, 2016, 6:10:56 AM11/7/16
to
wke...@gmail.com writes:

> How can I empty a text file in a bash script or from command line ?
<snip>

printf "" >myFile.txt

From the command line I use cat and type Ctl-D (just because of muscle
mempry, I suspect) though you could use cat in a self-contained way like
this:

cat >myFile.txt </dev/null

--
Ben.

Chris Elvidge

unread,
Nov 7, 2016, 6:30:18 AM11/7/16
to
Try: "> myFile.txt" (without the " obviously)


--

Chris Elvidge, England

Martin Vaeth

unread,
Nov 7, 2016, 7:53:31 AM11/7/16
to
Chris Elvidge <ch...@mshome.net> wrote:
>>
> Try: "> myFile.txt" (without the " obviously)

This wouldn't work in zsh unless e.g. NULLCMD=: is set.
More compatible is

: >|myFile.txt

(the "|" overrides a possible noclobber option of some shells)

Christian Weisgerber

unread,
Nov 7, 2016, 9:30:08 AM11/7/16
to
On 2016-11-07, wke...@gmail.com <wke...@gmail.com> wrote:

> I of course know about the possibility of "rm myFile.txt; touch myFile.txt", but is there a way to do it in a single command ?

: >myFile.txt

--
Christian "naddy" Weisgerber na...@mips.inka.de

Ivan Shmakov

unread,
Nov 9, 2016, 2:05:18 PM11/9/16
to
>>>>> paul <pa...@donotuse.me.invalid> writes:
>>>>> On Mon, 7 Nov 2016 02:52:11 -0800 (PST), wke...@gmail.com wrote:

[...]

>> Then after running this script/command, then running cat myFile.txt
>> will show nothing, and ls -al myFile.txt will show size of 0.

>> I of course know about the possibility of "rm myFile.txt; touch
>> myFile.txt", but is there a way to do it in a single command ?

> One way is

> > myFile.txt

Or, as already suggested, ">|" can be used in place of ">" so
that the command does not fail when "set -o noclobber"
("set -C") is in effect.

But please note that there's a semantic difference between
"rm && touch" and ">". Namely, "rm && touch" /replaces/ the
file with a /new/, empty one. Should the file happen to have
other names ("hardlinks"), they will still refer to the original
data; also, the original data will still be accessible by the
processes, if any, having the file open.

On the contrary, ">" (">|") replaces the file contents in place.
Any and all processes accessing that file will find it empty.

Generally, unless the intent is to, for some reason, instantly
remove the data stored in a file, it makes sense to replace one
instead. A better way to do so is to create a temporary file
and use "mv" to replace the original /atomically/; say:

## Usage: replace_with_empty FILENAME
replace_with_empty () {
## NB: mktemp(1) is non-POSIX.
rwe_f_=$(mktemp -- "$1".XXXXXXXX) \
&& mv -f -- "$rwe_f_" "$1"
}

Although that's not without a flaw, as the new file created will
be accessible only by the user. With GNU Coreutils, it's
possible to copy the access mode by adding chmod(1), and perhaps
chown(1), calls to the code above; like:

&& (chmod --reference="$1" -- "$rwe_f_" ;
chown --reference="$1" -- "$rwe_f_" ; :) \

> (exactly as written) or

> truncate -s 0 myFile.txt

Note, however, that like mktemp(1), truncate(1) is non-POSIX.
But with the GNU Coreutils version, it's possible to also use
"-c" (--no-create), so to do nothing if the file does not exist.

--
FSF associate member #7257 np. Memories and Stories -- Night Breeze
0 new messages