Why does Colab extract a Google Drive .tar file to /content directory (and not to Google Drive)?

Viewed 109

I use Colab. I am amazed by the following behavior:

drive.mount('/content/drive')
os.listdir("/content/drive/MyDrive/Colab Notebooks")

or

!ls -al /content/drive/MyDrive/Colab\ Notebooks

returns: ['FGNET_all.tar']

When I extract FGNET_all.tar

!tar xvf /content/drive/MyDrive/Colab\ Notebooks/FGNET_all.tar

FGNET is extracted to /content directory:

os.listdir("/content/drive/MyDrive/Colab Notebooks")

=> Only ['FGNET_all.tar'] not FGNET directory

os.listdir("/content")

or

!ls -al /content

returns ['.config', 'drive', '._FGNET', 'FGNET'] => FGNET directory

Why?

1 Answers

Unless the destination is specified, tar command extracts contents of the file to the working directory. Default working directory of Google Colab is the /content folder, which is why your extracted file is saved there.

You can specify the destination directory as follows:

!tar xvf /content/drive/MyDrive/Colab\ Notebooks/FGNET_all.tar -C /content/drive/MyDrive/Colab\ Notebooks/
Related