Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to archive a project folder with hidden files and folders, and I get one with a root entry of .. It's annoying. Typically :

tar -czf $ARCHIVE_NAME -C $DIR .

will create an archive like

./package.json
./.git
./.gitignore
.
.
.

where I'd like the root of the archive to be the actual files and folders, not having a . entry. Like

package.json
.git
.gitignore
.
.
.

How do I do that?

* Edit *

The below image may be helpful clarifying this question.

  • On the left, an archive created with the GUI tool of the project (from two days ago).
  • On the right, an archive created with the above tar command.

Notice the "Location" bar; the archive on the right should be /, and not /./.

enter image description here

share|improve this question
    
A process will respect the filesystem. If you have marked a file as hidden then the process cannot see it unless you esclated its own permission so it can see it. But that sort of makes the fact the file is hidden pointless so why don't you just not make the files hidden –  Ramhound Jul 17 '14 at 12:33
    
@Ramhound, hmm... because .git cannot be renamed... nor .gitignore... nor .travis.yml, etc. and that it is require to archive these files for this shell script? –  Yanick Rochon Jul 17 '14 at 13:50
    
You used the term hidden files. That indicates the filesystem is hidding. –  Ramhound Jul 17 '14 at 13:55
    
In Unix/Linux, all files starting with a dot are "hidden". It doesn't mean that they should not be copied or archived. –  Yanick Rochon Jul 17 '14 at 13:57

3 Answers 3

./ means the current directory. Your two listings are identical.

Having ./ in front of the file when listing the archive helps to handle files with a special name. A file name -file will be displayed as ./-file allowing to be processed by a second tool.

share|improve this answer
    
The listing I gave was to show the archive structure. I mean, when I open the archive in a GUI tool, I don't want a . folder, but all the files directly at the top level, including hidden files. –  Yanick Rochon Jul 17 '14 at 13:51
    
@YanickRochon They are at the top level. –  Matteo Jul 17 '14 at 14:44

This command will tar files in the current directory without the leading ./:

tar cf /some/tarfile.tar *

Of course, it won't include filenames starting with a period, which probably isn't what you want. You can add additional wildcards to catch those:

tar cf /some/tarfile.tar * .[a-zA-Z]*

But this isn't a general-purpose solution; you have to know what filenames are in the directory so that you can craft appropriate wildcards.

GNU tar--which is what most Linux distros use--has a feature to transform filenames using sed expressions. This will remove the ./ from files while creating the archive:

tar cf /some/tarfile.tar --transform 's,^\./,,' .

The resulting archive will still have an entry in it for the "." directory itself.

Some versions of tar can read the names of the files to be tarred from a file. You can exploit that feature to do this:

ls -a | egrep -v '^\.\.?$' | tar cf - -T /dev/stdin

This pipeline constructs a list of all files in the current directory, removes the . and .. entries, and pipes the rest of the names to tar. tar makes the archive from filenames read from /dev/stdin (its standard input).

share|improve this answer
    
This is a good start. But for some reason, does not archive some files. I'll investigate this further. –  Yanick Rochon Jul 17 '14 at 16:11

Changing your command from

tar -czf $ARCHIVE_NAME -C $DIR .

to

tar -czf $ARCHIVE_NAME -C $DIR ./*

should do the trick. This way, you're not saying "Add the directory . to the archive" but instead "Add all these files in the directory . to the archive".

share|improve this answer
    
After posting this I read in detail Kenster's answer. The above is what I do, but I know it will work with my files. His point regarding not picking up hidden files, etc., is very valid. –  Steve Jul 17 '14 at 21:42
    
Yeah, and I need the hidden files in the archive :) –  Yanick Rochon Jul 18 '14 at 19:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.