Solved

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

Posted on 2011-09-02
10
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Introduction to Processes
Starting up a Project

705 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