0

I have over a thousand compressed files inside each other, named in descending order starting at 1022; and need to extract all of them. What I'm trying to do is to write a shell script that will do just that. Here's what I've got so far:

cd "PATH"

for file in PATH/*; do
    filename=${PWD: -4}

    extract $file

    numb=$(echo $filename - 1 | bc)
    echo $numb
    cd "$numb"
done

The "extract" function extracts any format of compression.

The problem is the loop doesn't seem to go on inside the new directory. Can anyone help me out with this?

mr4s
  • 19
  • 2
  • Why do you think `cd` isn't working? Note that `PATH/*` is expanded *once*, at the very beginning of the loop, not after each iteration of the body. – chepner Jun 05 '17 at 00:23
  • This might help: [How to debug a bash script?](http://unix.stackexchange.com/q/155551/74329) – Cyrus Jun 05 '17 at 00:36
  • If you are in directory `1022` you can‘t change in directory `1023` with a simple `cd 1023`. You need something like this: `cd ../1023`. – Cyrus Jun 05 '17 at 00:41
  • Your `filename` is probably wrong. Perhaps you can echo it to see what is wrong with it. Also see [How do I use a for-each loop to iterate over file paths output by the find utility in the shell / Bash?](https://stackoverflow.com/q/15065010/608639), [How to loop through file names returned by find?](https://stackoverflow.com/q/9612090/608639), [Loop over file names from `find`?](https://stackoverflow.com/q/9391003/608639), [Iterate over list of files with spaces](https://stackoverflow.com/q/7039130/608639), [for vs find in Bash](https://unix.stackexchange.com/q/97084/56041), etc. – jww Jun 05 '17 at 01:02
  • Without knowing specifically what your directory structure is -- this is a shot in the dark, but you might be able to accomplish your goal with a simple `find` one-liner: `cd PATH;find . -type f -exec gunzip {f} \;` which will decompress every file in PATH using `gunzip`. Season to taste. If that doesn't work, you'll need to provide more information about what you're trying to solve. – Jim Jun 05 '17 at 05:07
  • @Cyrus : While you are right with this, this CD would produce an error message on stderr. However, the OP never mentioned an error message. He just said that there is no change in directory. I therefore wonder whether maybe the loop is ever entered. The loop would be skipped if the wildcard pattern `PATH/*` does not match anything **and** bash is configured for *nullglob*. – user1934428 Jun 05 '17 at 07:01
  • It doesn't produce any messages, it simply extracts the one file in my current directory. Therefore, I'd have to run the code for every single directory, which is not at all what I want. I want it to extract the file and cd into the created subdirectory until there are no more compressed files – mr4s Jun 05 '17 at 17:18

0 Answers0