tar: Removing leading `/' from member names
Clash Royale CLAN TAG#URR8PPP
root@server # tar fcz bkup.tar.gz /home/foo/
tar: Removing leading `/' from member names
How can I solve this problem and keep the /
on file names ?
gzip tar
migrated from stackoverflow.com Dec 23 '12 at 15:32
This question came from our site for professional and enthusiast programmers.
add a comment |
root@server # tar fcz bkup.tar.gz /home/foo/
tar: Removing leading `/' from member names
How can I solve this problem and keep the /
on file names ?
gzip tar
migrated from stackoverflow.com Dec 23 '12 at 15:32
This question came from our site for professional and enthusiast programmers.
2
What exactly is not working as expected?
– Joachim Isaksson
Dec 23 '12 at 12:48
28
It is not a problem. You do not want leading slashes in a tar archive. Seriously. If you want to extract an archive to your system root, specify-C /
when extracting it.
– ThiefMaster
Dec 23 '12 at 15:31
add a comment |
root@server # tar fcz bkup.tar.gz /home/foo/
tar: Removing leading `/' from member names
How can I solve this problem and keep the /
on file names ?
gzip tar
root@server # tar fcz bkup.tar.gz /home/foo/
tar: Removing leading `/' from member names
How can I solve this problem and keep the /
on file names ?
gzip tar
gzip tar
edited Dec 23 '12 at 16:44
Renan
14.5k65578
14.5k65578
asked Dec 23 '12 at 12:47
superusersuperuser
676256
676256
migrated from stackoverflow.com Dec 23 '12 at 15:32
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Dec 23 '12 at 15:32
This question came from our site for professional and enthusiast programmers.
2
What exactly is not working as expected?
– Joachim Isaksson
Dec 23 '12 at 12:48
28
It is not a problem. You do not want leading slashes in a tar archive. Seriously. If you want to extract an archive to your system root, specify-C /
when extracting it.
– ThiefMaster
Dec 23 '12 at 15:31
add a comment |
2
What exactly is not working as expected?
– Joachim Isaksson
Dec 23 '12 at 12:48
28
It is not a problem. You do not want leading slashes in a tar archive. Seriously. If you want to extract an archive to your system root, specify-C /
when extracting it.
– ThiefMaster
Dec 23 '12 at 15:31
2
2
What exactly is not working as expected?
– Joachim Isaksson
Dec 23 '12 at 12:48
What exactly is not working as expected?
– Joachim Isaksson
Dec 23 '12 at 12:48
28
28
It is not a problem. You do not want leading slashes in a tar archive. Seriously. If you want to extract an archive to your system root, specify
-C /
when extracting it.– ThiefMaster
Dec 23 '12 at 15:31
It is not a problem. You do not want leading slashes in a tar archive. Seriously. If you want to extract an archive to your system root, specify
-C /
when extracting it.– ThiefMaster
Dec 23 '12 at 15:31
add a comment |
7 Answers
7
active
oldest
votes
Use the --absolute-names
or -P
option to disable this feature.
tar fczP bkup.tar.gz /home/foo/
tar fcz bkup.tar.gz --absolute-names /home/foo
45
This is the correct answer, but be aware, that in most cases, this is not what you want, cause it results in an archive that extracts in complete paths!
– rubo77
Nov 21 '13 at 10:04
7
Using the-C /
option as described in @Marcus' answer will git rid of the STDERR message if that is your primary goal.
– Matt Sanders
Dec 28 '13 at 22:29
1
As @rubo77 commented, usually this is not the expected result of the command.
– alfredocambera
Aug 25 '16 at 15:16
3
Most of the time this is not what a user wants, simply because most of them don't read the manual until they need to (ain't nobody got time for that). So, it would be wise to, at least, expand your answer, warning people not to do this unless they are absolutely sure they understand what's the effect of it. Especially being accepted as an answer.
– Mladen B.
Dec 2 '17 at 7:16
1
@Toskan Examples added
– Barmar
Sep 5 '18 at 17:20
|
show 2 more comments
That's actually a feature, not a problem. Archives with absolute locations are a security risk. Attackers could use such archives to trick users into installing files in critical system locations.
Yes, you could use -P
. But what's wrong with allowing tar to remove the forward slash, and simply requiring the user of the archive to explicitly do the extraction in the root directory? Then they're consciously impacting critical system locations, and can't do it by accident.
4
Sometimes features are problems. I found this thread after setting up a backup script that would email on a non-zero exit status fromtar
. This particular message was causingtar
to exit with a status of1
, which was causing me to receive false email alerts on successful backups, simply becausetar
was writing this message to STDERR. I fixed this issue by using my own tweaked version of @Marcus's solution:cd /path/to/network/share && tar -cJf scripts.backup.tar.xz -C / home/user/scripts 2>/dev/null || [send an email alert]
– rubynorails
Dec 14 '15 at 19:14
1
I think there are some use cases for using-P
- for example, if you're providing some kind of filesystem snapshotting feature in FUSE. Some of the time you may want to untar a snapshot in a specific directory rather than thecwd
from the perspective of the user.
– DIMMSum
Nov 28 '16 at 4:11
add a comment |
If you want to get rid of "Removing leading `/' from member names" being printed to STDERR, but still want to leave off those leading slashes as tar wisely does by default, I saw an excellent solution here by commenter timsoft.
The solution involves using -C option to change directory to the root (/), then specifying the file tree to archive without a leading slash, because now you only need a relative path. This does the same thing as a normal tar create command, but no stripping is needed:
tar fcz bkup.tar.gz -C / home/foo/
It won't work with incremental backups. But a good answer.
– gajdipajti
Aug 5 '14 at 7:12
It also doesn't work if you want to tar files denoted by shell-expansion (e.g.tar c -C / home/foo/*
), because the shell doesn't know about the changed root. But still a good answer for other cases.
– Boris
Apr 15 '15 at 6:27
1
-C /
doesn't work for me, period. It doesn't prevent the stderr for me.
– A-B-B
Aug 10 '15 at 7:45
2
@A-B-B make sure the path following -C / is relative.
– zurfyx
Dec 31 '16 at 10:17
Will this approach work if a user doesn't have permissions on the root directory?
– Mladen B.
Dec 2 '17 at 7:20
add a comment |
One month late, but I found the most appropriate solution for my case (in a shell script) is to go to the parent directory and execute the command there.
cd /var/www/
tar -czf mysite.gz mysite
Instead of:
tar -czf /var/www/mysite.gz /var/www/mysite
6
Indeed you could have done it using: tar -zcvf mysite.gz -C /var/www/ mysite/ The benefit of this is that you can execute it from any directory
– alfredocambera
Aug 25 '16 at 15:19
@alfredocambera thanks for this (y)
– amd
Aug 26 '16 at 11:15
Thank you for your help. This is what I needed. I didn't want the full path in my tarball, just the destination folder.
– Christia
Jan 11 '17 at 1:37
This should probably have been accepted as a correct answer, I think.
– Mladen B.
Dec 2 '17 at 7:19
I don't see how this answers the question at all. It's something different that could be done. … … … Like: Q: How do I get from Los Angeles to New York? A: Go to San Francisco instead.
– G-Man
May 2 '18 at 20:49
|
show 1 more comment
This is how I did it by using brute force method: 2>&1 | grep -v "Removing leading"
.
For example:
tar -cf "$BKUPDIR/$BKUPFILE.tar" --overwrite --exclude '.*' --one-file-system "$SRCDIR" 2>&1 | grep -v "Removing leading"
3
The problem with this is it hides the error code. So if you want to check the error code from tar in a bash script, then it won't return 0 on success.
– Brian
Apr 7 '15 at 15:19
@staticx, the error code is 0 regardless.
– A-B-B
Aug 10 '15 at 7:58
1
@A-B-B Mytar
command may return2
on fatal error.
– Jite
Nov 2 '15 at 15:46
1
@Brian you can use $PIPESTATUS to get the exit code. See unix.stackexchange.com/questions/14270/…
– simpleuser
Sep 10 '16 at 17:13
add a comment |
I solved this problem with:
cd /home/foo && tar czf ~/backup.tar.gz .
that way you aren't trying to put absolute paths into the tar archive in the first place. If you want untar it at the root of the file system you just
cd / && tar xzf backupt.tar.gz
after transferring it.
add a comment |
Try to use -C
for path only which would prevent compressing with complete paths:
root@server # tar fcz bkup.tar.gz -C /home/ foo/
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f59243%2ftar-removing-leading-from-member-names%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the --absolute-names
or -P
option to disable this feature.
tar fczP bkup.tar.gz /home/foo/
tar fcz bkup.tar.gz --absolute-names /home/foo
45
This is the correct answer, but be aware, that in most cases, this is not what you want, cause it results in an archive that extracts in complete paths!
– rubo77
Nov 21 '13 at 10:04
7
Using the-C /
option as described in @Marcus' answer will git rid of the STDERR message if that is your primary goal.
– Matt Sanders
Dec 28 '13 at 22:29
1
As @rubo77 commented, usually this is not the expected result of the command.
– alfredocambera
Aug 25 '16 at 15:16
3
Most of the time this is not what a user wants, simply because most of them don't read the manual until they need to (ain't nobody got time for that). So, it would be wise to, at least, expand your answer, warning people not to do this unless they are absolutely sure they understand what's the effect of it. Especially being accepted as an answer.
– Mladen B.
Dec 2 '17 at 7:16
1
@Toskan Examples added
– Barmar
Sep 5 '18 at 17:20
|
show 2 more comments
Use the --absolute-names
or -P
option to disable this feature.
tar fczP bkup.tar.gz /home/foo/
tar fcz bkup.tar.gz --absolute-names /home/foo
45
This is the correct answer, but be aware, that in most cases, this is not what you want, cause it results in an archive that extracts in complete paths!
– rubo77
Nov 21 '13 at 10:04
7
Using the-C /
option as described in @Marcus' answer will git rid of the STDERR message if that is your primary goal.
– Matt Sanders
Dec 28 '13 at 22:29
1
As @rubo77 commented, usually this is not the expected result of the command.
– alfredocambera
Aug 25 '16 at 15:16
3
Most of the time this is not what a user wants, simply because most of them don't read the manual until they need to (ain't nobody got time for that). So, it would be wise to, at least, expand your answer, warning people not to do this unless they are absolutely sure they understand what's the effect of it. Especially being accepted as an answer.
– Mladen B.
Dec 2 '17 at 7:16
1
@Toskan Examples added
– Barmar
Sep 5 '18 at 17:20
|
show 2 more comments
Use the --absolute-names
or -P
option to disable this feature.
tar fczP bkup.tar.gz /home/foo/
tar fcz bkup.tar.gz --absolute-names /home/foo
Use the --absolute-names
or -P
option to disable this feature.
tar fczP bkup.tar.gz /home/foo/
tar fcz bkup.tar.gz --absolute-names /home/foo
edited Sep 5 '18 at 17:20
answered Dec 23 '12 at 12:57
BarmarBarmar
7,0721424
7,0721424
45
This is the correct answer, but be aware, that in most cases, this is not what you want, cause it results in an archive that extracts in complete paths!
– rubo77
Nov 21 '13 at 10:04
7
Using the-C /
option as described in @Marcus' answer will git rid of the STDERR message if that is your primary goal.
– Matt Sanders
Dec 28 '13 at 22:29
1
As @rubo77 commented, usually this is not the expected result of the command.
– alfredocambera
Aug 25 '16 at 15:16
3
Most of the time this is not what a user wants, simply because most of them don't read the manual until they need to (ain't nobody got time for that). So, it would be wise to, at least, expand your answer, warning people not to do this unless they are absolutely sure they understand what's the effect of it. Especially being accepted as an answer.
– Mladen B.
Dec 2 '17 at 7:16
1
@Toskan Examples added
– Barmar
Sep 5 '18 at 17:20
|
show 2 more comments
45
This is the correct answer, but be aware, that in most cases, this is not what you want, cause it results in an archive that extracts in complete paths!
– rubo77
Nov 21 '13 at 10:04
7
Using the-C /
option as described in @Marcus' answer will git rid of the STDERR message if that is your primary goal.
– Matt Sanders
Dec 28 '13 at 22:29
1
As @rubo77 commented, usually this is not the expected result of the command.
– alfredocambera
Aug 25 '16 at 15:16
3
Most of the time this is not what a user wants, simply because most of them don't read the manual until they need to (ain't nobody got time for that). So, it would be wise to, at least, expand your answer, warning people not to do this unless they are absolutely sure they understand what's the effect of it. Especially being accepted as an answer.
– Mladen B.
Dec 2 '17 at 7:16
1
@Toskan Examples added
– Barmar
Sep 5 '18 at 17:20
45
45
This is the correct answer, but be aware, that in most cases, this is not what you want, cause it results in an archive that extracts in complete paths!
– rubo77
Nov 21 '13 at 10:04
This is the correct answer, but be aware, that in most cases, this is not what you want, cause it results in an archive that extracts in complete paths!
– rubo77
Nov 21 '13 at 10:04
7
7
Using the
-C /
option as described in @Marcus' answer will git rid of the STDERR message if that is your primary goal.– Matt Sanders
Dec 28 '13 at 22:29
Using the
-C /
option as described in @Marcus' answer will git rid of the STDERR message if that is your primary goal.– Matt Sanders
Dec 28 '13 at 22:29
1
1
As @rubo77 commented, usually this is not the expected result of the command.
– alfredocambera
Aug 25 '16 at 15:16
As @rubo77 commented, usually this is not the expected result of the command.
– alfredocambera
Aug 25 '16 at 15:16
3
3
Most of the time this is not what a user wants, simply because most of them don't read the manual until they need to (ain't nobody got time for that). So, it would be wise to, at least, expand your answer, warning people not to do this unless they are absolutely sure they understand what's the effect of it. Especially being accepted as an answer.
– Mladen B.
Dec 2 '17 at 7:16
Most of the time this is not what a user wants, simply because most of them don't read the manual until they need to (ain't nobody got time for that). So, it would be wise to, at least, expand your answer, warning people not to do this unless they are absolutely sure they understand what's the effect of it. Especially being accepted as an answer.
– Mladen B.
Dec 2 '17 at 7:16
1
1
@Toskan Examples added
– Barmar
Sep 5 '18 at 17:20
@Toskan Examples added
– Barmar
Sep 5 '18 at 17:20
|
show 2 more comments
That's actually a feature, not a problem. Archives with absolute locations are a security risk. Attackers could use such archives to trick users into installing files in critical system locations.
Yes, you could use -P
. But what's wrong with allowing tar to remove the forward slash, and simply requiring the user of the archive to explicitly do the extraction in the root directory? Then they're consciously impacting critical system locations, and can't do it by accident.
4
Sometimes features are problems. I found this thread after setting up a backup script that would email on a non-zero exit status fromtar
. This particular message was causingtar
to exit with a status of1
, which was causing me to receive false email alerts on successful backups, simply becausetar
was writing this message to STDERR. I fixed this issue by using my own tweaked version of @Marcus's solution:cd /path/to/network/share && tar -cJf scripts.backup.tar.xz -C / home/user/scripts 2>/dev/null || [send an email alert]
– rubynorails
Dec 14 '15 at 19:14
1
I think there are some use cases for using-P
- for example, if you're providing some kind of filesystem snapshotting feature in FUSE. Some of the time you may want to untar a snapshot in a specific directory rather than thecwd
from the perspective of the user.
– DIMMSum
Nov 28 '16 at 4:11
add a comment |
That's actually a feature, not a problem. Archives with absolute locations are a security risk. Attackers could use such archives to trick users into installing files in critical system locations.
Yes, you could use -P
. But what's wrong with allowing tar to remove the forward slash, and simply requiring the user of the archive to explicitly do the extraction in the root directory? Then they're consciously impacting critical system locations, and can't do it by accident.
4
Sometimes features are problems. I found this thread after setting up a backup script that would email on a non-zero exit status fromtar
. This particular message was causingtar
to exit with a status of1
, which was causing me to receive false email alerts on successful backups, simply becausetar
was writing this message to STDERR. I fixed this issue by using my own tweaked version of @Marcus's solution:cd /path/to/network/share && tar -cJf scripts.backup.tar.xz -C / home/user/scripts 2>/dev/null || [send an email alert]
– rubynorails
Dec 14 '15 at 19:14
1
I think there are some use cases for using-P
- for example, if you're providing some kind of filesystem snapshotting feature in FUSE. Some of the time you may want to untar a snapshot in a specific directory rather than thecwd
from the perspective of the user.
– DIMMSum
Nov 28 '16 at 4:11
add a comment |
That's actually a feature, not a problem. Archives with absolute locations are a security risk. Attackers could use such archives to trick users into installing files in critical system locations.
Yes, you could use -P
. But what's wrong with allowing tar to remove the forward slash, and simply requiring the user of the archive to explicitly do the extraction in the root directory? Then they're consciously impacting critical system locations, and can't do it by accident.
That's actually a feature, not a problem. Archives with absolute locations are a security risk. Attackers could use such archives to trick users into installing files in critical system locations.
Yes, you could use -P
. But what's wrong with allowing tar to remove the forward slash, and simply requiring the user of the archive to explicitly do the extraction in the root directory? Then they're consciously impacting critical system locations, and can't do it by accident.
answered Dec 23 '12 at 15:28
Mark AdlerMark Adler
1,42131112
1,42131112
4
Sometimes features are problems. I found this thread after setting up a backup script that would email on a non-zero exit status fromtar
. This particular message was causingtar
to exit with a status of1
, which was causing me to receive false email alerts on successful backups, simply becausetar
was writing this message to STDERR. I fixed this issue by using my own tweaked version of @Marcus's solution:cd /path/to/network/share && tar -cJf scripts.backup.tar.xz -C / home/user/scripts 2>/dev/null || [send an email alert]
– rubynorails
Dec 14 '15 at 19:14
1
I think there are some use cases for using-P
- for example, if you're providing some kind of filesystem snapshotting feature in FUSE. Some of the time you may want to untar a snapshot in a specific directory rather than thecwd
from the perspective of the user.
– DIMMSum
Nov 28 '16 at 4:11
add a comment |
4
Sometimes features are problems. I found this thread after setting up a backup script that would email on a non-zero exit status fromtar
. This particular message was causingtar
to exit with a status of1
, which was causing me to receive false email alerts on successful backups, simply becausetar
was writing this message to STDERR. I fixed this issue by using my own tweaked version of @Marcus's solution:cd /path/to/network/share && tar -cJf scripts.backup.tar.xz -C / home/user/scripts 2>/dev/null || [send an email alert]
– rubynorails
Dec 14 '15 at 19:14
1
I think there are some use cases for using-P
- for example, if you're providing some kind of filesystem snapshotting feature in FUSE. Some of the time you may want to untar a snapshot in a specific directory rather than thecwd
from the perspective of the user.
– DIMMSum
Nov 28 '16 at 4:11
4
4
Sometimes features are problems. I found this thread after setting up a backup script that would email on a non-zero exit status from
tar
. This particular message was causing tar
to exit with a status of 1
, which was causing me to receive false email alerts on successful backups, simply because tar
was writing this message to STDERR. I fixed this issue by using my own tweaked version of @Marcus's solution: cd /path/to/network/share && tar -cJf scripts.backup.tar.xz -C / home/user/scripts 2>/dev/null || [send an email alert]
– rubynorails
Dec 14 '15 at 19:14
Sometimes features are problems. I found this thread after setting up a backup script that would email on a non-zero exit status from
tar
. This particular message was causing tar
to exit with a status of 1
, which was causing me to receive false email alerts on successful backups, simply because tar
was writing this message to STDERR. I fixed this issue by using my own tweaked version of @Marcus's solution: cd /path/to/network/share && tar -cJf scripts.backup.tar.xz -C / home/user/scripts 2>/dev/null || [send an email alert]
– rubynorails
Dec 14 '15 at 19:14
1
1
I think there are some use cases for using
-P
- for example, if you're providing some kind of filesystem snapshotting feature in FUSE. Some of the time you may want to untar a snapshot in a specific directory rather than the cwd
from the perspective of the user.– DIMMSum
Nov 28 '16 at 4:11
I think there are some use cases for using
-P
- for example, if you're providing some kind of filesystem snapshotting feature in FUSE. Some of the time you may want to untar a snapshot in a specific directory rather than the cwd
from the perspective of the user.– DIMMSum
Nov 28 '16 at 4:11
add a comment |
If you want to get rid of "Removing leading `/' from member names" being printed to STDERR, but still want to leave off those leading slashes as tar wisely does by default, I saw an excellent solution here by commenter timsoft.
The solution involves using -C option to change directory to the root (/), then specifying the file tree to archive without a leading slash, because now you only need a relative path. This does the same thing as a normal tar create command, but no stripping is needed:
tar fcz bkup.tar.gz -C / home/foo/
It won't work with incremental backups. But a good answer.
– gajdipajti
Aug 5 '14 at 7:12
It also doesn't work if you want to tar files denoted by shell-expansion (e.g.tar c -C / home/foo/*
), because the shell doesn't know about the changed root. But still a good answer for other cases.
– Boris
Apr 15 '15 at 6:27
1
-C /
doesn't work for me, period. It doesn't prevent the stderr for me.
– A-B-B
Aug 10 '15 at 7:45
2
@A-B-B make sure the path following -C / is relative.
– zurfyx
Dec 31 '16 at 10:17
Will this approach work if a user doesn't have permissions on the root directory?
– Mladen B.
Dec 2 '17 at 7:20
add a comment |
If you want to get rid of "Removing leading `/' from member names" being printed to STDERR, but still want to leave off those leading slashes as tar wisely does by default, I saw an excellent solution here by commenter timsoft.
The solution involves using -C option to change directory to the root (/), then specifying the file tree to archive without a leading slash, because now you only need a relative path. This does the same thing as a normal tar create command, but no stripping is needed:
tar fcz bkup.tar.gz -C / home/foo/
It won't work with incremental backups. But a good answer.
– gajdipajti
Aug 5 '14 at 7:12
It also doesn't work if you want to tar files denoted by shell-expansion (e.g.tar c -C / home/foo/*
), because the shell doesn't know about the changed root. But still a good answer for other cases.
– Boris
Apr 15 '15 at 6:27
1
-C /
doesn't work for me, period. It doesn't prevent the stderr for me.
– A-B-B
Aug 10 '15 at 7:45
2
@A-B-B make sure the path following -C / is relative.
– zurfyx
Dec 31 '16 at 10:17
Will this approach work if a user doesn't have permissions on the root directory?
– Mladen B.
Dec 2 '17 at 7:20
add a comment |
If you want to get rid of "Removing leading `/' from member names" being printed to STDERR, but still want to leave off those leading slashes as tar wisely does by default, I saw an excellent solution here by commenter timsoft.
The solution involves using -C option to change directory to the root (/), then specifying the file tree to archive without a leading slash, because now you only need a relative path. This does the same thing as a normal tar create command, but no stripping is needed:
tar fcz bkup.tar.gz -C / home/foo/
If you want to get rid of "Removing leading `/' from member names" being printed to STDERR, but still want to leave off those leading slashes as tar wisely does by default, I saw an excellent solution here by commenter timsoft.
The solution involves using -C option to change directory to the root (/), then specifying the file tree to archive without a leading slash, because now you only need a relative path. This does the same thing as a normal tar create command, but no stripping is needed:
tar fcz bkup.tar.gz -C / home/foo/
answered Jan 18 '13 at 15:33
MarcusMarcus
80154
80154
It won't work with incremental backups. But a good answer.
– gajdipajti
Aug 5 '14 at 7:12
It also doesn't work if you want to tar files denoted by shell-expansion (e.g.tar c -C / home/foo/*
), because the shell doesn't know about the changed root. But still a good answer for other cases.
– Boris
Apr 15 '15 at 6:27
1
-C /
doesn't work for me, period. It doesn't prevent the stderr for me.
– A-B-B
Aug 10 '15 at 7:45
2
@A-B-B make sure the path following -C / is relative.
– zurfyx
Dec 31 '16 at 10:17
Will this approach work if a user doesn't have permissions on the root directory?
– Mladen B.
Dec 2 '17 at 7:20
add a comment |
It won't work with incremental backups. But a good answer.
– gajdipajti
Aug 5 '14 at 7:12
It also doesn't work if you want to tar files denoted by shell-expansion (e.g.tar c -C / home/foo/*
), because the shell doesn't know about the changed root. But still a good answer for other cases.
– Boris
Apr 15 '15 at 6:27
1
-C /
doesn't work for me, period. It doesn't prevent the stderr for me.
– A-B-B
Aug 10 '15 at 7:45
2
@A-B-B make sure the path following -C / is relative.
– zurfyx
Dec 31 '16 at 10:17
Will this approach work if a user doesn't have permissions on the root directory?
– Mladen B.
Dec 2 '17 at 7:20
It won't work with incremental backups. But a good answer.
– gajdipajti
Aug 5 '14 at 7:12
It won't work with incremental backups. But a good answer.
– gajdipajti
Aug 5 '14 at 7:12
It also doesn't work if you want to tar files denoted by shell-expansion (e.g.
tar c -C / home/foo/*
), because the shell doesn't know about the changed root. But still a good answer for other cases.– Boris
Apr 15 '15 at 6:27
It also doesn't work if you want to tar files denoted by shell-expansion (e.g.
tar c -C / home/foo/*
), because the shell doesn't know about the changed root. But still a good answer for other cases.– Boris
Apr 15 '15 at 6:27
1
1
-C /
doesn't work for me, period. It doesn't prevent the stderr for me.– A-B-B
Aug 10 '15 at 7:45
-C /
doesn't work for me, period. It doesn't prevent the stderr for me.– A-B-B
Aug 10 '15 at 7:45
2
2
@A-B-B make sure the path following -C / is relative.
– zurfyx
Dec 31 '16 at 10:17
@A-B-B make sure the path following -C / is relative.
– zurfyx
Dec 31 '16 at 10:17
Will this approach work if a user doesn't have permissions on the root directory?
– Mladen B.
Dec 2 '17 at 7:20
Will this approach work if a user doesn't have permissions on the root directory?
– Mladen B.
Dec 2 '17 at 7:20
add a comment |
One month late, but I found the most appropriate solution for my case (in a shell script) is to go to the parent directory and execute the command there.
cd /var/www/
tar -czf mysite.gz mysite
Instead of:
tar -czf /var/www/mysite.gz /var/www/mysite
6
Indeed you could have done it using: tar -zcvf mysite.gz -C /var/www/ mysite/ The benefit of this is that you can execute it from any directory
– alfredocambera
Aug 25 '16 at 15:19
@alfredocambera thanks for this (y)
– amd
Aug 26 '16 at 11:15
Thank you for your help. This is what I needed. I didn't want the full path in my tarball, just the destination folder.
– Christia
Jan 11 '17 at 1:37
This should probably have been accepted as a correct answer, I think.
– Mladen B.
Dec 2 '17 at 7:19
I don't see how this answers the question at all. It's something different that could be done. … … … Like: Q: How do I get from Los Angeles to New York? A: Go to San Francisco instead.
– G-Man
May 2 '18 at 20:49
|
show 1 more comment
One month late, but I found the most appropriate solution for my case (in a shell script) is to go to the parent directory and execute the command there.
cd /var/www/
tar -czf mysite.gz mysite
Instead of:
tar -czf /var/www/mysite.gz /var/www/mysite
6
Indeed you could have done it using: tar -zcvf mysite.gz -C /var/www/ mysite/ The benefit of this is that you can execute it from any directory
– alfredocambera
Aug 25 '16 at 15:19
@alfredocambera thanks for this (y)
– amd
Aug 26 '16 at 11:15
Thank you for your help. This is what I needed. I didn't want the full path in my tarball, just the destination folder.
– Christia
Jan 11 '17 at 1:37
This should probably have been accepted as a correct answer, I think.
– Mladen B.
Dec 2 '17 at 7:19
I don't see how this answers the question at all. It's something different that could be done. … … … Like: Q: How do I get from Los Angeles to New York? A: Go to San Francisco instead.
– G-Man
May 2 '18 at 20:49
|
show 1 more comment
One month late, but I found the most appropriate solution for my case (in a shell script) is to go to the parent directory and execute the command there.
cd /var/www/
tar -czf mysite.gz mysite
Instead of:
tar -czf /var/www/mysite.gz /var/www/mysite
One month late, but I found the most appropriate solution for my case (in a shell script) is to go to the parent directory and execute the command there.
cd /var/www/
tar -czf mysite.gz mysite
Instead of:
tar -czf /var/www/mysite.gz /var/www/mysite
answered May 24 '15 at 6:52
amdamd
36733
36733
6
Indeed you could have done it using: tar -zcvf mysite.gz -C /var/www/ mysite/ The benefit of this is that you can execute it from any directory
– alfredocambera
Aug 25 '16 at 15:19
@alfredocambera thanks for this (y)
– amd
Aug 26 '16 at 11:15
Thank you for your help. This is what I needed. I didn't want the full path in my tarball, just the destination folder.
– Christia
Jan 11 '17 at 1:37
This should probably have been accepted as a correct answer, I think.
– Mladen B.
Dec 2 '17 at 7:19
I don't see how this answers the question at all. It's something different that could be done. … … … Like: Q: How do I get from Los Angeles to New York? A: Go to San Francisco instead.
– G-Man
May 2 '18 at 20:49
|
show 1 more comment
6
Indeed you could have done it using: tar -zcvf mysite.gz -C /var/www/ mysite/ The benefit of this is that you can execute it from any directory
– alfredocambera
Aug 25 '16 at 15:19
@alfredocambera thanks for this (y)
– amd
Aug 26 '16 at 11:15
Thank you for your help. This is what I needed. I didn't want the full path in my tarball, just the destination folder.
– Christia
Jan 11 '17 at 1:37
This should probably have been accepted as a correct answer, I think.
– Mladen B.
Dec 2 '17 at 7:19
I don't see how this answers the question at all. It's something different that could be done. … … … Like: Q: How do I get from Los Angeles to New York? A: Go to San Francisco instead.
– G-Man
May 2 '18 at 20:49
6
6
Indeed you could have done it using: tar -zcvf mysite.gz -C /var/www/ mysite/ The benefit of this is that you can execute it from any directory
– alfredocambera
Aug 25 '16 at 15:19
Indeed you could have done it using: tar -zcvf mysite.gz -C /var/www/ mysite/ The benefit of this is that you can execute it from any directory
– alfredocambera
Aug 25 '16 at 15:19
@alfredocambera thanks for this (y)
– amd
Aug 26 '16 at 11:15
@alfredocambera thanks for this (y)
– amd
Aug 26 '16 at 11:15
Thank you for your help. This is what I needed. I didn't want the full path in my tarball, just the destination folder.
– Christia
Jan 11 '17 at 1:37
Thank you for your help. This is what I needed. I didn't want the full path in my tarball, just the destination folder.
– Christia
Jan 11 '17 at 1:37
This should probably have been accepted as a correct answer, I think.
– Mladen B.
Dec 2 '17 at 7:19
This should probably have been accepted as a correct answer, I think.
– Mladen B.
Dec 2 '17 at 7:19
I don't see how this answers the question at all. It's something different that could be done. … … … Like: Q: How do I get from Los Angeles to New York? A: Go to San Francisco instead.
– G-Man
May 2 '18 at 20:49
I don't see how this answers the question at all. It's something different that could be done. … … … Like: Q: How do I get from Los Angeles to New York? A: Go to San Francisco instead.
– G-Man
May 2 '18 at 20:49
|
show 1 more comment
This is how I did it by using brute force method: 2>&1 | grep -v "Removing leading"
.
For example:
tar -cf "$BKUPDIR/$BKUPFILE.tar" --overwrite --exclude '.*' --one-file-system "$SRCDIR" 2>&1 | grep -v "Removing leading"
3
The problem with this is it hides the error code. So if you want to check the error code from tar in a bash script, then it won't return 0 on success.
– Brian
Apr 7 '15 at 15:19
@staticx, the error code is 0 regardless.
– A-B-B
Aug 10 '15 at 7:58
1
@A-B-B Mytar
command may return2
on fatal error.
– Jite
Nov 2 '15 at 15:46
1
@Brian you can use $PIPESTATUS to get the exit code. See unix.stackexchange.com/questions/14270/…
– simpleuser
Sep 10 '16 at 17:13
add a comment |
This is how I did it by using brute force method: 2>&1 | grep -v "Removing leading"
.
For example:
tar -cf "$BKUPDIR/$BKUPFILE.tar" --overwrite --exclude '.*' --one-file-system "$SRCDIR" 2>&1 | grep -v "Removing leading"
3
The problem with this is it hides the error code. So if you want to check the error code from tar in a bash script, then it won't return 0 on success.
– Brian
Apr 7 '15 at 15:19
@staticx, the error code is 0 regardless.
– A-B-B
Aug 10 '15 at 7:58
1
@A-B-B Mytar
command may return2
on fatal error.
– Jite
Nov 2 '15 at 15:46
1
@Brian you can use $PIPESTATUS to get the exit code. See unix.stackexchange.com/questions/14270/…
– simpleuser
Sep 10 '16 at 17:13
add a comment |
This is how I did it by using brute force method: 2>&1 | grep -v "Removing leading"
.
For example:
tar -cf "$BKUPDIR/$BKUPFILE.tar" --overwrite --exclude '.*' --one-file-system "$SRCDIR" 2>&1 | grep -v "Removing leading"
This is how I did it by using brute force method: 2>&1 | grep -v "Removing leading"
.
For example:
tar -cf "$BKUPDIR/$BKUPFILE.tar" --overwrite --exclude '.*' --one-file-system "$SRCDIR" 2>&1 | grep -v "Removing leading"
edited Mar 30 '15 at 21:12
kenorb
8,806372109
8,806372109
answered Aug 23 '13 at 1:00
bjackflybjackfly
17913
17913
3
The problem with this is it hides the error code. So if you want to check the error code from tar in a bash script, then it won't return 0 on success.
– Brian
Apr 7 '15 at 15:19
@staticx, the error code is 0 regardless.
– A-B-B
Aug 10 '15 at 7:58
1
@A-B-B Mytar
command may return2
on fatal error.
– Jite
Nov 2 '15 at 15:46
1
@Brian you can use $PIPESTATUS to get the exit code. See unix.stackexchange.com/questions/14270/…
– simpleuser
Sep 10 '16 at 17:13
add a comment |
3
The problem with this is it hides the error code. So if you want to check the error code from tar in a bash script, then it won't return 0 on success.
– Brian
Apr 7 '15 at 15:19
@staticx, the error code is 0 regardless.
– A-B-B
Aug 10 '15 at 7:58
1
@A-B-B Mytar
command may return2
on fatal error.
– Jite
Nov 2 '15 at 15:46
1
@Brian you can use $PIPESTATUS to get the exit code. See unix.stackexchange.com/questions/14270/…
– simpleuser
Sep 10 '16 at 17:13
3
3
The problem with this is it hides the error code. So if you want to check the error code from tar in a bash script, then it won't return 0 on success.
– Brian
Apr 7 '15 at 15:19
The problem with this is it hides the error code. So if you want to check the error code from tar in a bash script, then it won't return 0 on success.
– Brian
Apr 7 '15 at 15:19
@staticx, the error code is 0 regardless.
– A-B-B
Aug 10 '15 at 7:58
@staticx, the error code is 0 regardless.
– A-B-B
Aug 10 '15 at 7:58
1
1
@A-B-B My
tar
command may return 2
on fatal error.– Jite
Nov 2 '15 at 15:46
@A-B-B My
tar
command may return 2
on fatal error.– Jite
Nov 2 '15 at 15:46
1
1
@Brian you can use $PIPESTATUS to get the exit code. See unix.stackexchange.com/questions/14270/…
– simpleuser
Sep 10 '16 at 17:13
@Brian you can use $PIPESTATUS to get the exit code. See unix.stackexchange.com/questions/14270/…
– simpleuser
Sep 10 '16 at 17:13
add a comment |
I solved this problem with:
cd /home/foo && tar czf ~/backup.tar.gz .
that way you aren't trying to put absolute paths into the tar archive in the first place. If you want untar it at the root of the file system you just
cd / && tar xzf backupt.tar.gz
after transferring it.
add a comment |
I solved this problem with:
cd /home/foo && tar czf ~/backup.tar.gz .
that way you aren't trying to put absolute paths into the tar archive in the first place. If you want untar it at the root of the file system you just
cd / && tar xzf backupt.tar.gz
after transferring it.
add a comment |
I solved this problem with:
cd /home/foo && tar czf ~/backup.tar.gz .
that way you aren't trying to put absolute paths into the tar archive in the first place. If you want untar it at the root of the file system you just
cd / && tar xzf backupt.tar.gz
after transferring it.
I solved this problem with:
cd /home/foo && tar czf ~/backup.tar.gz .
that way you aren't trying to put absolute paths into the tar archive in the first place. If you want untar it at the root of the file system you just
cd / && tar xzf backupt.tar.gz
after transferring it.
answered Jul 21 '17 at 15:44
JamesJames
211
211
add a comment |
add a comment |
Try to use -C
for path only which would prevent compressing with complete paths:
root@server # tar fcz bkup.tar.gz -C /home/ foo/
add a comment |
Try to use -C
for path only which would prevent compressing with complete paths:
root@server # tar fcz bkup.tar.gz -C /home/ foo/
add a comment |
Try to use -C
for path only which would prevent compressing with complete paths:
root@server # tar fcz bkup.tar.gz -C /home/ foo/
Try to use -C
for path only which would prevent compressing with complete paths:
root@server # tar fcz bkup.tar.gz -C /home/ foo/
answered Jul 25 '17 at 10:35
Nick TsaiNick Tsai
1213
1213
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f59243%2ftar-removing-leading-from-member-names%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What exactly is not working as expected?
– Joachim Isaksson
Dec 23 '12 at 12:48
28
It is not a problem. You do not want leading slashes in a tar archive. Seriously. If you want to extract an archive to your system root, specify
-C /
when extracting it.– ThiefMaster
Dec 23 '12 at 15:31