When dealing with files in Linux , sometimes you need to empty it without opening the actual file . Also , this is very useful is safely emptying a dynamically writing file, especially some of the log files . So we are looking on some of the commands to do this .
Empty using echo command, say we are emptying the access.log
echo "" > access.log
echo > access.log
So the normal echo commands with redirect output will leave an empty line at the end . To send a null output , use -n flag which tells echo to not output the trailing newline that leads to the empty line produced in the previous command.
echo -n "" > access.log
Empty using dd , cat and cp commands
dd if=/dev/null of=access.log
cat /dev/null > access.log
cp /dev/null access.log