How does Linux TAR Exclusion works? - linux

Let's say I have a directory structure like this:
# tree original_directory/
|-- sub-1
|-- sub-2
|-- ignore_this_dir
|-- sub-3
Then the tar command to exclude the directory called ignore_this_dir is actually:
# tar -cf new_archived.tar original_directory/ --exclude=ignore_this_dir
OR
# tar -cf new_archived.tar original_directory/ --exclude=original_directory/ignore_this_dir

The man page states:
--exclude=PATTERN
exclude files, given as a PATTERN
Meaning
tar -cf new_archived.tar origin_directory/ --exclude=ignore_this_dir
will be ok in your situation as the pattern ignore_this_dir will match original_directory/ignore_this_dir.

Related

Rsync: Exclude directory contents, but include directory

I know that a directory can be excluded with --exclude like this:
rsync -avz --exclude=dir/to/skip /my/source/path /my/backup/path
This will omit the directory dir/to/skip
However I want to copy the directory itself but not the contents | Is there a one-liner with rsync to accomplish this?
Essentially, include dir/to/skip but exclude dir/to/skip/*
NOTE: I did search for this question. I found a lot of similar posts but not exactly this. Apologies if there is a dupe already.
The --exclude option takes a PATTERN, which means you just should just be able to do this:
rsync -avz --exclude='dir/to/skip/*' /my/source/path /my/backup/path
Note that the PATTERN is quoted to prevent the shell from doing glob expansion on it.
Since dir/to/skip doesn't match the pattern dir/to/skip/*, it will be included.
Here's an example to show that it works:
> mkdir -p a/{1,2,3}
> find a -type d -exec touch {}/file \;
> tree --charset ascii a
a
|-- 1
| `-- file
|-- 2
| `-- file
|-- 3
| `-- file
`-- file
3 directories, 4 files
> rsync -r --exclude='/2/*' a/ b/
> tree --charset ascii b
b
|-- 1
| `-- file
|-- 2
|-- 3
| `-- file
`-- file
3 directories, 3 files
It is important to note that the leading / in the above PATTERN represents the root of the source directory, not the filesystem root. This is explained in the rsync man page. If you omit the leading slash, rsync will attempt to match the PATTERN from the end of each path. This could lead to excluding files unexpectedly. For example, suppose I have a directory a/3/2/ which contains a bunch of files that I do want to transfer. If I omit the leading / and do:
rsync -r --exclude='2/*' a/ b/
then the PATTERN will match both a/2/* and a/3/2/*, which is not what I wanted.
Try:
rsync -avz --include=src/dir/to/skip --exclude=src/dir/to/skip/* src_dir dest_dir
--include=src/dir/to/skip includes the directory. --exclude=src/dir/to/skip/* excludes everything under the directory.

Tar several files keeping directory tree

Ive been looking at this for hours, and cant find a way to tar several files with directory structure ( tree )
Example:
I have files in:
1. /home/user/somefolder/file.txt
2. /home/user/somefolder/file2.txt
3. /home/user/somefolder/file3.txt
4. /home/user/somefolder/somefolder2/file2.txt
5. /home/user/somefolder/somefolder3/file3.txt
now i want to take files 1+4 and tar it.
tar cvzf file.tar.gz /home/user/somefolder/file.txt /home/user/somefolder/somefolder/file2.txt
but it takes those files and tar it into one file without keeping the second some folder while i want to tar file to look like this:
tarfile
somefolder
|-- file.txt
|-- somefolder2
|-- file2.txt
Try this tar:
tar -cvz -f file.tar.gz -C /home/user/somefolder/ file.txt file2.txt file3.txt \
-C /home/user/somefolder/somefolder2/ file2.txt file3.txt
Well, silly me
When using full path from a remote directory tar saves the entire tree of the folders.
so there isnt really any issue :) sorry

How to recursively copy files under a folder and its subfolders as symbolic links

In linux or freebsd, Is there a way to copy all files under a folder and its subfolders as symbolic link ? I need to copy thousands of files into different locations as symbolic links and only have 2-3 configuration files as the actual file. The reason I'm doing this is, I have dozen of websites with with exactly the same engine code, but different configuration and look. I want to copy the engine as symbolic link so every change I make to original files will be applied to other websites as well.
I can't make symbolic link to the engine folder itself, because the configuration file is under that folder, and I can't copy files one by one ! cause obviously it's not practical.
Any suggestion ?
The command you are looking for is cp -rs /path/to/source dest.
Note that you need to provide full path to the source directory so that it can make absolute symlinks.
i don't know if this is what you want: (see example below)
dir one is your central "engine"
dir two is one of your website.
kent#ArchT60:/tmp$ tree one two
one
|-- 1.txt
|-- 2.txt
|-- 3.txt
|-- 4.txt
|-- 5.txt
|-- dirA
| |-- a
| |-- b
| `-- c
|-- dirB
`-- dirC
two
|-- myConf_a.conf
|-- myConf_b.conf
|-- myConf_c.conf
|-- myConf_d.conf
`-- myConf_e.conf
kent#ArchT60:/tmp$ ln -s /tmp/one/* /tmp/two/.
kent$ tree -l /tmp/two
/tmp/two
|-- 1.txt -> /tmp/one/1.txt
|-- 2.txt -> /tmp/one/2.txt
|-- 3.txt -> /tmp/one/3.txt
|-- 4.txt -> /tmp/one/4.txt
|-- 5.txt -> /tmp/one/5.txt
|-- dirA -> /tmp/one/dirA
| |-- a
| |-- b
| `-- c
|-- dirB -> /tmp/one/dirB
|-- dirC -> /tmp/one/dirC
|-- myConf_a.conf
|-- myConf_b.conf
|-- myConf_c.conf
|-- myConf_d.conf
`-- myConf_e.conf

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)?
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
If you want to tar files while keeping the structure but ignore it partially or completely when extracting, use the --strip-components argument when extracting.
In this case, where the full path is /home/username/drupal/sites/default/files, the following command would extract the tar.gz content without the full parent directory structure, keeping only the last directory of the path (e.g. files/file1).
tar -xzv --strip-components=5 -f backup.tgz
I've found this tip on https://www.baeldung.com/linux/tar-archive-without-directory-structure#5-using-the---strip-components-option.
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.
This worked for me:
gzip -dc "<your_file>.tgz" | tar x -C <location>
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 -
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
tar -Cczf ~/backup.tgz /home/username/drupal/sites/default/files
-C does the cd for you

Make Tar + gzip ignore directory paths

Is it possible, when making a tar + gzip through the 'tar c ...' command, to have the relative paths will be ignored upon expanding?
For example,
tar cvf test.tgz foo ../../files/bar
And then expanding the test.tgz with
tar xvf test.tgz
gives a directory containing:
foo files/bar
I want the directory to contain the files:
foo bar
Is this possible?
If all the paths begin with the same initial list of directories then you can use e.g. tar cvf test.tgz -C ../.. other/dir. Beware that the shell won't expand wildcards in pathnames "properly" however because -C asks tar to change directory.
Otherwise, the only way I've ever come up with is to make a temporary directory filled with appropriate symlinks and use the -h option to dereference through symlinks. Of course that won't work if some of the files you want to store are actually symlinks themselves.

Resources