How to create tar archive without some folders? - linux

How I can create tar archive without some folders?
Now I'm creating tar archive of folder and deleting some folders from it. But it is takes long time.
Structure:
www
- sub f 1
- sub f 2
- sub f 3
need create archive only with folders (sub f 1) and (sub f 2)
(It is only for example, my really structure have more sub levels)
Thanks!

Does this works ?
tar -cf backup.tar --exclude "www/subf3" www
// is your directory with spaces?

In addition of the --exclude option suggested by Ajreal, you could use some other tar option (from the --help output):
--exclude=PATTERN exclude files, given as a PATTERN
--exclude-backups exclude backup and lock files
--exclude-caches exclude contents of directories containing
CACHEDIR.TAG, except for the tag file itself
--exclude-caches-all exclude directories containing CACHEDIR.TAG
--exclude-caches-under exclude everything under directories containing
CACHEDIR.TAG
--exclude-tag=FILE exclude contents of directories containing FILE,
except for FILE itself
--exclude-tag-all=FILE exclude directories containing FILE
--exclude-tag-under=FILE exclude everything under directories
containing FILE
--exclude-vcs exclude version control system directories
-X, --exclude-from=FILE exclude patterns listed in FILE
and you could also use tardy, a tar post-processor (which is packaged in Debian)

Related

How to create a tar archive from an absolute path and package files as if they were relative?

On Linux, I am trying to create a .tar.gz archive from a different directory, that is I have a bash script that will be executed from a different directory. The script will package the folder, I will give the absolute directory of the folder say /home/user1/Documents/folder1 however when it packages the tar file, it puts the entire absolute directory in the archive, whereas I only want the relative one from folder1.
For example:
tar czf /home/user1/Documents/folder1.tar.gz /home/user1/Documents/folder1
This will create an archive but where the first folder will be home and then inside that user1 inside that documents and inside that the folder1, no other subfolders from other branches of course.
Also the console gives this error:
tar: Removing leading `/' from member names
I want it to be packaged as if I would execute the command from the same folder, so the only folder in the archive should be folder1, and inside that it's own subfolders.
So the archive inside should look just as if I would have executed this code from the same directory folder1 is in:
tar czf folder1.tar.gz folder1
You can use the -C option to change the directory before performing any operations:
tar czf /home/user1/Documents/folder1.tar.gz -C /home/user1/Documents folder1
Now, the contents of your archive will look like this:
$ tar tf /home/user1/Documents/folder1.tar.gz
folder1/
folder1/file1
The message you get is not an error, by the way. It's tar telling you that it made the paths in the archive relative so as to avoid overwriting files, which could easily happen when unpacking an archive with absolute paths. You can turn off the leading slash removal with -P, but you often don't want that.

How do I copy differing content files from one directory to another?

There exists two directories: a/ and b/.
I'd like to copy all the files(recursively) from a/ into b/.
However, I only want to copy over an a file if its content is different than the already existing b file. If the corresponding b file does not exist, then you would still copy over the a file.
*by "corresponding file", I mean a files with the same name and relative path from their parent directories.
note:
The reason I don't want to overwrite a b file with the same exact contents, is because the b directory is being monitored by another program, and I don't want the file date to change causing the program to do more work than required.
I'm essentially looking for a way to perform a cp -rf a/ b/ while performing a diff check on each file. If the file's are different, perform the copy; otherwise skip the copy.
I see that cp has an update flag:
-u, --update
copy only when the SOURCE file is newer than the destination file or when the
destination file is missing
but this will not work because I'm not concerned about newer files; I'm concerned about different file contents.
Any shell language will do.
I've been attempting to get this to work by injecting my diff check into a find command:
find a/ ??? -exec cp {} b \;
This doesn't seem like an uncommon thing to do between two directories, so I'm hoping there is an elegant command line solution as aposed to me having to write a python script.
You can achieve this using rsync. Files or directories will be updated only if there is any new update in source folder.
$rsync -av --progress sourcefolder destinationfolder

Shell script to delete files and folders that don't exist in another folder

Using a shell script I wish to delete all files and folders from /folder2/ that do not exist in /folder1/. Files only need to be matched by name.
I must add that the content of both folders shouldn't necessarily match after this operation because it's possible that /folder1/ contains files that do not in exist in /folder2/. So after executing the shell script all files and folders found in /folder2/ can also be found in /folder1/ but not vice versa.
The following works for me:
rsync -r --delete --existing --ignore-existing /path/to/folder1/ /path/to/folder2/
rsync will delete all files and folders from folder2 that are not found in folder1 recursively. Also, rsync will skip creating files on the destination. This answer was found here: https://serverfault.com/a/713577

tar exclude not working

What's wrong with this tar command?
$ tar --exclude='/tmp/test/exclude-me' -zcvf test.tar.gz test
test/
test/c.txt
test/exclude-me/
test/exclude-me/b.txt
test/a.txt
As you can see, exclude-me is present when I untar the archive. I also tried --exclude=/tmp/test/exclude-me/*.
The exclude family of parameters apply to the internal relative names of the files in the tarball. The absolute path you specify will never exist within the tarball since it only has relative paths from the provided root.
You have to omit the absolute parts of the path.
In your example you use the v-flag and the included files are listed.
The exclude pattern is matched against the entries of this list, not to the actual file paths. So you have to change your pattern to "test/exclude-me".
For some reason you also have to remove the trailing / for folders.

Copy files excluding some folder in linux

I want to create script that copy my project and make it zip archive. I want to exclude all folder named .svn in all sub directories. Any suggestion?
I'd use rsync's FILTER RULES for this:
Create an .rsync-filter file (in the origin directory) containing, e.g.
-.svn/
Now run rsync like an exalted copy:
rsync -aFF origin/ destination/
You can do this using rsync. Although this is designed to synchronise directories across servers, it can also be used to copy directories on a single machine.
rsync has a --exclude option to exclude files and directories by pattern. See http://www.samba.org/ftp/rsync/rsync.html for help and examples.
Just call the zip utility on your project’s folder and use the -r option for recursive plus the -x option to exclude files / folders by pattern.
zip -r target-filename.zip source-folder -x \*exclude-pattern\*
exclude-pattern in your case would be .svn
See also man zip

Resources