LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2015, 03:03 PM   #1
babyPen
LQ Newbie
 
Registered: May 2014
Posts: 17

Rep: Reputation: Disabled
TAR command assistance to have only files


Hello,

Help and assistance is much appreciated.

Can some one tell me how to tar only files with and not have directory included in them.

example: Directory1
|_ Sub_Directory_A
|_ Sub_Directory_B
|_Sub_Sub_Directory_C
|_File_1.txt
|_File_2.txt
|_File_3.txt

Currently when I tar using cmd: tar -cvf Directory1.tar Directory1
I am seeing:
Directory1/
Directory1/Sub_Directory_A
Directory1/Sub_Directory_B
Directory1/Sub_Directory_B/Sub_Sub_Directory_C
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_1.txt
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_2.txt
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_3.txt

But I actually need the tar to only list the file and should look like: when I do tar -tf Directory1.ta
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_1.txt
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_2.txt
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_3.txt

Suggestions are much appreciated.

Last edited by babyPen; 01-23-2015 at 03:10 PM. Reason: adding TAGS
 
Old 01-23-2015, 11:28 PM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
I think for that when preparing a tar archive only files should have been given.
 
Old 01-23-2015, 11:48 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,270
Blog Entries: 1

Rep: Reputation: 2304Reputation: 2304Reputation: 2304Reputation: 2304Reputation: 2304Reputation: 2304Reputation: 2304Reputation: 2304Reputation: 2304Reputation: 2304Reputation: 2304
Quote:
Originally Posted by babyPen View Post
Hello,

Help and assistance is much appreciated.

Can some one tell me how to tar only files with and not have directory included in them.

example: Directory1
|_ Sub_Directory_A
|_ Sub_Directory_B
|_Sub_Sub_Directory_C
|_File_1.txt
|_File_2.txt
|_File_3.txt

Currently when I tar using cmd: tar -cvf Directory1.tar Directory1
I am seeing:
Directory1/
Directory1/Sub_Directory_A
Directory1/Sub_Directory_B
Directory1/Sub_Directory_B/Sub_Sub_Directory_C
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_1.txt
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_2.txt
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_3.txt

But I actually need the tar to only list the file and should look like: when I do tar -tf Directory1.ta
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_1.txt
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_2.txt
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_3.txt

Suggestions are much appreciated.
Your question is not very clear - in fact confusing...

If you are asking for the tar archive to not include the parent directories then ...

Code:
cd Directory1/Sub_Directory_B/Sub_Sub_Directory_C
tar -cvf Directory1.tar *

tar -tf Directory1.tar
File_1.txt
File_2.txt
File_3.txt
On the other hand, from your example you seem to be asking how to not show the parent directories as separate lines when using -tf. That is, you want the archive to preserve the directory structure, but you don't want to see them in the listing.

In that case, I am not sure that you can do it with tar alone - tar is just telling you what is in the archive, and it does include the directories so they get listed.

If that is what you want you might try piping the tar output into grep or sed to strip out the directories...

Code:
tar -cvf Directory1.tar Directory1

tar -tf Directory1.tar | grep -v '/$'
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_1.txt
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_2.txt
Directory1/Sub_Directory_B/Sub_Sub_Directory_C/File_3.txt
If this does not answer your question please be a little more explicit about what you need.
 
Old 01-24-2015, 12:33 AM   #4
Bret W
Member
 
Registered: Jan 2015
Posts: 33

Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
On the other hand, from your example you seem to be asking how to not show the parent directories as separate lines when using -tf. That is, you want the archive to preserve the directory structure, but you don't want to see them in the listing.

In that case, I am not sure that you can do it with tar alone - tar is just telling you what is in the archive, and it does include the directories so they get listed.

If that is what you want you might try piping the tar output into grep or sed to strip out the directories...
OP can pipe tar to awk to show only the files when viewing the tar archive

Code:
tar -tvf file.tar | awk -F\/ '{ print $NF }'
The '/' character is the field separator and $NF prints the last field.

Last edited by Bret W; 01-24-2015 at 01:19 AM.
 
1 members found this post helpful.
Old 01-24-2015, 05:44 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
Tar puts a directory entry in the archive so that it may carry the ownership and permissions/acls/security labels of the directory so that when restored, so can the appropriate ownership, permissions/acls/security labels applied.

Without it, there is a significant loss of security...
 
Old 01-24-2015, 10:29 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,400

Rep: Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486
Quote:
Originally Posted by jpollard View Post
Tar puts a directory entry in the archive so that it may carry the ownership and permissions/acls/security labels of the directory so that when restored, so can the appropriate ownership, permissions/acls/security labels applied.

Without it, there is a significant loss of security...
Unless there is a significant loss of security with the directory permissions in the archive because you are picking up the settings that j.random.user had put on a directory that you have configured correctly. I've seen quite a few systems hurt by that. Not all tar archives come from ${ALL_KNOWING_AND_OMNIPOTENT_DIETY}.
 
Old 01-24-2015, 10:36 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,400

Rep: Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486
Quote:
Originally Posted by veerain View Post
I think for that when preparing a tar archive only files should have been given.
A way to do that is to use find to generate a list that excludes directories:
Code:
find Directory1 ! -type d | tar -cvf Directory1.tar --files-from=-
 
1 members found this post helpful.
Old 01-24-2015, 11:17 AM   #8
babyPen
LQ Newbie
 
Registered: May 2014
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
A way to do that is to use find to generate a list that excludes directories:
Code:
find Directory1 ! -type d | tar -cvf Directory1.tar --files-from=-
This is the approach I have used in past and wanted to know if there is any better way to do it.

Code:
find <directory> -type f | tar -cvzf <Tarfile-name> --files-from=-
tar -tf <Tarfile-name> --> to see what all files got TAR'ed
Question: In the above fashion, If I wish to exclude a set of files how is that I can do, currently I TAR them up and then run a --delete to remove the files. If I have a bunch of files I tend to write a while loop to do that, but is there a way in which I need not pull the files at the first place into the TAR.

Thanks for the post.

Last edited by babyPen; 01-24-2015 at 11:38 AM. Reason: Added Additional information.
 
Old 01-24-2015, 11:20 AM   #9
babyPen
LQ Newbie
 
Registered: May 2014
Posts: 17

Original Poster
Rep: Reputation: Disabled
Question

None to post, the question is asked in the above post, apologize for the confusion.

Last edited by babyPen; 01-24-2015 at 11:37 AM.
 
Old 01-24-2015, 01:03 PM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
Quote:
Originally Posted by rknichols View Post
Unless there is a significant loss of security with the directory permissions in the archive because you are picking up the settings that j.random.user had put on a directory that you have configured correctly. I've seen quite a few systems hurt by that. Not all tar archives come from ${ALL_KNOWING_AND_OMNIPOTENT_DIETY}.
If j.random.user could change the configuration then you didn't have security in the first place.

And if you are accepting archives from elsewhere and restoring them, you still don't have any security.
 
Old 01-24-2015, 01:35 PM   #11
Bret W
Member
 
Registered: Jan 2015
Posts: 33

Rep: Reputation: Disabled
Quote:
Originally Posted by babyPen View Post
Question: In the above fashion, If I wish to exclude a set of files how is that I can do, currently I TAR them up and then run a --delete to remove the files. If I have a bunch of files I tend to write a while loop to do that, but is there a way in which I need not pull the files at the first place into the TAR.

Thanks for the post.
Code:
find <directory> -type f ! -name "*.txt" <Other Options>
In this example, the '!' character will negate or ignore all txt files

Note: If your file extensions have a mix of upper and lowercase letters like txt TXT TxT etc, then use -iname instead.

For multiple patterns to ignore, do this

Code:
find <diectory> -type f ! \( -name "pattern1" -o -name "pattern2" \) <other options>
TAR
Second method to ignore patterns using tar is to use --exclude=PATTERN

Example

Code:
tar -cvf file.tar --exclude="*.txt" <directory>
For multiple patters to ignore, add an additional --exclude switch and so on...

Last edited by Bret W; 01-24-2015 at 02:17 PM.
 
Old 01-24-2015, 02:09 PM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,400

Rep: Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486
Quote:
Originally Posted by jpollard View Post
If j.random.user could change the configuration then you didn't have security in the first place.

And if you are accepting archives from elsewhere and restoring them, you still don't have any security.
The tar process will be running under your ID, not the ID of j.random.user. I agree that restoring foreign tar archives directly into your own directory tree is almost always a bad idea, but there are times when I want to take files from a machine that I control and insert them into a similar, existing directory tree at the destination without altering the established directory permissions at the destination. For that, it is important that no entries for the directories themselves exist in the tar archive.
 
Old 01-24-2015, 03:15 PM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
Quote:
Originally Posted by rknichols View Post
The tar process will be running under your ID, not the ID of j.random.user. I agree that restoring foreign tar archives directly into your own directory tree is almost always a bad idea, but there are times when I want to take files from a machine that I control and insert them into a similar, existing directory tree at the destination without altering the established directory permissions at the destination. For that, it is important that no entries for the directories themselves exist in the tar archive.
I believe that is what these are for:

Code:
       --no-overwrite-dir
              preserve metadata of existing directories
...
       --no-acls
              Don't extract the ACLs from the archive

...
       --no-same-owner
              extract files as yourself (default for ordinary users)

       --no-same-permissions
              apply  the user's umask when extracting permissions from the ar‐
              chive (default for ordinary users)

       --no-selinux
              Don't extract the SELinux context from the archive

       --no-xattrs
              Don't extract the user/root xattrs from the archive

Last edited by jpollard; 01-24-2015 at 03:22 PM.
 
Old 01-25-2015, 03:14 PM   #14
Bret W
Member
 
Registered: Jan 2015
Posts: 33

Rep: Reputation: Disabled
@ babyPen

Have you considered to trying rar? If you rar archive a directory with sub directories, you can still extract only the files and the files within the sub directories in the rar archive.

When you extract a rar archive, and depending on the syntax, it can extract only the files or both files and directories.

For example

Code:
rar e file.rar
This will extract all files and the files in the directories within the rar archive. The directories will not be extracted or created.

Code:
rar x file.rar
This will extract all files and the directories within the rar archive.

In short, use 'e' to extract only the files without directory paths. Use 'x' to extract the files and the directory paths.

To Ignore Certain Files & Folders

To rar archive a directory and ignore txt files, type this:

rar a -r -x*.txt test.rar <directory path>

To ignore a folder

rar a -r -x*folder/* test.rar <directory path>


Note: Sometimes it can be confusing as the rar command will use switches with and without hypens '-'

Last edited by Bret W; 01-25-2015 at 03:27 PM.
 
1 members found this post helpful.
Old 01-25-2015, 08:26 PM   #15
babyPen
LQ Newbie
 
Registered: May 2014
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thumbs up

@bret:

I haven't tried rar, but looks to be helpful. I am not that comfortable with rar yet, will try it.

Thanks all for the inputs, I appreciate each and everyone of you for sharing the knowledge.
 
  


Reply

Tags
linux, tar


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] best tar command for backing up files krwinfield Linux - Newbie 1 06-26-2014 12:32 PM
How to overwrite existing files by using tar command (Remove extra files) Kalibo Linux - Newbie 9 12-26-2013 12:42 AM
RHEL4 backup files using tar command... visitnag Linux - Newbie 3 10-24-2009 04:09 PM
TAR command to unpack files to NTFS part idamien Linux - Newbie 4 05-22-2007 11:50 AM
tar --exclude-from=files command not working cucolin@ Linux - Server 2 04-03-2007 09:33 PM


All times are GMT -5. The time now is 01:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration