Shell expansion of * and hidden files

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Aaron Gaudio wrote:
>
> $ mkdir foobar
> $ cd foobar
> $ touch .bar
> $ tar cvf ../foobar.tar .
> ./
> ./.foo

Robert P. J. Day replied:
> true, since the example you initially showed claimed that tar picked 
> up hidden files, but it only did so since that hidden file was in a 
> subdirectory.
> 
> naturally, if you put that hidden file in the current directory and 
> back up based on ".", it's going to work.  that was my whole point.
> 
> i think we've flogged this sufficiently for one day.

It looks like you're still missing something important.

Is this what you're complaining about? That .oink isn't picked up?

[james@howells tmp]$ mkdir moo
[james@howells tmp]$ cd moo
[james@howells moo]$ touch baa .oink
[james@howells moo]$ tar cvf woof.tar *
baa

Despite appearances, that is NOT tar "not picking up hidden files". It's
the shell. Tar is doing exactly what it's asked to do: the shell expands
* to all the non-hidden files in the directory (baa) and passes that to
tar. Tar never sees the star, or .oink.

[james@howells moo]$ tar cvf woof.tar baa .oink
baa
.oink

This is important because it is standard shell behaviour. Since * is
expanded by the shell, you can predict how it's going to work everywhere.
If you don't know how it's going to work, you're going to get unpleasant
surprises.

Now if you're complaining that there's no easy way in bash to match all
files, including hidden files, but excluding . and .., *then* we're
talking. The most straightforward I can think of at this time of night
is

[james@howells moo]$ find . -maxdepth 1 | egrep -v '^\.$' | xargs tar cvf woof.tar ./.oink
./baa

(which obviously gives the ./ prefix), or

[james@howells moo]$ find . -maxdepth 1 | cut -c3- | xargs tar cvf woof.tar
.oink
baa

or

[james@howells moo]$ ls -d * .?* | egrep -v '^\.\.$' | xargs tar cvf woof.tar
baa
.oink

which don't. And we still aren't handling spaces (but neither does
tar cvf woof.tar *
)...

Obviously, this is not nearly as clean as it could be. It's where the
shell starts giving out.

James.

-- 
E-mail address: james@ | We still have enough spare cardboard sitting around
westexe.demon.co.uk    | to send a bus by Parcelforce, although not enough
                       | wrapping to be sure they wouldn't deliver it broken
                       | into two pieces.  -- Alan Cox



[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux