How to clear the contents of a file from the command line?
I have a log file that has a bunch of stuff in it that I don't need anymore. I want to clear the contents.
I know how to print the contents to the screen:
cat file.logI know how to edit the file, line-by-line:
nano file.logBut I don't want to delete each line one at a time. Is there a way to do it in one command without destroying the file to do it?
18 Answers
In bash, just
> filenamewill do. This will leave you with an empty file filename.
PS: If you need sudo call, please consider to use truncate as answered here.
You can use the user command : truncate
truncate -s 0 test.txt("-s 0" to specify the size)
5You could do this:
echo -n "" > file.logUsing > to write the (null) input from echo -n to the file.
Using >> would append the null input to the file (effectively doing nothing but touching it).
: > file.logSame as > filename in Bash, but works in more shells (credit). Redirects the output from the true builtin (which has no output) to filename.
ZSH
>! filenameZSH will protect users from clobbering files using the io redirect operator >. If you use >! you can force the truncation of an existing file.
If you want ZSH to use Bash's redirection behavior, were there is no protection from file clobbering, then you need to set the clobber option for your shell.
More Info:
IF you want to do from inside a vim editor in command line, you can try this:
vim file.txt Press Esc.
:1,$dPress Enter.
You will find all lines deleted.
2Below command should also work :
cat /dev/null > file.log 2 $ rm file.log; touch file.logor
$ cat > file.logfollowed by control-d.
or...or...or...
Ah. Here is a single command version:
$ dd if=/dev/null of=file.log 5 If you need to sudo to superuser privilege to access the file then the accepted answer will not work. Using this does work:
truncate -s0 fileor explicitly with sudo:
sudo truncate -s0 fileMore info here
It can be done using sed
sed -i d filename
Few alternatives:
ex +%d -scwq file.log
cp /dev/null file.log
vi +%d -escwq file.log
install -m600 /dev/null file.log If you have spaces in filenames, use:
for file in /path/to/file/*; do > "$file"; done(I could not to include it in comments to previous answer because I don't have 50 reputation. Sometimes limitations are contra productive.)
There are multiple ways to clear the file as listed below:
echo "" > filename
cat /dev/null > filenameBelow examples are mostly used in shell scripting
just# > filename
: > filename One line at a time?
Try vi(m), the lovely text editor that can do anything. In this case, navigate to a line, press d (for delete), and d again (for line).
2With my permissions this is the only thing that worked:
touch temp.txt
sudo mv temp.txt original-file.txt Also, if you have multiple files you can use the following:
for file in /path/to/file/*; do > $file; doneThis is helpful for log files in the same directory.
In windows environment:
type nul >filename here is some test i have done: cat test cp test test.bkp cat /dev/null > test
This will empty the original test and but you can still access the test.bkp if you need the old contents of the file.
1