Solved

delete all blank files in a directory using shell script in command line

Posted on 2011-09-02
10
326 Views
Last Modified: 2012-05-12
If I have /path/so2 and I want to delete all files that are blank in that directory what command would I use?
0
Comment
Question by:libertyforall2
10 Comments
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 36476209
This should do it from the command-line...

 
for %f in (*.*) do if not %~zf gtr 0 del %f

Open in new window

0
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 36476235
Here is a scripted version...
@echo off
for /r %%f in (\path\so2\*.*) do (
	if %%~zf equ 0 (
		set /p p=%%f zero bytes... <nul
		del %%f
		echo deleted!
	)
)

Open in new window

0
 
LVL 26

Expert Comment

by:pony10us
ID: 36476269
@x66_x72_x65_x65:

Just wanted to point out that the scripted version you have specifies the location (\path\so2) that the asker mentioned however the one liner does not.

:)
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 36476308
Right-- one liner was intended for use in current folder (current folder being ...  \path\so2 ) <--- DISCLAIMER
:)

Here is another ver with some error checking...

 
@echo off
for /r %%f in (\path\so2\*.*) do (
	if %%~zf equ 0 (
		set /p p=%%f zero bytes... <nul
		del "%%f"/f/q/a>nul 2>&1||echo remove failed!
		if not exist %%f echo removed.
	)
)

Open in new window

0
 

Author Comment

by:libertyforall2
ID: 36476325
I get this error message for the command line below. I already cd'd into the current directory. I tried stating the directory after the f and got the same message

[rhuff@huina so2]$ for %f in (*.*) do if not %~zf gtr 0 del %f
Badly placed ()'s.
0
 
LVL 15

Accepted Solution

by:
Giovanni Heward earned 250 total points
ID: 36476334
The script is for windows... sorry. :(
0
 

Author Comment

by:libertyforall2
ID: 36476339
Tried the scripted version and got this

[rhuff@huina ~/scripts]$ sh zero.sh
zero.sh: line 1: @echo: command not found
zero.sh: line 2: syntax error near unexpected token `%%f'
zero.sh: line 2: `for /r %%f in (/path/path/path/path/so2*.*) do ('
[rhuff@huina ~/scripts]$

Also so2 is part of the path name so /path/so2/ all files would be after so2/
0
 

Author Comment

by:libertyforall2
ID: 36476342
[rhuff@huina ~/scripts]$ sh zero.sh
zero.sh: line 1: @echo: command not found
zero.sh: line 2: syntax error near unexpected token `%%f'
zero.sh: line 2: `for /r %%f in (/share/huina/rhuff/forecastfiles/so2*.*) do ('
[rhuff@huina ~/scripts]$ sh zero.sh
zero.sh: line 1: @echo: command not found
zero.sh: line 2: syntax error near unexpected token `%%f'
zero.sh: line 2: `for /r %%f in (\/share/huina/rhuff/forecastfiles/so2\*.*) do ('
[rhuff@huina ~/scripts]$ sh zero.sh
zero.sh: line 1: @echo: command not found
zero.sh: line 2: syntax error near unexpected token `%%f'
zero.sh: line 2: `for /r %%f in (\share/huina/rhuff/forecastfiles/so2\*.*) do ('
[rhuff@huina ~/scripts]$
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
ID: 36476479

As you put Powershell in the zones, everyone assumed you were wanting a Windows solution.


For *nix

find /path/so2 -type f -size 0 | xargs rm

Open in new window

0
 

Author Closing Comment

by:libertyforall2
ID: 36476494
Final value worked like a charm!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question