How do I tar a directory without retaining the directory structure?
cd /home/username/drupal/sites/default/files
tar czf ~/backup.tgz *
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 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
Tar a directory, but don't store full absolute paths in the archive
tar -cjf site1.tar.bz2 -C /var/www/site1 .
In the above example, tar will change to directory /var/www/site1
before doing its thing because the option -C /var/www/site1
was given.
From man tar
:
OTHER OPTIONS
-C, --directory DIR
change to directory DIR
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 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
Shell command to tar directory excluding certain files/folders
You can have multiple exclude options for tar so
$ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .
etc will work. Make sure to put --exclude
before the source and destination items.
Related Topics
Python Code to Check If Service Is Running or Not.
How to Grep For Contents After Pattern
How to Change the Environment Variables of Another Process in Unix
"Failed to Load Platform Plugin "Xcb" " While Launching Qt5 App on Linux Without Qt Installed
How to Use Local Docker Images With Minikube
How to Copy Commits from One Git Repo to Another
How to Rename All Folders and Files to Lowercase on Linux
How to Open a Socket and Pass It to Another Process in Linux
How to Use 'Cp' Command to Exclude a Specific Directory
How to Create a Crontab Through a Script
How to Concatenate Multiple Lines of Output to One Line
Can You Run Gui Applications in a Linux Docker Container
How to Search For a Multiline Pattern in a File
Fastest Way to Find Lines of a File from Another Larger File in Bash
How to Know the Script File Name in a Bash Script
How to Build & Install Glfw 3 and Use It in a Linux Project