How to fix "tar: Removing leading `/' from member names" warning? - linux

I'm trying to create tar.gz file but I keep getting the error
tar: Removing leading `/' from member names
by doing:
sudo tar -zcf test.tar.gz /opt/stagecoach/apps/test/current/
Ive googled it and found the -P option but I still get the same error.
Any ideas?

Try this:
sudo tar -cvzf test.tar.gz /opt/stagecoach/apps/test/current

Try this:
sudo tar -czf test.tar.gz -P /opt/stagecoach/apps/test/current

The path of the archive when specified causes this informational message, you can do the following to resolve it:
tar -zvcf /tmp/mike.tar.gz -P /tmp/mike
or
tar -zvcf mike.tar.gz -P /tmp/mike
or
tar -zvcf mike.tar.gz /tmp/mike
Note: it is not an error, just informational.

Changing directory using the -C option solved the confusing message about removing leading `/' from member names:
tar cf - -C /working-directory .

Related

tar: Removing leading `/' from member names "it is not duplicate"

#!/bin/bash
source="/home/user/work/tar/deneme"
source2="/home/user/work/tar/deneme1"
for i in {1..5}
do
tar -czvf $source2/$i/$i.tar.gz $source/$i/
done
I get this error message.
tar: Removing leading/' from member names`
this is my script and error. there are a lot of questions here but my problem doesn't solve. I run script than script create .tar.gz file. But if I unzip with tar -xzvf 1.tar.gzthis command, my file created in full path like home/user/work/tar/deneme/1/1-1.txt.
Do you have any idea?
I try some of ways.
For examle
Find /SED to convert absolute path to relative path within a single line tar statement for crontab
https://unix.stackexchange.com/questions/59243/tar-removing-leading-from-member-names/59244
This is because GNU tar remove leading / (by default). To avoid it you can rewrite your script on this way:
#!/bin/bash
cd /home/user/work/tar
source="deneme"
source2="deneme1"
for i in {1..5}
do
mkdir -p ${source2}/${i}
tar -czvf ${source2}/${i}/${i}.tar.gz ${source}/${i}/
done
Thank you for all your comments and answer.
I find the solution. I change some of codes. which is inside for loop
mkdir $source2/$i
cd $source/
tar -czvf $source2/$i/$i.tar.gz $i/*

How do I backup a directory using bash script

I am having difficulty trying to backup my 'my_work' directory into my destination 'Backup' directory. I tried running the script, but it does not seem to work. This is the script that I have written :
#!/bin/bash
SRCDIR="/home/student/Documents/my_work/"
DESTDIR="/home/student/Backups/"
FILENAME=backup1-$(date +%-Y%-m%-d)-$(date +%-T).tgz
tar --create --gzip --file=$DESTDIR $FILENAME $SRCDIR
This is the output I received :
tar: backup1-201576-10\:24\:17.tgz: Cannot stat: No such file or directory
tar: Removing leading '/' from member names
tar (child): /home/student/Backups/: Cannot open: Is a directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
I can't seem to find a solution to this, please help
Just make a subtle change in your script:
Change
tar --create --gzip --file=$DESTDIR $FILENAME $SRCDIR
to
tar --create --gzip --file=$DESTDIR$FILENAME $SRCDIR
Notice there is no space between $DESTDIR and $FILENAME. To suppress tar: Removing leading '/' from member names, you may use the -P flag cautiously.
Also, it'd be nice to replace colons in the filename with underscores or dashes. Colon is a reserved character that is also used in PATH and may cause confusion.

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

How do I exclude absolute paths for tar?

I am running a PHP script that gets me the absolute paths of files I want to tar up. This is the syntax I have:
tar -cf tarname.tar -C /www/path/path/file1.txt /www/path/path2/path3/file2.xls
When I untar it, it creates the absolute path to the files. How do I get just /path with everything under it to show?
If you want to remove the first n leading components of the file name, you need strip-components. So in your case, on extraction, do
tar xvf tarname.tar --strip-components=2
The man page has a list of tar's many options, including this one. Some earlier versions of tar use --strip-path for this operation instead.
You are incorrectly using the -C switch, which is used for changing directories. So what you need to do is:
tar -cf tarname.tar -C /www/path path/file1.txt path2/path3/file2.xls
or if you want to package everything under /www/path do:
tar -cf tarname.tar -C /www/path .
You can use -C switch multiple times.
For me the following works the best:
tar xvf some.tar --transform 's?.*/??g'
--transform argument is a replacement regex for sed, to which every extracted filepath is fed. Unlike --strip-components, it will remove all path information, not just fixed number of components.
If you don't know how many components are in the path, you could try this:
DIR_TO_PACK=/www/path/
cd $DIR_TO_PACK/..
tar -cf tarname.tar $(basename $DIR_TO_PACK)

go to path and then tar?

Can you have tar travel to a certain direct and then tar files relative to that directory? All while using one command (tar)?
For example instead of doing
cd /home/test/backups; tar zvPcf backup.tar.gz ../data/
I could do something like
tar -g '/home/test/backups/' zvPcf backup.tar.gz ../data/
see the -C option.
the tar man page gives this example :
tar -xjf foo.tar.bz2 -C bar/
extract bzipped foo.tar.bz2 after changing directory to bar
might be what you're looking for ...
Have you tried this:
tar zvPcf /home/test/backups/backup.tar.gz /home/test/backups/../data/
You could try:
tar zvPcf backup.tar.gz ../data/ -C '/home/test/backups/'
See tar(1) man page.
-C, --directory DIR
change to directory DIR

Resources