4

this is not a duplicate of creating a tar archive without including parent directory

If I do tar -czf archive directory, directory will be added to the archive. I want to add the files of the folder directory not the directory itself, how?
please suggest answer that does not involve cding to directory

this is not a duplicate of what its showing:
I have a directory called somedir with contents as abc, xyz:

ls somedir
abc xyz

I want to make archive that will contain files abc, xyz, not somedir folder

Update: if i use the command tar -C /home/username/dir1/dir2 -cvf temp.tar yourarchive (which the answer to the question of which my question is called duplicate of)
i get this:

enter image description here

what I wanted is tar czf archive.tar.gz -C yourarchive ., I get this (which is close enough of what I wanted):

enter image description here

what I wanted was is this (directly files, no folder):

enter image description here

  • This seems like an exact duplicate as far as I can tell. tar czf archive -C directory . will create an archive file containing abc and xyz. I am therefore closing this. If I'm missing something, please let me know. – terdon Apr 28 '15 at 13:30
  • @terdon you are wrong, by using tar czf archive -C directory . i am getting archive that contains a folder . which has files in it. on the other hand if I open the archive i want to make, i must see directly files and no folder – Edward Torvalds Apr 28 '15 at 13:56
  • But that makes no difference at all! If you untar it, you just get the files, no directory. The . is implied, anyway. Can you give an example where there is a difference? – terdon Apr 28 '15 at 14:03
  • @terdon I hope I am clear now, please reply – Edward Torvalds Apr 28 '15 at 14:15
7

Use -C:

tar czf archive -C directory .

This instructs tar to use directory as its working directory, and archive everything contained in the directory (.).

Using -C is nearly equivalent to using cd; archive above is interpreted relative to the current directory when tar starts, then tar changes its working directory to directory before interpreting ..

I'm not sure how widespread support for tar -C is, but any Linux distribution should support it.

2

try

tar cvf archive.tar -C somedir $(ls somedir)

where

  • cvf will Create Verbose(ly) File Archive.tar
  • -C somedir will instruct tar to use somedir
  • $(ls somedir ) list file in somedir and $( ) passe result as argument

Edit:

As pointed in comment, use ls -A (not ls -a) to list dotfile.

  • Or $(ls -A somedir) to also include the dotfiles. – chaos Apr 28 '15 at 9:23
  • 2
    This will break on files containing whitespace. – camh Apr 28 '15 at 9:38
  • @Archemar being root has nothing to do with ls -A; adding -A includes dotfiles except . and .., which is necessary to replicate tar's behaviour with directories (it archives everything, including hidden files). – Stephen Kitt Apr 28 '15 at 11:39
  • @StephenKitt It seems the other way round, ls will not list dotfile for normal user, whereas root will see it. – Archemar Apr 28 '15 at 11:46
  • 3
    Unless you have aliases which provide different options to ls, plain ls ignores dotfiles for all users, including root, ls -a lists all files including dotfiles and . and .., ls -A lists all files including dotfiles except . and ... – Stephen Kitt Apr 28 '15 at 11:50
1

You can do this with pax(1), which is the posix successor to tar, but it never really caught on. It can write tar files and it can do the filename transformation you want to do:

pax -wzf archive -s '|directory/\?||' directory

That breaks down to:

  • -w write an archive (same as -c for tar)
  • -z compress archive (same as tar)
  • -f archive write to file archive (same as tar)
  • -s '|directory/\?||' substitute filenames in the archive. Replace the string directory with an optional / with the empty string. The optional / is needed as the directory itself does not end with a /, but all the other members have that /. This does not need to be anchored to the start of the string (^) because it replaces only the first match (like sed(1)) and all files will start with directory/\?

To be explicit you could add:

  • -x ustar write a POSIX tar file

but that is the default.

You should be able to find pax in your Linux or BSD package repository. There seems to be a GNU pax, but I don't know the state of that. Debian seems to have a BSD version of pax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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