tar exclude not working - linux

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.

Related

Exclude flag in zip not excluding directory as expected

I'm trying to zip every file in my current directory to deploy.zip using the command:
zip -r deploy.zip -x "**/node_modules/**"
This command doesn't work and still zips all the node_modules and it's descendant folders and files.
This command is basically running on the GitHub Action panel.
What's wrong in here?
It looks like your exclude glob expression isn't quite right. The expression **/node_modules/** looks for absolute filenames from the search path that has the string /node_modules/ to be present. But running the zip from your current path won't include the leading / for any of the file/directory names under node_modules. I would suggest doing it as '*node_modules*' or './node_modules/*' or 'node_modules/*' for your exclude path definition.
Also note that using ** in your glob pattern doesn't mean the same as using simple wild card expansion as *. The former has a special meaning in zip when --wild-stop-dirs option is enabled. See zip(1) - Linux man page

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.

tar removing leading '/' from member names

I got an error when I append > /dev/null to tar command, anyone know what's going on in second example?
good:
tar -cvf $kname /var/www
bad:
tar -cvf $kname /var/www > /dev/null
error:tar: Removing leading `/' from member names
The "good" version is also displaying the same message you've just missed it.
If you don't like the behaviour, search for "leading", in manual. First hit:
-P, --absolute-names
don't strip leading '/'s from file names
This is because your file ($kname) has leading /.
To fix that, you may specify -C to change the directory, instead of specifying full path of the archive file.
It might be best to leave your files without the '/' in the backup and just ignore the error message. Tar does this as a safety precaution, because if you untar the file, it will automatically place the files back in the original directory. This can be dangerous and most people want to avoid this. Personally, I would be happy with the fact that it removed the '/' and then your restore will be relative and not absolute directory path. Then you can manually move the files into the right place, or a different place. Just posting this so people are aware and don't inadvertently replace their original files.
my version of tar does not extract an archive created with -P (--absolute-names) to the original location. this is only true for untar with argument -P

Linux zip a directory excluding some internal directories

I'm trying to zip the public_html folder to the exclusion of two folders, like this:
tar -czf myzip.tar.gz --exclude=home/mydomain/public_html/folder0 --exclude=home/zeejfl6/folder1 /home/mydomain/public_html/
But I get the error:
tar: Removing leading `/' from member names
I tried a few combinations... What am I doing wrong?
It's not an error, it's a warning.
Archives containing absolute paths to files are a security risk. Imagine an archive containing /etc/passwd.
If you insist upon having the absolute paths in the archive, use the -P option:
-P, --absolute-names
don't strip leading `/'s from file names

Wget - output directory prefix

Currently I try to use:
"wget --user=xxx --password=xxx -r ftp://www.domain.com/htdocs/"
But this saves output files to current directory in this fashion:
curdir/www.domain.com/htdocs/*
I need it to be:
curdir/*
Is there a way to do this, I only see a way to use output prefix, but i think this will just allow me to define directory outside current dir?
You can combine --no-directories if you want all your files inside one directory or --no-host-directories to have subdirectories but no subdirectories per host with your --directory-prefix option.
2.6 Directory Options
‘-nd’
‘--no-directories’
Do not create a hierarchy of directories when retrieving recursively. With this option turned on, all files will get saved to the current directory, without clobbering (if a name shows up more than once, the filenames will get extensions ‘.n’).
‘-nH’
‘--no-host-directories’
Disable generation of host-prefixed directories. By default, invoking Wget with ‘-r http://fly.srk.fer.hr/’ will create a structure of directories beginning with fly.srk.fer.hr/. This option disables such behavior.
‘-P prefix’
‘--directory-prefix=prefix’
Set directory prefix to prefix. The directory prefix is the directory where all other files and subdirectories will be saved to, i.e. the top of the retrieval tree. The default is ‘.’ (the current directory).
(From the wget manual.)

Resources