How to empty the files in UNIX?



There are two ways to empty the file are set of files at a time(using loops) in unix

1). Using the symbol  " > "

  i.e. /local/home# > /bharath/file1
 now the file1 became empty.

Example:

[root@www ~]# cat file1
this is my
first file in linux
linux is rhel6.4
linux kernel dvpd by
linus torvalds
[root@www ~]# ls -l file1
-rw-r--r--. 1 root root 84 Apr 22 05:19 file1
You have new mail in /var/spool/mail/root

[root@www ~]# >file1


[root@www ~]# ls -l file1
-rw-r--r--. 1 root root 0 Apr 22 05:19 file1

[root@www ~]# cat file1
[root@www ~]#

2). using /dev/null we can empty the file as below.


[root@www ~]# ls -l file2
-rw-r--r--. 1 root root 159 Apr 22 05:22 file2

[root@www ~]# cat file2
this is linux
opensorce OS
linux is very compatiblw
Linux kernel is
develped by linus torvalds
liux os has 3 IPC's
namely semaphore,sharedmemory,message queue

[root@www ~]# cat /dev/null > file2

[root@www ~]# ls -l file2
-rw-r--r--. 1 root root 0 Apr 22 05:23 file2


[root@www ~]# cat file2
[root@www ~]#

How to empty multiple files at once using loops:

[root@www ~]# for i in `find . -xdev -type f -name "file*" -ls | awk '{print $NF}'`
> do
> echo $i
> done
./file
./file1
./file2
./file5
./file4
./file3
You have new mail in /var/spool/mail/root


[root@www ~]# for i in `find . -xdev -type f -name "file*" -ls | awk '{print $NF}'`
> do
> `cat /dev/null > $i`
> done
[root@www ~]# cat file3
[root@www ~]# cat file4
[root@www ~]# cat file5
[root@www ~]#

Comments

Post a Comment

Popular posts from this blog

Ansible for Devops

python in liunx

How to check the hardware information in Linux Systems?