?
Solved

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

Posted on 2011-09-02
10
Medium Priority
?
329 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Simple Linear Regression
Six Sigma Control Plans
Suggested Courses

752 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