Search code examples
stringshellunixsubstringfilelist

Extracting a .tar file and creating a filelist


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.


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}