1

H All,

I am trying to archive files older than specific duration, when I am trying to untar the tar file it retains the directory structure from root folder. I don't want to retain parent folders

command used :

find /home/prj/myfolder/* -mtime 1 -type f | 
    while read file; 
    do
        tar -Pcvzf archive.tar.gz --remove-files "$file"
    done

Problem: File location is /home/prj/myfolder/abc/a.txt when I untar archive.tar.gz it contains /home/prj/myfolder/abc/a.txt dir stucture.

I am expecting abc/a.txt

2 Answers 2

4

Drop the "/home/prj/myfolder/*" and use "cd /home/prj/myfolder/" to navigate to that location.

cd /home/prj/myfolder/
find . -mtime 1 -type f | 
    while read file; 
    do
        tar -Pcvzf archive.tar.gz --remove-files "$file"
    done

The files will then be stored with a relative path.

3
  • I using "/home/prj/myfolder/*" since I want to recursively iterate all sub folders under /home/prj/myfolder/ .If I manully use cd then script would be hardcoded. Can u please suggeste any other alternative way so that iteration would be dynamic ?
    – Peter
    May 25, 2016 at 11:31
  • and how is that different from the "cd" and "."?
    – Rinzwind
    May 25, 2016 at 11:32
  • If I use cd /home/prj/myfolder/ , if their is a file in /home/prj/myfolder/demo/temp/b2.txt .The tar file contains folder structure from demo/temp/b2.txt. My requirement is it should have only temp/b2.txt I dont want demo folder.In solutions provided by you it maintain folder sturcture after myfolder(/demo/) .I am looking for a generic solution if I use cd /home/prj/myfolder/ it would be scenario specific
    – Peter
    May 27, 2016 at 5:36
0

I'd use the solution in Rinzwind's answer. There's no real reason to avoid the cd; especially if you put this in a shell script, as the cd will affect just the script, not the shell you call the script from.

If you really really don't wan't to use cd, you can use tars --transform option:

gbl@roran:~$ tar -cf /tmp/archive.tar /home/gbl/Temp/a.out
gbl@roran:~$ tar -tf /tmp/archive.tar
home/gbl/Temp/a.out

gbl@roran:~$ tar --transform 's/^home\/gbl//' -cf /tmp/archive2.tar /home/gbl/Temp/a.out
gbl@roran:~$ tar -tf /tmp/archive2.tar
/Temp/a.out

gbl@roran:~$ tar --transform 's/^home\/gbl\///' -cf /tmp/archive3.tar /home/gbl/Temp/a.out
gbl@roran:~$ tar -tf /tmp/archive3.tar
Temp/a.out

I omitted some tar: Removing leading '/' from member names messages here as they don't contribute to the answer.

Use any sed regular expression for the transformation, read the manual for a few more options.

There's also --strip-components to remove that many path components from the start of the file name, but it works on extraction only, not when you create an archive.

However, this is restricted to gnu tar, so you'll be ok on Linux, but probably not whenever you switch to MacOS, Solaris, or AIX. Which is another reason to avoid it, especially as there's an easy different solution (cd first).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .