in shell script,how to get file name after untar the file - shell

i am having tar file "test.tar.gz'.now i want to get the name "test' after untar the "test.tar.gz'. In shell script ,how to get this.

To strip an extension from a file name, use basename:
basename test.tar.gz .tar.gz
prints test
But that is just the base name of the archive. It's not always the name of any file or directory which the tar creates. The tar archive can contain any file names. If you need those, use tar tf to list the content of the archive.

Use something like:
file="test.tar.gz"
tarfilename=${file%.tar.gz}
read up about it man bash search for variable expansion.

Related

Get specific file from .tar file within a Bat file

I want to extract a specific file named WholeImage.jpg from a .tar file in Windows.
I got it working in one specific case, in which I specify the exact filelocation in the .tar file:
tar -xf %~1 --strip-components 5 InspectionProjectList/InspectionProject0718/InspectionProgram001/Sensing/Board0003/WholeImage.jpg
The --strip-components 5 is to get rid of the unnecessary folders while extracting the wanted file.
However the filepath for the WholeImage.jpg changes from tar file to tar file.
The parts that change are noted here witd a dollar sign:
InspectionProjectList/InspectionProject$$$$/InspectionProgram$$$/Sensing/Board$$$$/WholeImage.jpg

Compress full directory with bzip2 into a tar file whose name depends on the current date

I'm trying to compress a directory but I want to change the tar file name to have the current date. The problem is that tar doesn't accept:
#!/bin/bash
tar -cvjSf $(date +%d/%m/%y.%HH:%MM)home_backup.tar.bz2 /home
I want to make a compressed File with bzip2 with the actual date but the name is not accepted. It only works if I use a simple name like:
#!/bin/bash
tar -cvjSf home.tar.bz2 /home
Don't put : or / in the name of the tar file.
Try:
tar -cvjSf "$(date +%d-%m-%y.%HH.%MM)home_backup.tar.bz2" /home
Notes:
In Unix, / means directory. The expansion of $(date +%d/%m/%y.%HH:%MM)home_backup.tar.bz2 will contain two / and tar would want to create the file in specified subdirectory. In the command above, we replaced / with - and the problem is avoided.
tar treats the part of a file name that precedes : as the name of a remote host. Since you are not trying to send the file to a remote host, all : should be removed from the date command that is used to create the file name. In the command above, we replaced : with . and the problem is avoided.
The command above shows the name of the tar file inside double-quotes. With the specific command shown above, this is not necessary. The use of double-quotes, however, prevents word-splitting and this may save you from unpleasant surprises in the future.

tar extracted archive removes version

I have a zipped archive version 0.0.1: myarch_0.0.1.tar.gz
When I extract it with tar, everything is unzipped and extracted in a myarch folder, stripping the version number.
ls
myarch_0.0.1.tar.gz
tar -zxvf myarch_0.0.1.tar.gz
ls
myarch/ myarch_0.0.1.tar.gz*
I want the extracted folder to be named: myarch_0.0.1/
How do I keep my version number stuck to the extracted folder name?
The name of an archive file, and the name of the files inside, have nothing to do with each other in general. If you want extracted directories to have a certain name, with a version number, then you have to create the archive with so named directories.
In this example, the extracted content is a directory named myarch, instead of your desired myarch_0.0.1. You can rename the directory and recreate the archive:
mv myarch myarch_0.0.1
tar zcf myarch_0.0.1.tar.gz myarch_0.0.1
That's it. When you untar this new archive, you will get a directory named myarch_0.0.1, simply because that's what you put inside. Even if you rename this file to mickeymouse.tar.gz, when you untar it, you will still get a directory named myarch_0.0.1, simply because that's what's inside the archive. Nothing to do with the filename of the archive.

Shell command to rename files with ? inside name

Environment: CentOS, shell
I have a few files under a directory with the names like
B?orn.txt
F?ord.xml
etc
How to find/rename all files under the directory containing ? and delete this ? in the filenames to make all them like:
Born.txt
Ford.xml
etc
If you have the rename utillity installed, just use that command:
$ rename -n 's/\?//' dir/*
dir/f?ile renamed as dir/file
dir/f?ile2 renamed as dir/file2
dir/f?ile3 renamed as dir/file3
The -n flag is to check what the utillity would do, remove it to do the rename.

How do I Unzip a file, replace a file with a new copy and re-zip the file in Apple script?

I have a string with the path to a .ipa file:
set ipa_path to POSIX path of ipa_file
Now I want to:
Un-zip the .ipa file (its really a zip file)
Replace a file in the zip called "embedded.mobileprovision" with a new version of the file.
Re-zip the file and replace the original ipa file.
So for I have:
do shell script "unzip " & ipa_path
Is this right so far? I just started to learn AppleScript today ...
You don't really need AppleScript for this. The command line zip utility has the -r option to replace existing files (of the same name and relative path) within a zip archive. Here's a quote from the man page man zip:
if foo.zip exists and contains foo/file1 and foo/file2, and the
directory foo contains the files foo/file1 and foo/file3, then:
zip -r foo.zip foo
will replace foo/file1 in foo.zip and add foo/file3 to foo.zip.
Of course, you can still wrap the calls to zip in AppleScript do shell script commands.

Resources