16

Unix shell script to truncate a large file

 3 years ago
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

advertisements

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

I have an xml file source.xml. I want to create an property file with key-value pairs from this xml file info. <?xml version="1.0" ?> <persons> <person> <name>John</name> <family-name>Smith</family-name>

Unix shell script: update the timestamp on all subdirectories and subfiles, including those with spaces

Here's what I'm trying to achieve. I have a directory with over 4200 sub-files/sub-directories, all of which need to be set to a certain timestamp. This is a problem because many of them have whitespaces and other weird characters. First thing I trie

How do I have the settings listed in a settings page and refer them to a Unix shell script

I have a Unix shell script where i have some oracle connectivity information to be passed to. My lead requested me to pass the parameters from a parameter page since the connectivity information can change from environment to environment. How can I d

Run the Perl script from Unix Shell Script

Hi I have a Unix Shell Script call Food.sh ; I have a Perl Script call Track.pl. Is there a way where I can put Track.pl's code in to Food.sh code and still have it work ? Track.pl requires one arugement to label a name of a folder. Basically it will

Can the Unix shell script be used to manipulate databases?

I have to read data from some files and insert the data into different tables in a database. Is Unix shell script powerful enough to do the job? Is it easy to do the job in shell script or should I go about doing this in Java?If the data you are tryi

Arithmetic dates in Unix shell scripts

I need to do date arithmetic in Unix shell scripts that I use to control the execution of third party programs. I'm using a function to increment a day and another to decrement: IncrementaDia(){ echo $1 | awk ' BEGIN { diasDelMes[1] = 31 diasDelMes[2

Need help solving a small error in a Unix shell script

I am having a problem with a unix shell script. The script is for renaming the Files and directories which is having spaces with "-" recursively. Example "Stack Overflow" to "Stack-Overflow" This is running perfectly and work

What is the concise way to check that environment variables are defined in a Unix shell script?

I've got a few Unix shell scripts where I need to check that certain environment variables are set before I start doing stuff, so I do this sort of thing: if [ -z "$STATE" ]; then echo "Need to set STATE" exit 1 fi if [ -z "$DEST&

Validating the Date in the Unix Shell Script (ksh)

I am validating the date in Unix shell script as follow: CheckDate="2010-04-09" regex="[1-9][0-9][0-9][0-9]-[0-9][0-9]-[0-3][0-9]" if [[ $CheckDate = *@$regex ]] then echo "ok" else echo "not ok" fi But ksh it is gi

How do you tell if a string contains another string in Unix shell scripts?

I want to write a Unix shell script that will do various logic if there is a string inside of another string. For example, if I am in a certain folder, branch off. Could someone please tell me how to accomplish this? If possible I would like to make

Reading in the directory, Unix shell script

I am trying to analyze the files/directories inside of a directory using a shell script, for example if the file is readable, if it is a file, if it is a directory, etc. My script is set up to take a directory as input. so I would type 'file.sh direc

Executing Unix Shell Scripts in the Open Talend Studio for Large Data

Is it possible to execute a shell script in talend, the same way we run scrips in informatica using command task? I am new to talend. I have few scripts running in UNIX servers through informatica, Can i achieve the same using TALEND? I have installe

UNIX shell script to read files, copy files, and delete files

Hi I am writing a shell script in UNIX which reads a directory and copies the files in to another directory and then deletes the files in the first directory I have tried this but it does not work:- files"=/project/scripts/input/" for f in $file

Unix shell script to archive files on the remote system

I have a requirement to archive files on remote location. i.e., I need to write a shell script that will connect to remote path copy(move) files from this path and then paste them on another location in the same system (The target system could be eit

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK