LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   New to linux and stuck in the tar (https://www.linuxquestions.org/questions/linux-newbie-8/new-to-linux-and-stuck-in-the-tar-4175579818/)

penquin.toes 05-15-2016 12:05 AM

New to linux and stuck in the tar
 
While in /home/user1, create a new tar file which contains the entire contents of your current directory. Name the new tar file: user1.tar Have the new tar file created in your current directory. When you create the tar file, create it so that the extraction path will be the current directory.

Performing tar -cvf user1.tar /home/user1 yielded results with the 1st immediate line stating tar: Removing leading '/' from memeber names followed by a massive amount of files. I am literally in my 2nd day of linux and not sure if I did this correctly with my command. The manual pages advised of multiple options but I attempted others like z and tvf and other options as well that failed giving errors: Cowardly refusing to create an empty archive

grail 05-15-2016 12:13 AM

Not sure I am following your issue. Are you saying you have created a tar file and it is empty?

penquin.toes 05-15-2016 12:23 AM

Quote:

Originally Posted by grail (Post 5545675)
Not sure I am following your issue. Are you saying you have created a tar file and it is empty?

Hello,

No. I am wondering if the command I performed is correct for the above instruction. A small brown box with the name I assigned showed up in the /home/user1 directory

dunne 05-15-2016 12:56 AM

To see what is in the tar file you created, type:

Code:

tar -tf user1.tar | less
This lists what is in user1.tar, and pipes that output into less so you can page through it.

Code:

man tar
Will tell you more about the tar command.

syg00 05-15-2016 01:12 AM

Quote:

Originally Posted by penquin.toes (Post 5545671)
... followed by a massive amount of files.

This is a result of using "-v"; not necessary IMHO.
As suggested "-t" is handy to see if you actually created anything useful.

Turbocapitalist 05-15-2016 01:37 AM

Quote:

Originally Posted by penquin.toes (Post 5545671)
While in /home/user1, create a new tar file which contains the entire contents of your current directory. Name the new tar file: user1.tar Have the new tar file created in your current directory. When you create the tar file, create it so that the extraction path will be the current directory.

In addition to what dunne wrote, you can take advantage of the advanced features of tar. You can use --exclude to not tar the existing tarball, for example.

Code:

tar cf user1.tar --exclude=user1.tar /home/user1/
tar tf user1.tar

Note that the --exclude has to come after the cf clause.

An even more advanced feature --transform allows you to use a sed-like pattern to find and replace parts of the file name and path.

Code:

tar -c --transform='s#home/user1/#./#' -f user1.tar --exclude=user1.tar /home/user1/;
tar -tf user1.tar

Mentioning sed, though, may be pushing you off the edge into the deep end on your second day because it drags regular expressions into the question. Regular expressions are definitely worth learning and the time invested will pay for itself in short order. The 30-second explanation is s#old#new# substitutes the string 'old' for the string new 'new'

By the way you can compress the tarball with the z, j, or J options.

Code:

tar zcf user1.tar.gz --exclude=user1.tar.gz /home/user1/
tar ztf user1.tar.gz

z is for gzip, it is common. j is for bzip2 and J for xz. All, and more, are in the detailed manual page, which hopefully gets less confusing over time.

Code:

man tar
The manual pages are quite useful references but do vary in quality from program to program. It's useful to pick through them for useful options and ignore the rest until next time.

Turbocapitalist 05-15-2016 01:45 AM

An easier way than the --transform option is to just point tar at the current directory with a dot ( . )

Code:

cd /home/user1/
tar zcf user1.tar.gz --exclude=user1.tar.gz .
tar ztf user1.tar.gz


pan64 05-15-2016 02:37 AM

just a comment: you tried to put the result into tar, because user1.tar is inside /home/user1. That is obviously impossible. As a solution you can specify an exclusion or a much better way (at least for me) to avoid putting the final tar among the source files:
Code:

# for example:
tar -C /home/user1 -czvf /tmp/user1.tgz .
# or
tar -C /home/user1 -cf /some_dir/user1.tar .


Fred Caro 05-16-2016 08:48 PM

-C means change directory but you might need to create one first, as c only creates the file.

hence:


tar -C /home/user1 -cf /some_dir/user1.tar .

Fred.


All times are GMT -5. The time now is 02:28 AM.