f



Delete empty files in the current directory but not the ones in the sub-directories of current directory.

Hi all,

I want to delete empty files in the current directory but not the ones
in the sub-directories of current directory.  Any hints?

Regards.
-- 
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
0
Hongyi
2/12/2010 5:15:12 AM
comp.unix.shell 15484 articles. 3 followers. Post Follow

8 Replies
994 Views

Similar Articles

[PageSpeed] 17

Hongyi Zhao <hongyi.zhao@gmail.com> writes:

> I want to delete empty files in the current directory but not the ones
> in the sub-directories of current directory. Any hints?

As usual, ‘find(1)’ is your friend. Read about the ‘-maxdepth’ and
‘-size’ options.

-- 
 \       “People come up to me and say, ‘Emo, do people really come up |
  `\                                            to you?’” —Emo Philips |
_o__)                                                                  |
Ben Finney
0
Ben
2/12/2010 5:31:36 AM
On Fri, 12 Feb 2010 16:31:36 +1100, Ben Finney
<ben+unix@benfinney.id.au> wrote:

>As usual, ‘find(1)’ is your friend. Read about the ‘-maxdepth’ and
>‘-size’ options.

In my case, I want to delete the empty files just in the current
directory among the following names:

myfile.pdf
myfile.1.pdf
myfile.2.pdf
myfile.3.pdf
....

I use the following code:

find . -maxdepth 1 -name "myfile*".pdf  -type f -size 0 -exec rm {} \;

This will do the trick, but I still have the following issues:

1- The "myfile*".pdf regexp is too loose and may lead to some files,
say, myfileone.pdf, been deleted accidentally.  So, how can I improve
the match pattern to perform exact search and remove operations?

2- The above code is based on someone else's examples on the internet.
I don't understand the function of the *{} \* part in the end.  Any
hints?

Regards.
-- 
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
0
Hongyi
2/12/2010 5:53:04 AM
Hongyi Zhao <hongyi.zhao@gmail.com> writes:

> 2- The above code is based on someone else's examples on the internet.
> I don't understand the function of the *{} \* part in the end. Any
> hints?

Please do take the time to *read the manual page* for these tools when
you use them. The obvious place to look is the documentation for the
option to which those arguments are given.

-- 
 \          “Isn't it enough to see that a garden is beautiful without |
  `\      having to believe that there are fairies at the bottom of it |
_o__)                                             too?” —Douglas Adams |
Ben Finney
0
Ben
2/12/2010 6:30:14 AM
On 2010-02-12, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> In my case, I want to delete the empty files just in the current
> directory among the following names:

> myfile.pdf
> myfile.1.pdf
> myfile.2.pdf
> myfile.3.pdf

This is not very specific.

> find . -maxdepth 1 -name "myfile*".pdf  -type f -size 0 -exec rm {} \;

> This will do the trick, but I still have the following issues:

> 1- The "myfile*".pdf regexp is too loose and may lead to some files,
> say, myfileone.pdf, been deleted accidentally.  So, how can I improve
> the match pattern to perform exact search and remove operations?

Well, for one thing, figure out exactly what you want.  When you can
articulate it more clearly than "among the following", you will have a
better chance.  One starting point might be "myfile.?.pdf".  You might
have to think about how you'd define what names you do and don't want.

Also, for zero-size files, you're probably not in much danger of losing
data, so maybe don't worry.  Or make the files you are trying to clean up
go into a separate directory.

> 2- The above code is based on someone else's examples on the internet.
> I don't understand the function of the *{} \* part in the end.  Any
> hints?

There is a thing called a "manual" which contains an extremely detailed
description of what that thing does.

Long story short:  "-exec" provides a command.  {} is replaced by the
name of the file found, and a semicolon (;) terminates that command.
But, because ; is special to the shell, it has to be escaped so that the
shell passes it to find as an argument rather than eating it.  You
may also prefer + to ; if your find supports it.

-s
-- 
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
0
Seebs
2/12/2010 6:30:14 AM
Hongyi Zhao à écrit :

> Hi all,
> 
> I want to delete empty files in the current directory but not the ones
> in the sub-directories of current directory.  Any hints?
> 
> Regards.

With the GNU find:

$ find . -maxdepth 1 -type f -empty -delete


  LG
0
Laurianne
2/12/2010 10:19:42 AM
On 2010-02-12, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
[...]
> I want to delete empty files in the current directory but not the ones
> in the sub-directories of current directory.  Any hints?
[...]

With zsh:

rm myfile*.pdf(.L0)

-- 
Stephane
0
Stephane
2/12/2010 10:35:45 AM
On Fri, 12 Feb 2010 03:53:04 -0200, Hongyi Zhao  =

<hongyi.zhao@gmail.com> wrote:

> On Fri, 12 Feb 2010 16:31:36 +1100, Ben Finney
> <ben+unix@benfinney.id.au> wrote:
>
>> As usual, =E2=80=98find(1)=E2=80=99 is your friend. Read about the =E2=
=80=98-maxdepth=E2=80=99 and
>> =E2=80=98-size=E2=80=99 options.
>
> In my case, I want to delete the empty files just in the current
> directory among the following names:
>
> myfile.pdf
> myfile.1.pdf
> myfile.2.pdf
> myfile.3.pdf
> ...
>
> I use the following code:
>
> find . -maxdepth 1 -name "myfile*".pdf  -type f -size 0 -exec rm  =

> {} \;
>
> This will do the trick, but I still have the following issues:
>
> 1- The "myfile*".pdf regexp is too loose and may lead to some  =

> files,
> say, myfileone.pdf, been deleted accidentally.  So, how can I  =

> improve
> the match pattern to perform exact search and remove operations?
>
> 2- The above code is based on someone else's examples on the  =

> internet.
> I don't understand the function of the *{} \* part in the end.  Any
> hints?
>
> Regards.

Are just 4 pdf files?
So with bash (I don't know about others) you can try:
for f in myfile.pdf myfile.{1..3}.pdf;do [ -e $f ]&&[ ! `head -c1  =

$f` ]&&rm $f; done
0
mop2
2/12/2010 11:01:42 AM
On 2010-02-12, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> In my case, I want to delete the empty files just in the current
> directory among the following names:
>
> myfile.pdf
> myfile.1.pdf
> myfile.2.pdf
> myfile.3.pdf
> ...
>
> I use the following code:
>
> find . -maxdepth 1 -name "myfile*".pdf  -type f -size 0 -exec rm {} \;
>
> This will do the trick, but I still have the following issues:

You can't accurately match the above set with a single file glob,
because the glob language lacks disjunction. But find has disunction:
the -o (or) operator.

-maxdepth 1 \( -name myfile.pdf -o -name "myfile.[0-9].pdf" \) ...

Say, do you need a diaper change and burp?
0
Kaz
2/12/2010 4:23:47 PM
Reply: