Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some directories with a varying numbers of files and subdirectories. All files that have no extensions have to be tared. Their names may change, thus they can not be hardcoded. Subdirectories may also contain a unspecified number of files and subdirectories that are not relevant and should not be included in the archive.

I'd like to be able to do this with a single line, not using any .sh scripts, so that this process could be reproduced as portably as possible.


Sample directory structure:

$ ls -1F

bar-dir/
bar.with.dots
foo-directory/
foo-no-ext
quux.dir/
quux.ext

The resulting tar should contain only foo-no-ext in this case.


The closest I've come up with this far is

tar -cf archive.tar --exclude=*.* --no-recursion *

But, when executed on the sample set, the archive.tar still includes the subdirectories (even though empty):

$ tar -tf archive.tar

bar-dir/
foo-directory/
foo-no-ext
share|improve this question
1  
Using a shell script might be more portable than you think. For example, bash or perl is exactly the same on all systems, while tar may be GNU, BSD, Solaris tar... – grawity Sep 1 '11 at 18:33
up vote 3 down vote accepted

This command does what you want:

 find * -maxdepth 0 -type f | tar -cf archive.tar --exclude=*.* -T -

The find command finds all files * only in the current directory -maxdepth 0 that are files -type f (not directories, devices or other special files). The resulting files are passed to tar which excludes the names containing a dot.

share|improve this answer
    
This doesn't accept filenames with spaces. find ... | tar -T - ... would be somewhat better. – grawity Sep 1 '11 at 19:55
    
@grawity Edited as you suggested, now it allows filenames with spaces. – cantfork Sep 1 '11 at 20:47
    
I would suggest -type f unless you want to find also links, sockets, pipes and device nodes. – matthias krull Sep 2 '11 at 19:27
    
@mugen I took your suggestion and edited the command. – cantfork Sep 3 '11 at 1:14

How about:

tar -cf archive.tar `ls|grep "^[^.]*$"`

It doesn't expect much out of the shell, but assume that grep exists (I've never seen a system without it).

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .