- Forums
- Linux Systems
- How To Empty A Large File In Linux Without Deleting It
follow this tutorial that will show you how to empty a large file to make it small size without having to delete it using a command in the shell in linux. [2879], Last Updated: Sat May 18, 2024
unix
Sat Jan 20, 2007
10 Comments
21777 Visits
Linux General Good HousekeepingOften when i login into my shell, i get this message:
CODE:
[root@host file]#
You have new mail in /var/spool/mail/root
then to find out how big the size of the file is i send this command:
CODE:
[root@host file]#
total 1276
drwxrwxr-x 2 root mail 4096 Jan 20 14:59 .
drwxr-xr-x 14 root root 4096 Nov 21 13:15 ..
-rw------- 1 root root 1284052 Jan 20 14:59 root
ok, one meg is not that bad, but lets say i wanted to empty the file, not delete it, so to do that, the process of reducing an existing file to a file with the same name that is 0 bytes large is called "truncating."
For creating a new empty file, the same effect is obtained with the touch command. On an existing file, touch will only update the timestamp. See the Info pages on touch for more details.
so to empty my root mail file, i would send this command:
CODE:
tail
To "almost" empty a file, use the tail command. Suppose my error.log file becomes rather long because im gettin alot of errors on my web pages. now to remove all but the last 5 lines i can send this command:
first i want to see how large my file is, from this command you can see its 338384078 bytes
CODE:
[root@ log]# ls -la
total 330792
drwxr-xr-x 2 wallpaperama 4096 Dec 23 13:48 .
drwxr-xr-x 14 wallpaperama 4096 Feb 27 21:49 ..
-rw-r--r-- 1 wallpaperama 338384078 Mar 2 11:53 error.log
Now remove all the lines except the last 5
CODE:
tail -5 error.log > error.log
now i resend the list command and now my error.log file is only 439 bytes
CODE:
[root@ log]# ls -la
total 12
drwxr-xr-x 2 wallpaperama 4096 Dec 23 13:48 .
drwxr-xr-x 14 wallpaperama 4096 Feb 27 21:49 ..
-rw-r--r-- 1 wallpaperama 439 Mar 2 11:55 error.log