Help - Search - Members - Calendar
Full Version: Howto Delete Contents Of A File
Linuxhelp > Support > Technical Support
Robert83
Hi,

when I wan't to delete one of them /var/log log files

what is the command to delete only the contents of the file ? (so the file name will not be removed)

Sincerely
Robert B
Corey
Well, there are two options:

$ rm -rf /var/log/filename
$ touch /var/log/filename

And...

$ echo "">/var/log/filename


The second option is probably the one you are looking for. It echo's 'nothing' and using the '>' it overwrites the file. If you wanted to append to the file, you would use '>>'.
Hemant
The idea is same just in form of a shell script..
CODE
#!/bin/bash
if [ ! -d $1 -a ! -L $1 ] #the file is not a directory...niether a link
then
 do
   rm -rf "$1"
   touch "$1"
 done
fi

save the script with a name and you can pass the name of the fila as the argument.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.