1

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

4
  • 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, 2014 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? Jul 17, 2014 at 13:50
  • You used the term hidden files. That indicates the filesystem is hidding.
    – Ramhound
    Jul 17, 2014 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. Jul 17, 2014 at 13:57

3 Answers 3

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.

2
  • 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. Jul 17, 2014 at 13:51
  • 1
    @YanickRochon They are at the top level.
    – Matteo
    Jul 17, 2014 at 14:44
1

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".

2
  • 1
    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, 2014 at 21:42
  • Yeah, and I need the hidden files in the archive :) Jul 18, 2014 at 19:22
0

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).

1
  • This is a good start. But for some reason, does not archive some files. I'll investigate this further. Jul 17, 2014 at 16:11

You must log in to answer this question.

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