In the past I have shown ways to run Linux scripts on windows based system. I have also talked about one use of the
find command, in conjunction with
grep command to search for files having a given text content (say a word) in multiple folders.
Today I will show you another use of the
find command: to automate tasks such as deleting zero byte files. This is a pretty common cleanup task that’s carried out on machines that are involved in EDI file transfers.
Here is the script:
find . -size 0 | sed -e 's/^/rm /' | sh
It deletes all zero byte files in the current folder, and in the folders below it.
In order to understand this, you may need to read about pipes. The task is carried out in three steps:
find . -size 0searches for all zero byte files in the given folder and the folders below it and returns the filepaths.
sed -e 's/^/rm /'turns the list of names into a script – for example if the name is ‘/data/x’ it changes it to ‘rm /data/x’. More on sed here.
- the last steps simply forwards the script to the Linux shell for execution.
This is also a very flexible script and can be customised to carry out a wide variety of tasks. Please post your variations as comments.