Change directory after unziping

186 views Asked by At

I am making a script that allow's me to unzip a given file. My problem is that i don't now how to change directory to the directory just created by the unzip process. I tried with this command, but it's not working: SITE_DIRECTORY="$(ls -dt */ | head -1)"

Any idea on how to get the name of the directory just extracted ?

Edit: Now i got to SITE_DIRECTORY=unzip $SITE_NAME | grep 'creating:' | head -1 | cut -d' ' -f5-

But a new problem arise: the unzip command does not extract all the files. New ideas ?

2

There are 2 answers

1
sebastian.roibu On BEST ANSWER

The solution to my problem was the following commands:

unzip $SITE_NAME >output.txt
SITE_DIRECTORY=$(cat output.txt | grep -m1 'creating:' |  cut -d' ' -f5-)
rm output.txt

Thanks goes to Evan @ Unzip File which directory was created

2
sitilge On

If the directory is known, you could

unzip -j yourzip.zip -d /path/to/dir && cd /path/to/dir

Extra info from man page (j option)

-j junk paths. The archive's directory structure is not recreated; all files are deposited in the extraction directory (by default, the current one).