Question

i have this little code here

     for file in *.tar.gz;
     do tar xzvf "${file}" && rm "${file}";
     done

It extracts a tar.gz and deletes it. Now I have to create a filelist file (.fl) named like a substring from the .tar . For example, I have to delete the first 5 letters and the last 5 (the extension) from the name of the .tar.gz . And that for every .tar.gz that I extract.

Example:

  • I have a ABC_A.tar.gz with a ABC_A.xml in it.
  • I have to make a A.fl and in that A.fl i have to write ABC_A.xml

Thanks in advance.

Was it helpful?

Solution

In your loop, you can do the following for each file:

# delete first five characters
name=${file:5}

# delete .tar.gz suffix
name=${file%%.tar.gz}

OTHER TIPS

Use

for file in *.tar.gz
do
    # this creates the file list into a .fl file:
    tar tfz "${file}" > "${file:5:-5}.fl"
    # this extracts and afterwards removes the tar archive:
    tar xzvf "${file}" && rm "${file}"
done

You also can combine the two in one step:

for file in *.tar.gz
do
    tar xzvf "${file}" > "${file:5:-5}.fl" && rm "${file}"
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top