How to Extract Files Without Folder Structure Using Tar

How do I extract files without folder structure using tar

You can use the --strip-components option of tar.

 --strip-components count
(x mode only) Remove the specified number of leading path ele-
ments. Pathnames with fewer elements will be silently skipped.
Note that the pathname is edited after checking inclusion/exclu-
sion patterns but before security checks.

I create a tar file with a similar structure to yours:

$tar -tf tarfolder.tar
tarfolder/
tarfolder/file.a
tarfolder/file.b

$ls -la file.*
ls: file.*: No such file or directory

Then extracted by doing:

$tar -xf tarfolder.tar --strip-components 1
$ls -la file.*
-rw-r--r-- 1 ericgorr wheel 0 Jan 12 12:33 file.a
-rw-r--r-- 1 ericgorr wheel 0 Jan 12 12:33 file.b

tar without retaining the directory structure

Use tar -C to change directory before creating the archive:

tar cz -C /path/to/dir . -f /path/to/archive.tar.gz
# archive.tar.gz gets contents of /path/to/dir as root entry:
# ./
# ./data.txt

If I got your PHP-code correctly, it would be something like:

$tar_dir = "/home/minecraft/multicraft/servers/$entry";
$archive = __DIR__ . "/tmp/$entry.tar.gz";
shell_exec("tar cz -C $tar_dir . -f $archive");

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

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

Create tar-file without folder strucure

tar -cf a/b/c/tarfile.tar -C a/b/c . will switch to the directory a/b/c and read in the entire directory (the . - you could specify specific files as well, but a wildcard will not do what you're expecting). The -C <directory> <filelist> pattern can be repeated as necessary to process additional files from different locations.

Another possibility given your original example would be cd a/b/c; tar cf ../../../tarfile.tar *, but that doesn't give you the possibility to pull multiple files from different locations (of course you could still use -C, but the relative paths would have to be adjusted accordingly.

How do I tar a directory of files and folders without including the directory itself?

cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd - 

should do the job in one line. It works well for hidden files as well. "*" doesn't expand hidden files by path name expansion at least in bash. Below is my experiment:

$ mkdir my_directory
$ touch my_directory/file1
$ touch my_directory/file2
$ touch my_directory/.hiddenfile1
$ touch my_directory/.hiddenfile2
$ cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
$ tar ztf my_dir.tgz
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2

How do I tar the certain files of a directory without retaining the directory structure?

cd [dir]; tar cz ./* >x.tgz

or

cd [dir]; tar cz file1 file2 >x.tgz

How do I extract only the desired files from tar.gz?

I tested it with the following folder structure:

data/
data/a
data/a/ANOTHER_SNAPSHOT.jar
data/b
data/c
data/c/SNAPSHOT.jar
data/d
data/e
data/f
data/f/SNAPSHOT.jar.with.extension
data/g
data/g/SNAPSHOT.jar
data/h

The following wildcard mask works and extract only the files matching exactly SNAPSHOT.jar not SNAPSHOT.jar.with.extension and ANOTHER_SNAPSHOT.jar

tar -xf data.tar.gz --wildcards "*/SNAPSHOT.jar"

Result:

data/c/SNAPSHOT.jar
data/g/SNAPSHOT.jar


Related Topics



Leave a reply



Submit