Home > OS >  Looping over the last subdirectory to zip in linux terminal
Looping over the last subdirectory to zip in linux terminal

Time:10-12

given the following structure:

.
├── a1
│   ├── a11
│   ├── a12
│   └── a13
├── a2
│   ├── a21
│   ├── a22
│   └── a23
├── b1
│   ├── b11
│   ├── b12
│   ├── b13
│   └── b14
├── b2
│   ├── b21
│   └── b22

How do I find & zip the last subdirectory in specific directories?

  1. The directory starts with 'b' (ex) 'b1', 'b2'
  2. Find the last subdirectory and zip it (ex) 'b14', 'b22'

I want something like:

FILES=./b*
for f in $FILES;
do
  FILELIST=ls ./$f;
  LASTFOLDER = "FIND THE LAST SUBDIRECTORY";
  tar zcvf LASTFOLDER.tar.gz ./$f/LASTFOLDER/;
done

The output has to be:

./b1/b14.tar.gz
./b2/b22.tar.gz

CodePudding user response:

Pipe to tail -1 to get the last line.

lastfolder=$(ls ./$f | tail -1)
  • Related