Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-22-2005, 06:29 PM   #1
LQ Newbie
Registered: Nov 2003
Posts: 3

Rep: Reputation: 0
Truncate A File

I hope this is a simple question.

What's the quickest and easiest way to truncate a file to zero bytes?

I have a FreeBSD file server and I can just use the command:
truncate -s 0 filename

My CentOS3.5 machine does not seem to have the "truncate" command on it. If it does, I'm not finding it. There are man pages for "truncate" but the command is not found.

So, whats the easiest way to make a file zero bytes in Linux?

Old 10-22-2005, 06:37 PM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,407
Blog Entries: 15

Rep: Reputation: 1424Reputation: 1424Reputation: 1424Reputation: 1424Reputation: 1424Reputation: 1424Reputation: 1424Reputation: 1424Reputation: 1424Reputation: 1424
"> filename"

Doing a redirect to a file with no input makes it 0 bytes.

By the way that would work in BSD and other Unix flavors as well. I imagine the "truncate" command is designed more for removing SOME of the contents rather than all of them. Its a new one on me though so thanks for sharing it as I do have some BSD systems.
Old 10-22-2005, 06:58 PM   #3
LQ Newbie
Registered: Nov 2003
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks jlightner . . . that's about as simple as it can get!

Yes, the "truncate" command can be used to remove some of the contents but the -s switch sets the "size" of the resulting file so "-s 0" means make the file 0 bytes.

Thanks for the reply.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
truncate a text file in CLI? hottdogg Linux - General 2 09-06-2005 09:32 PM
truncate lines with c alaios Programming 17 08-25-2005 08:51 AM
Zipslack 9.1 long file names truncate jimk Slackware - Installation 5 05-16-2004 06:38 AM
how to use truncate sureshbabu Linux - Newbie 1 10-29-2003 04:22 AM
truncate command butthead Linux - General 1 05-14-2002 03:40 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration