Solved

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

Posted on 2011-09-02
10
320 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 14

Expert Comment

by:Giovanni Heward
Comment Utility
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 14

Expert Comment

by:Giovanni Heward
Comment Utility
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
Comment Utility
@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
 
LVL 14

Expert Comment

by:Giovanni Heward
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 14

Accepted Solution

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

Author Comment

by:libertyforall2
Comment Utility
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
Comment Utility
[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
Comment Utility

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
Comment Utility
Final value worked like a charm!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now