[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-09-02
10
Medium Priority
?
332 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Make the most of your online learning experience.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Progress

872 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