Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

unix

tar

gnu

People also ask

Can we tar a directory?

Tape Archive or tar is a file format for creating files and directories into an archive while preserving filesystem information such as permissions. We can use the tar command to create tar archives, extract the archives, view files and directories stored in the archives, and append files to an existing archive.

How do I archive a directory in tar?

To create a tar archive, use the -c option followed by -f and the name of the archive. You can create archives from the contents of one or more directories or files. By default, directories are archived recursively unless --no-recursion option is specified.


Use the --directory option:

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

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 !!


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

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/

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