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

tar file without directory structure
tar extract without parent directory
tar --exclude multiple directories
tar not include root directory
man tar
tar options
tar file without root directory
tar base directory

I typically do:

tar -czvf my_directory.tar.gz my_directory

What if I just want to include everything (including any hidden system files) in my_directory, but not the directory itself? I don't want:

   --- my_file
   --- my_file
   --- my_file

I want:

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 ..
$ tar ztf my_dir.tgz

creating a tar archive without including parent directory, Try the following: find . -type f -printf "%h\n%f\n" | xargs -L 2 tar -rf /tmp/files.tar -C. or some variation depending on your needs. One should consider security� The resulting file will have a .tar extension. .tar files are often referred to as tarballs. The tar command will only archive the files. It will not perform any compression, so the archive will be the same size as the original files. You can compress the .tar file using gzip or bzip2, resulting in a .tar.gz or .tar.bz2 extension.

Use the -C switch of tar:

tar -czvf my_directory.tar.gz -C my_directory .

The -C my_directory tells tar to change the current directory to my_directory, and then . means "add the entire current directory" (including hidden files and sub-directories).

Make sure you do -C my_directory before you do . or else you'll get the files in the current directory.

Make tar archive only files without directories (equivalent of zip -D -j , Let's say you have a directory named "top-dir" with subfolders but not the directory itself, the command line that actually works is tar -zcp� If you wish to extract files in particular directory, for example in /tmp then you need to use the following command: $ tar -zxvf prog-1-jan-2005.tar.gz -C /tmp $ cd /tmp $ ls - Compress an entire directory to a single file. To compress directory named /home/vivek/bin/ in to a /tmp/bin-backup.tar.gz type the tar command on Linux:

You can also create archive as usual and extract it with:

tar --strip-components 1 -xvf my_directory.tar.gz

How do I use tar to exclude all files of a certain directory?, How can I recursively find a directory by name and delete its contents (including all sub-directories and files) while keeping the directory itself? 1. Example 1: Extracting tar Files to a Specific Directory. In the first example, I will extract the files in articles.tar to a directory /tmp/my_article. Always make sure that the directory into which you want to extract tar file exists. Let me start by creating the /tmp/my_article directory using the command below: # mkdir /tmp/my_article

Have a look at --transform/--xform, it gives you the opportunity to massage the file name as the file is added to the archive:

% mkdir my_directory
% touch my_directory/file1
% touch my_directory/file2
% touch my_directory/.hiddenfile1
% touch my_directory/.hiddenfile2
% tar -v -c -f my_dir.tgz --xform='s,my_directory/,,' $(find my_directory -type f)
% tar -t -f my_dir.tgz 

Transform expression is similar to that of sed, and we can use separators other than / (, in the above example).

command line, A function letter need not be prefixed with ``-'', and may be combined with other TAG --exclude-tag=FILE exclude contents of directories containing FILE, except files with the same ownership --no-same-owner extract files as yourself� I want to make a tar file with everything inside the directory. This includes files, folders, and hidden "." files and folders. Ive tried two commands (lets say current directory Im in is /hello) tar -zcvf something.tar.gz . This does it but inside the archive, it adds a "." to the front of all files/folders Next I tried: tar -zcvf something

find /my/dir/ -printf "%P\n" | tar -czf mydir.tgz --no-recursion -C /my/dir/ -T -

With some conditions (archive only files, dirs and symlinks):

find /my/dir/ -printf "%P\n" -type f -o -type l -o -type d | tar -czf mydir.tgz --no-recursion -C /my/dir/ -T -

The below unfortunately includes a parent directory ./ in the archive:

tar -czf mydir.tgz -C /my/dir .

You can move all the files out of that directory by using the --transform configuration option, but that doesn't get rid of the . directory itself. It becomes increasingly difficult to tame the command.

You could use $(find ...) to add a file list to the command (like in magnus' answer), but that potentially causes a "file list too long" error. The best way is to combine it with tar's -T option, like this:

find /my/dir/ -printf "%P\n" -type f -o -type l -o -type d | tar -czf mydir.tgz --no-recursion -C /my/dir/ -T -

Basically what it does is list all files (-type f), links (-type l) and subdirectories (-type d) under your directory, make all filenames relative using -printf "%P\n", and then pass that to the tar command (it takes filenames from STDIN using -T -). The -C option is needed so tar knows where the files with relative names are located. The --no-recursion flag is so that tar doesn't recurse into folders it is told to archive (causing duplicate files).

If you need to do something special with filenames (filtering, following symlinks etc), the find command is pretty powerful, and you can test it by just removing the tar part of the above command:

$ find /my/dir/ -printf "%P\n" -type f -o -type l -o -type d
> textfile.txt
> documentation.pdf
> subfolder2
> subfolder
> subfolder/.gitignore

For example if you want to filter PDF files, add ! -name '*.pdf'

$ find /my/dir/ -printf "%P\n" -type f ! -name '*.pdf' -o -type l -o -type d
> textfile.txt
> subfolder2
> subfolder
> subfolder/.gitignore
Non-GNU find

The command uses printf (available in GNU find) which tells find to print its results with relative paths. However, if you don't have GNU find, this works to make the paths relative (removes parents with sed):

find /my/dir/ -type f -o -type l -o -type d | sed s,^/my/dir/,, | tar -czf mydir.tgz --no-recursion -C /my/dir/ -T -

tar man page, A function letter does not need to be prefixed with a dash ("-"), and may be the contents of the directory, but archives the directory itself and the CACHEDIR. This option recognizes the files and directories used by many� To remove a directory that contains other files or directories, use the following command. rm -r mydir. In the example above, you would replace "mydir" with the name of the directory you want to delete. Executing the command would recursively delete all files and subdirectories in that directory.

Linux tar command help and examples, As of this writing (November 2005) this works only with Linux, and only with Linux But other times tar might think that the option works when it actually does not. a valid cache directory tag file, but still dump the directory node and the tag file itself. Exclude from dump directories and files, that are internal for some widely � cd /tmp mkdir folder touch folder/file.txt when you do tar -zcvf folder.tar.gz folder everything is as expected = when you untar it now it will be untarred (folder will be create, if you removed it) as /tmp/folder/.

GNU tar 1.32: 3.4.2 tar Options, In computing, tar is a computer software utility for collecting many files into one archive file, The original tar implementation did not care about the contents of the padding bytes The final block of an archive is padded out to full length with zeros. tar [-options] <name of the tar archive> [files or directories which to add into� The tar --exclude=PATTERN command matches the given pattern and excludes those files, but I need specific files & folders to be ignored (full file path), otherwise valid files might be excluded. I could also use the find command to create a list of files and exclude the ones I don't want to archive and pass the list to tar, but that only works

tar (computing), Here we explain how you can create incremental backups with tar. You'll need to modify the following example script yourself to suit your needs and directory structure: It's conceivable that you can also exclude directories or files with If you're not in the root directory when extracting the archive, tar�

  • Is that the default behavior of doing tar -czf? In my case it's only storing the files and not the directory. When I just tar the directory it includes it but with tar -czf it is only adding the files.
  • This will also work on files with spaces or other special characters. Good job!
  • Not perfect - tar file contains '.' and also ./file1 instead of just file1. I like the solution by mateusza below to use --strip-components when un-tarring.
  • @Ivan if you replace . with * so the command will be cd my_directory/ && tar -zcvf ../my_dir.tgz * && cd .. then it will work as you expected.
  • @jmathew You can also use a subshell so your current shell's working directory doesn't change: $ (cd my_directory/ && tar -zcvf ../my_dir.tgz .)
  • I know it's an old answer but cding into directories and out is pretty lame. Could at least use pushd and popd if tar didn't have any flags like -C.
  • +1 thank you! It was the damn '.' I was missing. so aggravating
  • "Unlike most options, -C is processed at the point it occurs within the list of files to be processed. Consider the following command: tar --create --file=foo.tar -C /etc passwd hosts -C /lib libc.a" I always try tar -czvf my_directory.tar.gz * -C my_directory and that does not work. -C location is important! Damn tar...
  • Not perfect - tar file contains '.' and also ./file1 instead of just file1. I like the solution by mateusza below to use --strip-components when un-tarring.
  • @Superole: the shell substitues the wildcards before running tar. Also note that using a wildcard like * will not include hidden files (which was the original requirement).