Linux zip a directory excluding some internal directories - linux

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

Related

Tar command keeps bundling up entire directory path

I have a few sub-directories with files inside each of them in /home/user/archived/myFiles that I'm trying to bundle into a single tar file. The issue is, it keeps bundling a full directory path instead of just everything in the myFiles folder.
When I untar the file, I just want all the bundled sub-directories/files inside to appear in the directory I extracted the file rather than having to go through a series of folders that get created.
Instead, when I currently untar the file, I get a "home" folder and I have to go through /home/user/archived/myFiles to reach all the files.
I tried using the -C flag that I saw suggested online here Tar a directory, but don't store full absolute paths in the archive where you insert parameters for the full directory minus the last folder, and then the name of the last folder which contains all the stuff you want bundled. But the tar command doesn't work as I get a no such file or directory error.
#!/bin/bash
archivedDir="/home/user/archived/myFiles"
tar -czvf "archived-files.tar.gz" "${archivedDir}"/*
rm -vrf "${archivedDir}"/*
# Attempt with -C flag
#tar -cvf "${archivedDir}/archived-files.tar.gz" -C "${archivedDir}" "/*"
So for example, if I did an ls on /home/user/archived/myFiles, and it listed two directories called folderOne and folderTwo, and I ran this bash script and did an ls on /home/user/archived/myFiles again, that directory should only contain archived-files.tar.gz.
If I extracted the tar file, then folderOne and folderTwo would appear.
As I explain already here you should first change to this directory and then create the archive.
So change you script to something like:
archivedDir="/home/user/archived/myFiles"
cd $archivedDir
tar -czvf "../archived-files.tar.gz" *
This will create the archive in upper directory so you will not remove it with the next command.
the extraction should be something like:
archivedDir="/home/user/archived/myFiles"
cd $archivedDir
tar -xzvf "../archived-files.tar.gz"

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

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.

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