/dev/null a file using xargs


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers /dev/null a file using xargs
# 1  
Old 07-11-2011
Data /dev/null a file using xargs

Hi,

I'm currently using the following command to wipe clean a log file which can't be straight out RM'd:
Code:
cat /dev/null > server.log

I'm building this into a script and I'm current working on a command to run on each machine to do this automatically however I have multiple files so I need to do these all at the same time by running a check using 'find' and listing files above 10G.
Code:
find /data/logs/ -name server.log -size +10G | xargs cat /dev/null >{}

Looking for a solution similar to the above that will pass /dev/null to the file, blanking it essentially.

Any help is appreciated. Smilie

Last edited by vbe; 07-11-2011 at 09:40 AM.. Reason: code tags...
# 2  
Old 07-11-2011
How sure are you that this will work? Truncating an open file is not guaranteed to release the space it uses, and the process(es) that already have the file open could very well just write more data at their current offset, which at best will create a sparse file.

The only way to make something like this work is if you have control of the application(s) generating the log file(s).

And if you have control of the application(s), you don't need a kludge like this because you'd have the app(s) properly manage their log files.
# 3  
Old 07-11-2011
It seems to have worked previously from what I've seen when running the command manually, so I see no reason why it wouldn't work now.

Any suggestions?
# 4  
Old 07-11-2011
Code:
cat /dev/null > something

magic power is kind of a urban legend. "cat /dev/null" does pretty much nothing useful and can be replaced by the null or even an empty command, i.e.
Code:
: > server.log

or simply
Code:
> server.log

Beyond that, I concur with achenle it is an unreliable way to zero out an open log file.
# 5  
Old 07-11-2011
how about doing it all in find instead of piping to xargs...
Code:
find /data/logs/ -name server.log -size +10G -exec cp /dev/null {} \;

thogh i am not sure if 10G is a valid argument to -size.
# 6  
Old 07-11-2011
Again, there is no magic with /dev/null, whether it is used with cat or cp.

Here is something both optimized and a little cryptic Smilie :
Code:
find /data/logs/ -name server.log -size +10G -exec sh -c 'for i; do > $i; done' {} +

"+10G" is a GNU find extension, otherwise, "+2097152" would equally work and with any standard compliant find.
# 7  
Old 07-11-2011
Quote:
Originally Posted by jlliagre
Again, there is no magic with /dev/null, whether it is used with cat or cp.

Here is something both optimized and a little cryptic Smilie :
Code:
find /data/logs/ -name server.log -size +10G -exec sh -c 'for i; do > $i; done' {} +

"+10G" is a GNU find extension, otherwise, "+2097152" would equally work and with any standard compliant find.
And so is this...
Code:
find /data/logs/ -name server.log -size +10G -exec cp /dev/null {} +

addition of the trailing + prevents the shell from spawning a new process for every file Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

2>/dev/null

Friends have the following problem a search may not find anything which would correct example: ls -ltr *prueba.txt | nawk '{ print $9 }' > Procesar.dat 2>/dev/null When he finds nothing gives me the following error ls: prueba.txt: No such file or directory because 2> / dev / null... (4 Replies)
Discussion started by: tricampeon81
4 Replies

2. AIX

Size of file /dev/./null 2>&1

Hello Can I just remove/delete flile ? rw-r--r-- 1 root system 2385088512 Jun 30 21:25 /dev/null 2>&1 size of this flile is 2274.75 m and fill up my filesystem: Filesystem MB blocks Used Free %Used Mounted on /dev/hd4 2560.00 2558.59 1.41 100% / (10 Replies)
Discussion started by: primo102
10 Replies

3. AIX

/dev/null file issue

Hi Experts, I Have a query. In one of my server I just came to know that there was /dev/null file which is a not a character file. Its just a normal file. I know the command to create the character file (/dev/null) but what is the procedure. Like should i delete /dev/null and create or... (7 Replies)
Discussion started by: jayadeava
7 Replies

4. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

5. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

6. Solaris

What is /dev/tty /dev/null and /dev/console

Hi, Anyone can help My solaris 8 system has the following /dev/null , /dev/tty and /dev/console All permission are lrwxrwxrwx Can this be change to a non-world write ?? any impact ?? (12 Replies)
Discussion started by: civic2005
12 Replies

7. UNIX for Advanced & Expert Users

/dev/NULL can't open this file ??

when i write the following two statements : cp /dev/NULL /clocal/mqbrkrs/user/mqsiadm/sanjay/dspmq_temp cat /dev/NULL > /clocal/mqbrkrs/user/mqsiadm/sanjay/dspmq_temp its gives me errors like : cp: /dev/NULL: A file or directory in the path name does not exist. cat : /dev/NULL can't open... (2 Replies)
Discussion started by: varungupta
2 Replies

8. Solaris

URGENT - setup port and dump all output to /dev/null or a file

Please help urgently. I need to setup up some sort of service on a solaris server on a port. I dont need it do anything special, anything that is sent to this port from an external server should be dump to /dev/null or a flat file.. Can you help urgently? (1 Reply)
Discussion started by: frustrated1
1 Replies

9. UNIX for Advanced & Expert Users

Q1 :/dev/null Q2 -A

Hi, Q1-What does nroff -ms > /dev/null Q2- What does mean -A under STAT column : ps aux |head -20 UTIL PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND root 516 93,0 0,0 12 12 - A 04 nov 3906:51 wait Thank you. (4 Replies)
Discussion started by: big123456
4 Replies

10. UNIX for Dummies Questions & Answers

Creating a file / /dev/null

First of all, hello there cOmMuNity ! :cool: Well, I've got two basic questions: 1) In how many ways it's possible to create a file ? I know one manner using "touch", other typing something like echo "" > file ... The fact is that I need to overwrite the file if it exists, and touch... (8 Replies)
Discussion started by: 435 Gavea
8 Replies
Login or Register to Ask a Question