Files with Hyphen at Beginning of Name - linux

I have a bunch of files such as this --4gqARaEJE_0.000.wav.gz. How do I decompress them? Executing tar -xz --4gqARaEJE_0.000.wav.gz returns the error tar: unrecognized option '--4gqARaEJE_0.000.wav.gz'. Gzip returns the same error. I take it the hyphens are causing the error, but don't know the workaround.
I tried changing the file names to remove the hyphens, but that gives the same error. I also tried \ prior to the file name, but no luck there either. How do you handle this kind of thing?

I've tried a small example on my server.
With wget https://sourceforge.net/projects/od1n/files/samples.tar.gz/download I've downloaded a random sample file and saved it. File name was download initially.
I tried this:
root#server [~/stackoverflow]# mv download --download.tar.gz
mv: unrecognized option '--download.tar.gz'
Try `mv --help' for more information.
As expected, it didn't work but adding a -- did help.
root#server [~/stackoverflow]# mv -- download --download.tar.gz
root#server [~/stackoverflow]# ls
./ ../ --download.tar.gz
Now our file name is --download.tar.gz, to extract it I used following command.
tar -xvzf -- --download.tar.gz
This still fails.
tar: unrecognized option '--download.tar.gz'
However specifiying folder location did work as intended.
root#server [~/stackoverflow]# tar -xvzf ./--download.tar.gz
samples/
samples/modifiedSheppLogan_256x256.smp
samples/fmri_64x64.smp
samples/qualityPhantom_64x64.smp
samples/brain_64x64.smp
samples/brain_64x64x5.smp
samples/qualityPhantom_64x64x5.smp
samples/modifiedSheppLogan_64x64.smp
samples/brain_256x256.smp
samples/qualityPhantom_256x256.smp
samples/brain_128x128x5.smp
samples/fmri_128x128.smp
samples/modifiedSheppLogan_128x128.smp
samples/qualityPhantom_128x128x5.smp
samples/brain_256x256x5.smp
samples/point_spread_function.smp
samples/qualityPhantom_256x256x5.smp
samples/brain_128x128.smp
samples/qualityPhantom_128x128.smp
samples/frequency_dist.smp
root#server [~/stackoverflow]# ls
./ ../ --download.tar.gz samples/
In your case file might've been corrupted or the extension could be wrong.

Related

Structure of .tar files

I am trying to run some benchmarks which take in input a tar file.
Now there is a runme.sh inside the tar file and that needs to be modified and the folder has to be made a .tar again.
The original benchmark works, but the modifies one doesn't. I believe that it is a problem with the file format.
Note : My modification is not creating the problem. If I just uncompress the working tar and tar it again without modification, it does not work. Surprisingly the size of the new file changes.
What I tried :
file command on the working and non-working tar files.
Both returned same POSIX tar archive
Tried to run the command tar cvf folder_name.tar folder_name/
Does not work.
What works :
I am on Ubuntu (14.04) and I double clicked on the tar, directly edited the file I wanted and updated it. This works, but is not a feasible solution as I have a large number of files and I want to write a script to automate it.
Screenshot of how it works with GUI :
Does the original tar file include the top-level directory name? It doesn't look like it from your screenshot. If you re-create the tar file with a top level directory, as indicated by point 2 in the things you tried, the structure won't be the same, and whatever program is trying to consume the tar file won't be able to parse it.
How do you test "If I just uncompress the working tar and tar it again without modification, it does not work." In a GUI or in a shell? If in a shell - what exact commands do you use?
In a shell, you can get the contents of the tarball with the command tar -tf filename.tar. If all the files it lists starts with the same folder name, your tarball includes a top level directory. If it just lists various files and subdirectories, it doesn't. (Tarballs that don't are an abomination, but if whatever you are using them for requires it, you'll just have to cope.)
I'm guessing that if you do this on your original tar file and your modified, non-working tar file, the results will differ.
The following should work in a shell if you have/need a tarball without a toplevel directory:
$ mkdir workdir
$ cd workdir
$ tar -xf ../tarball.tar
<edit your file however you like>
$ tar -cf ../tarball-new.tar *
$ cd ..
$ rm -r workdir
In case you have/need a tarball with a toplevel directory, the following should suffice:
$ tar -xf ../tarball.tar
$ cd toplevel_directory
<edit your file however you like>
$ cd ..
$ tar -cf tarball-new.tar toplevel_directory
$ rm -r toplevel_directory
Edit: I'm glad it worked for you. The point is, of course, that tar includes the paths of the files it stores, not just the filenames. So if you need a flat list of files, you need to run tar in the directory containing those files, giving all of them as arguments to tar. If you try to take the shortcut of going up a level and only specifying the directory name to pack up, tar will include the directory name in the archive.

How tar xvfz works in cygwin?

I tried tar with xvfz and -xvfc both didn't work in Cygwin on Windows.
$ tar xvfz sshpass-1.0.5.tar.gz
tar (child): sshpass-1.0.5.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
Here cmd tar with '-'
$ tar -xvfz sshpass-1.0.5.tar.gz
tar: z: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
Question :
1 . How does tar works with - or without _ ?
Please suggest to execute tar -xvfz sshpass-1.0.5.tar.gz.
tar has 3 types of syntax (according to this ):
long options (--file)
short options (-f)
old options (f)
For the old option syntax, all the letters must follow "tar" and must all fall into a single clump with no spaces. The order of the letters doesn't really matter, so long as the arguments to those letters follow the same order after the clump of options.
This old way of writing tar options can surprise even experienced users. For example, the two commands:
# tar cfz sshpass-1.0.5.tar.gz file
# tar -cfz sshpass-1.0.5.tar.gz file
are quite different. The first example uses ‘sshpass-1.0.5.tar.gz’ as the value for option ‘f’ and recognizes the option ‘z’. The second example, however, uses ‘z’ as the value for option ‘f’ — probably not what was intended.
Old options are kept for compatibility with old versions of tar.
The command with a '-' is equivalent to
tar -czf sshpass-1.0.5.tar.gz file
tar -cf sshpass-1.0.5.tar.gz -z file
tar cf sshpass-1.0.5.tar.gz -z file
Got it, there was a typo error for in file name, corrected and ran. it worked.
$ tar xvfz sshpass-1.05.tar.gz
sshpass-1.05/
sshpass-1.05/COPYING
sshpass-1.05/AUTHORS
sshpass-1.05/aclocal.m4
sshpass-1.05/INSTALL
sshpass-1.05/Makefile.am
sshpass-1.05/configure.ac
sshpass-1.05/install-sh
sshpass-1.05/Makefile.in
sshpass-1.05/sshpass.1
sshpass-1.05/configure
sshpass-1.05/depcomp
sshpass-1.05/README
sshpass-1.05/NEWS
sshpass-1.05/config.h.in
sshpass-1.05/missing
sshpass-1.05/main.c
sshpass-1.05/ChangeLog

Need help on the tar command

Have got a problem executing the command as below:
tar -xvf arch.tar.gz -s '/^bundle//'
Could be the
-s '/^bundle//'
is a problem as I've got errors like:
$ tar -xvf arch.tar.gz -s '/^bundle//'
tar: /^bundle: Not found in archive
tar: Exiting with failure status due to previous errors
The tried to run the command under Cygwin/Win10.
It's part of the longer script but I'm not sure what was the idea of original author. Archive does include the 'bundle' folder inside... and it's the only first level file there.
Thank you in advance :)
-s does not mean to do a substitution, which seems to be how you're trying to use it. You probably want --xform='s/^bundle//'
-s has the following entry in the help listing:
-s, --preserve-order, --same-order
member arguments are listed in the same order as
the files in the archive
With your code it's actually trying to find a file with the name /^bundle// which does not exist, even if bundle does. Also, the --xform option I gave will rewrite the names of files to strip the string bundle from the front. If you are just trying to not extract the file bundle you would want the flag --exclude='bundle'
In this case, if bundle is a top level directory in the archive, and it's the only one, you could also use the flag --strip-components=1, though this would get rid of all the top level directories, so might not be exactly what you want depending on your archive
Thanks all,
Problem solved other, then 'tar', way but for those who may be interested here is the answer I have found on the web:
If you are developing on Linux, or using GNU tar, this command should work:
tar -xvf arch.tar.gz --transform 's|^bundle/||'
For Mac or BSD-based operating systems:
tar -xvf arch.tar.gz -s '/^bundle//'
Yes, the idea was to remove the /bundle/ folder from files paths.

Is it possible to create a folder with the filename into the tar file you are creating?

Let's say I'm trying to tar.gz all the files and folders in /usr/local/bin/data/*
The file name would be data-2015-10-01.tar.gz. When I untar it, is it possible that the root directory would be data-2015-10-01 followed by the contents of whatever is inside of data/* ?
If not, how can I tar /usr/local/bin/data/* but start at the /data/ folder level?
I can't do this unfortunately since the program spits out /usr/local/bin/data/ and I'm unable to change it.
cd /usr/local/bin
tar ... /data/*
There are a couple of ways to do what I think you're trying to accomplish. First, you can use the -C option to tar when creating the archive. That changes tar's current working directory to that directory before creating the archive. Not strictly required in your case, but probably helpful.
# tar -C /usr/local/bin -czf data-2015-10-01.tar.gz data/*
That at least gets you to a single directory named data. If you have control of the extraction (manually or via a script you provide to whomever is unpacking this), then you can do something like this on the extraction:
# mkdir -f data-2015-10-01 && tar -C data-2015-10-01 --strip-components=1 -xzf data-2015-10-01.tar.gz
This will remove the first path, which is "data" and extract everything from there into the directory which is your current working directory, data-2015-10-01. So, it isn't specifically tar that's doing the renaming, but you will effectively end up with the same result.
I've accomplished something similar with a symlink. This is not a great solution if you have (or might have) symlinks in the directory structure you're trying to archive. I have to say that I prefer #geis' solution to strip out the top-level directory on extract, but this gives you another option.
ln -s /usr/local/bin/data data-2015-10-01
tar -cvhf data-2015-10-01.tar.gz data-2015-10-01/
rm data-2015-10-01
(Note the additional -h option in the tar invocation.)

Script don't extract tar

I have this script shell, it extract and install libpcap library
#!/bin/sh
PATH=/usr/src
wget=/usr/bin/wget
tar=/bin/tar
echo "###############"
echo "INSTALL LIBPCAP"
echo "###############"
$tar -xvf libpcap-1.3.0.tar.gz
cd libpcap-1.3.0
./configure --prefix=/usr
make && make install
When I execute it , I have this error
tar (child): gzip: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
/bin/tar: Child returned status 2
/bin/tar: Error is not recoverable: exiting now
./install.sh: 14: cd: can't cd to libpcap-1.3.0
You've changed PATH to /usr/src so when tar tries to exec gzip, it cannot find it because it only looks in /usr/src. You'll need to add the location of gzip to PATH (and the location of every tool that the configure script is going to call, as well as make), or call it explicitly instead of letting tar call it, or (best choice), don't modify PATH. If you insist on changing PATH, try PATH=/usr/src:/usr/sbin:/usr/bin:/sbin:/bin or PATH=/usr/src:$PATH, but really it's best to leave it alone and really odd to put a directory named src into it.
Incase if you want /usr/src added to PATH variable then the better approach will be as
PATH="$PATH:/usr/src"
To make your script run from any directory path use a separate variable which holds the absolute path of the source file path like file_path variable which is used in the below script.
#!/bin/sh
PATH="$PATH:/usr/src"
wget=/usr/bin/wget
tar=/bin/tar
file_path="/path/to/source-files"
echo "###############"
echo "INSTALL LIBPCAP"
echo "###############"
$tar -xvf $file_path/libpcap-1.3.0.tar.gz
cd $file_path/libpcap-1.3.0
./configure --prefix=/usr
make && make install
When dealing with gzipped tar, you should explicitly pass the option -z, so your code should be something like:
$tar -xzvf libpcap-1.3.0.tar.gz

Resources