ChatGPT解决这个技术问题 Extra ChatGPT

How do I tar a directory without retaining the directory structure?

I'm working on a backup script and want to tar up a file directory:

tar czf ~/backup.tgz /home/username/drupal/sites/default/files

This tars it up, but when I untar the resulting file, it includes the full file structure: the files are in home/username/drupal/sites/default/files.

Is there a way to exclude the parent directories, so that the resulting tar just knows about the last directory (files)?


T
Tom Aranda

Use the --directory option:

 tar czf ~/backup.tgz --directory=/home/username/drupal/sites/default files 

What if I want to put 2 directories here?
@user3352668 simply add two directories. E.g. if default contains "files" and "morefiles", simply add "morefiles" to the string of nbt. Btw: Best solution is nbt's, should be the accepted answer
-C DIR is same as --directory=DIR, changes directory to DIR
Just thought I'd mention that the order here is significant. You can't have the --directory="/home/username/drupal/sites/default files" infront of the destination ~/backup.tgz.
@ChrisStryczynski Note that the path given by the --directory option is not "/home/username/drupal/sites/default files". It is /home/username/drupal/sites/default, followed by a positional argument specifying the name of the directory to be tar'ed, files.
D
Danvil

Hi I've a better solution when enter in the specified directory it's impossible (Makefiles,etc)

tar -cjvf files.tar.bz2 -C directory/contents/to/be/compressed .

Do not forget the dot (.) at the end !!


What does the dot at the end do?
@BruceSun The -C changes your working directory. The dot tells tar which directory you want to archive (i.e the current working directory).
If you omit the dot at the end, you likely will receive the warning: tar: Cowardly refusing to create an empty archive
Everyone talks about the -C option or --directory but without the little '.' at the end as @MaikolD mentioned it, it does not work - at least on my environment.
This still leaves you with a . folder in the root of the tar
J
John Gibb
cd /home/username/drupal/sites/default/files
tar czf ~/backup.tgz *

You can also use the -C option of tar.
cd is not recommended and error prone, e.g. big scripts or makefiles. The answer below is more generally usable and correct.
Tries -C and --directory didn't work for me on Ubuntu 16.04. This is only option that works.
This is the only option that works -C add a . root folder, which is not what we want.
u
upe

Create a tar archive

tar czf  $sourcedir/$backup_dir.tar --directory=$sourcedir WEB-INF en

Un-tar files on a local machine

tar -xvf $deploydir/med365/$backup_dir.tar -C $deploydir/med365/

Upload to a server

scp -r -i $privatekey $sourcedir/$backup_dir.tar $server:$deploydir/med365/
echo "File uploaded.. deployment folders"

Un-tar on server

ssh -i $privatekey $server tar -xvf $deploydir/med365/$backup_dir.tar -C $deploydir/med365/

M
Manos Nikolaidis

To gunzip all txt (*.txt) files from /home/myuser/workspace/zip_from/ to /home/myuser/workspace/zip_to/ without directory structure of source files use following command:

tar -P -cvzf /home/myuser/workspace/zip_to/mydoc.tar.gz  --directory="/home/myuser/workspace/zip_from/" *.txt

D
Druckles

To build on nbt's and MaikoID's solutions:

tar -czf destination.tar.gz -C source/directory $(ls source/directory)

This solution:

Includes all files and folders in the directory

Does not include any of the directory structure (or .) in the final product

Does not require you to change directories.

However, it requires the directory to be given twice, so it may be most useful in another script. It may also be less efficient if there are a lot of files/folders in source/directory. Adjust the subcommand as necessary.

So for instance for the following structure:

|- source
|  |- one
|  `- two
`- working

the following command:

working$ tar -czf destination.tar.gz -C ../source $(ls ../source)

will produce destination.tar.gz where both one and two (and sub-files/-folders) are the first items.


V
Vishrant

This worked for me:

gzip -dc "<your_file>.tgz" | tar x -C <location>

A
Aashutosh Taikar

For me -C or --directory did not work, I use this

cd source/directory/or/file
tar -cvzf destination/packaged-app.tgz *.jar
# this will put your current directory to what it previously was
cd -

a
atline

Kindly use the below command to generate tar file without directory structure

tar -C <directoryPath> -cvzf <Path of the tar.gz file> filename1 filename2... filename N

eg:

tar -C /home/project/files -cvzf /home/project/files/test.tar.gz text1.txt text2.txt

M
Manos Nikolaidis
tar -Cczf ~/backup.tgz /home/username/drupal/sites/default/files

-C does the cd for you


This will not work. See MaikoID's and Neil Butterworth's solutions.
-C works, but has to be used like -C <dir> file1 file2 ...