How to add files from a folder to archive instead of the folders itself

tar

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

Best Answer

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.

Related Question