How to extract only files without creating directory paths from tar.gz file

tar

I have a tar.gz file from which I have to extract only the files in current directory without creating the whole structure.

For example:

tar.gz contains below files,

/u01/app/oracle/file1
..
..
/u01/app/oracle/file10
/u01/testdata/file1
..
..
/u01/testdata/file5

the tar.gz is present in /u02. So when extracting I want file1 through file10 coming under /u02 instead, the whole directory structure is getting created under /u02

Best Answer

Since you seem to have a fixed number of path components, pass --strip-components=3 to tar to remove /u01/app/oracle while extracting to /u02

For a variable number of components, use the --transform flag instead. Something along the lines of --transform='s,^\([^/][^/]*/\)\+,,'

Related Question