# tar command help -- extract single file

**URL:** <https://community.unix.com/t/tar-command-help-extract-single-file/203132>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [July 1, 2008, 2:20am UTC](https://community.unix.com/t/tar-command-help-extract-single-file/203132 "2008-07-01T02:20:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![icefish](https://community.unix.com/letter_avatar/icefish/32/5_5575768a8748004e209b776fc1b2916d.png) [@icefish](https://community.unix.com/u/icefish)\
**Post date:** [July 1, 2008, 2:20am UTC](https://community.unix.com/t/tar-command-help-extract-single-file/203132/1 "2008-07-01T02:20:35Z")

</div>

Hi,  
I want to view/display the contents of one file in tar file.  
For example if the tar file is sam.tar.gz and one of the file inside is E1.txt, how do i view the contents of this E1.txt file.

Olso if I want to extract the E1.txt file only from sam.tar.gz how can i do that.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [July 1, 2008, 2:34am UTC](https://community.unix.com/t/tar-command-help-extract-single-file/203132/2 "2008-07-01T02:34:48Z")

</div>

To print the files use "zcat". But I know no option for it to cat only a single file of the archive.

To list the contents of an archive, use:

```nohighlight
tar tvf my.tar
# or
tar tvzf my.tar.gz

```

To extract the file "singlefile.dat" of the archive:

```nohighlight
tar xvzf my.tar.gz singlefile.dat

```

---

<div class="post-metadata">

**Author:** ![icefish](https://community.unix.com/letter_avatar/icefish/32/5_5575768a8748004e209b776fc1b2916d.png) [@icefish](https://community.unix.com/u/icefish)\
**Post date:** [July 1, 2008, 2:40am UTC](https://community.unix.com/t/tar-command-help-extract-single-file/203132/3 "2008-07-01T02:40:30Z")

</div>

Thanks.

One more question  
Where will the singlefile.dat be extracted. I dont want to overwrite any existing file.

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [July 1, 2008, 2:48am UTC](https://community.unix.com/t/tar-command-help-extract-single-file/203132/4 "2008-07-01T02:48:29Z")

</div>

Usually in your pwd where you currently are. If done non-interactive via script and cronjob for example, you should do a cd to be sure, you are at the place in the filesystem, where you want to be.  
I avoid to tar stuff with a leading slash. So I can always move to a tmp directory and extract there to avoid overwriting.  
It's always a bad surprise if some &\*!@$! (to have it in comic speech censorship) creates an archive with absolute path. That can cause an unwanted update of your data heh.

---

<div class="post-metadata">

**Author:** ![icefish](https://community.unix.com/letter_avatar/icefish/32/5_5575768a8748004e209b776fc1b2916d.png) [@icefish](https://community.unix.com/u/icefish)\
**Post date:** [July 1, 2008, 2:58am UTC](https://community.unix.com/t/tar-command-help-extract-single-file/203132/5 "2008-07-01T02:58:45Z")

</div>

Actually I tared the file with absolute path like the command below  
tar -cvf sam.tar /home/sam

Now I want to extract /home/sam/E1.txt in sam.tar to /home/ram

Please help.

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [July 1, 2008, 3:24am UTC](https://community.unix.com/t/tar-command-help-extract-single-file/203132/6 "2008-07-01T03:24:29Z")

</div>

As I said, please tar without absolute paths. You can get more problems of this than you'd like. Just create some tmp subdirectory close to your destination, uncompress/untar it there and move it to where it belongs.

---

<div class="post-metadata">

**Author:** ![spirtle](https://community.unix.com/letter_avatar/spirtle/32/5_5575768a8748004e209b776fc1b2916d.png) [@spirtle](https://community.unix.com/u/spirtle)\
**Post date:** [July 1, 2008, 5:45am UTC](https://community.unix.com/t/tar-command-help-extract-single-file/203132/7 "2008-07-01T05:45:40Z")

</div>

Gnu tar helpfully removes any leading / in a path to avoid the potential problems that zaxxon describes -- unless specifically told to keep it with -P. If your tar doesn't do this, you can only do

```nohighlight
tar -xf sam.tar /home/sam/E1.txt
cp /home/sam/E1.txt /home/ram

```

i.e. you will create E1.txt with the absolute rather than a relative path.  
tar -x can only extract from the archive what you put in there; use -t (as zaxxon said) to see what that is.

---

<div class="post-metadata">

**Author:** ![jhtrice](https://community.unix.com/user_avatar/community.unix.com/jhtrice/32/995_2.png) [@jhtrice](https://community.unix.com/u/jhtrice)\
**Post date:** [July 1, 2008, 2:02pm UTC](https://community.unix.com/t/tar-command-help-extract-single-file/203132/8 "2008-07-01T14:02:37Z")

</div>

With GNU tar you can use --strip to remove the top level directories when unpacking. See "man tar".
