Unix shell script to truncate a large file
source link: https://www.codesd.com/item/unix-shell-script-to-truncate-a-large-file.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Unix shell script to truncate a large file
I am trying to write a Unix script which will truncate/empty a file which is continuously being written/open by an application when it reaches say 3GB of space. I know that the below command would do it :
cp /dev/null [filename]
But I am going to run this in an production environment automatically as a cron job - just posting here to see if you guys faced any issues while doing something similar to this.
Just to add another answer,
: > filename
:
is a no-op in bash, so this essentially just opens the file for writing (which of course truncates the file) and then immediately closes it.
EDIT: as shellter commented, you don't actually need a command to go along with the redirection:
$ echo foo > foo.txt
$ cat foo.txt
foo
$ > foo.txt
$ cat foo.txt
$
A simple redirection all by itself will clear the file.
Related Articles
How to convert the xml file to a properties file using a Unix shell script
Unix shell script: update the timestamp on all subdirectories and subfiles, including those with spaces
How do I have the settings listed in a settings page and refer them to a Unix shell script
Run the Perl script from Unix Shell Script
Can the Unix shell script be used to manipulate databases?
Arithmetic dates in Unix shell scripts
Need help solving a small error in a Unix shell script
What is the concise way to check that environment variables are defined in a Unix shell script?
Validating the Date in the Unix Shell Script (ksh)
How do you tell if a string contains another string in Unix shell scripts?
Reading in the directory, Unix shell script
Executing Unix Shell Scripts in the Open Talend Studio for Large Data
UNIX shell script to read files, copy files, and delete files
Unix shell script to archive files on the remote system
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK