How to extract a .tar.gz file on UNIX - unix

I have a file (reviews_dataset.tar.gz) that contains many files which contains data. I am required to extract the files in this archive and then perform some basic commands on the file. So far I have created a directory named (CW) and found a command tar zxvf fileNameHere.tgz but when I run this it of course cannot find my file as I have not "downloaded it" into my directory yet? How do I get this file into my directory so that I can then extract it? Sorry if this is poorly worded I am extremely new to this.

You must either run the command from the directory your file exists in, or provide a relative or absolute path to the file. Let's do the latter:
cd /home/jsmith
mkdir cw
cd cw
tar zxvf /home/jsmith/Downloads/fileNameHere.tgz

You should use the command with the options preceded by dash like this:
tar -zxvf filename.tar.gz
If you want to specify the directory to save all the files use -C:
tar -zxf filename.tar.gz -C /root/Desktop/folder

Related

Change last modified time of untarred files

I am using below command to untar
tar -xvf <filename.tar>
I want to make sure extracted files' modified time is updated to current time.
GNU tar has an option for this:
-m, --touch
don't extract file modified time
With this option, extracted files have the time of extraction as their last-modified.
Anyways, I found an answer. I looped through all the files in the tar and used touch command to change the modified time.
tar -xvf <filename.tar>
for f in $(tar -tf <filename>.tar); do touch $f;done

Extract tar file without creating folder

I want to extract tar file in Unix tar xvf /home/test/group.tar and once extracted got a folder group which has list of xls,pdf,txt files.
How can i extract contents of group.tar inside /home/test/list of xls, pdf files without creating group folder.
Any specific command available or have to follow with copy and move??
Thanks!
You may use the --strip-components 1 parameter.
tar xvf group.tar --strip-components 1

What is the differences between real so file and ln -s

for example, my directory like this:
lib
|
+--foo.so
+--bar.so -> bar.so.1.0.0.0
+--bar.so.1.0.0.0
Are these both ways always same?
The reason I ask this question is that I found unix will copy to real so file when I
cp -r lib /path/to/
new directory like this:
/path/to/lib
|
+--foo.so
+--bar.so
+--bar.so.1.0.0.0
The difference between so and ls -s is the difference between a file and a symbolic link. Symbolic links are like aliases to other files and operations on them result in changes in the linked files. When you do cp, it copies the linked file to the target directory with the link name as the file name, i.e., it reads the linked file when it opens the symbolic link to copy it. So lose the link and instead get a copy of the linked file. If you use -P option of the cp command you can preserve the symbolic link information.
cp -P lib /path/to/

how to undo tar operation?

I used tar -cvf sample_directory/* and didn't specify file.tar.gz. So the Makefile within the folder is in some unreadable format. is there a way to recover my Makefile?
The Makefile within the folder contains the output from the tar command, so it's not "some unreadable format", it's gzipped tar format. that tar archive won't contain your missing Makefile though.
The comments about recovering the Makefile from your backups or from your version control system are apt. This is in fact what you need to do.
If you don't have a backup or the Makefile wasn't checked in to a version control system, then there isn't a feasible way to recover its contents.
Aside from the issue of your poor lost Makefile, a piece of advice about using tar: never tar up a bunch of individual files inside a directory. Always tar up the directory itself instead. There is not much more annoying than untarring an archive that contains a big bunch or files instead of a single directory (which then contains files). Doing that makes a mess by littering files all over the directory that happens to be the current directory. Please be nice to whoever is going to extract your tar files (which might be yourself, later on!), follow convention, and tar up complete directories.
tar -czf file.tar.gz sample_directory
As a bonus, if you do it that way, and you forget the output filename like this:
tar -czf sample_directory
You won't squash anything, you'll just get an error.

Zip command without including the compressed dir itself

Suppose the structure:
/foo/bar/
--file1
--file2
--file3
--folder1
--file4
--folder2
--file5
I want to run the unix zip utility, compressing the bar folder and all of it's files and subfolders, from foo folder, but not have the bar folder inside the zip, using only command line.
If I try to use the -j argument, it doesn't create the bar folder inside the zip as I want, but doesn't create folder1 and folder2. Doing -rj doesn't work.
(I know I can enter inside bar and do zip -r bar.zip . I want to know if it's possible to accomplish what $/foo/bar/ zip -r bar.zip . but doing it from $/foo).
You have to do cd /foo/bar then zip -r bar.zip ., however, you can group them with parentheses to run in a subshell:
# instead of: cd /foo/bar; zip -r bar.zip; cd -
( cd /foo/bar; zip -r bar.zip . )
The enclosed (paren-grouped) commands are run in a subshell and cd within it won't affect the outer shell session.
See sh manual.
Compound Commands
A compound command is one of the following:
(list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below).
Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes.
The return status is the exit status of list.
zip doesn't have a -C (change directory) command like tar does
you can do:
cd folder1 && zip -r ../bar.zip *
from within a command line shell
or you can use bsdtar which is a version of tar from libarchive that can create zips
bsdtar cf bar.zip --format zip -C folder1 .
(this creates a folder called ./ -- not sure a way around that)
I can't speak for the OP's reasoning. I was looking for this solution as well.
I am in the middle of coding a program that creates an .ods by building the internal xml files and zipping them together. They must be in the root dir of the archive, or you get an error when you try and run OOo.
I'm sure there is a dozen other ways to do this:
create a blank .ods file in OOo named blank.ods, extract to dir named blank, then try running:
cd blank && zip -r ../blank.ods *
The way I wrote mine, the shell closes after one command, so I don't need to navigate back to the original directory, if you do simply add && cd .. to the command line:
cd blank && zip -r ../blank.ods * && cd ..

Resources