All commands tested on Mac OS Monterey / Jul 02, 2024

How to extract a single file from a tar archive

Extract my-file.txt from the files.tar archive.

You can use this short tar command.

              
                  tar -xvf files.tar files/my-file.txt
              
          

Or this more verbose tar command.

              
                  tar --extract --verbose --file files.tar files/my-file.txt
              
          

Ensure you provide the full path to the file you need to extract. Ie. directory/directory/directory/my-file.txt. Tar won't know which file to extract if you don't.

Note: The required directories will be extracted also.


Options explained

  1. -x or --extract tells tar to extract an existing archive
  2. v or --verbosetells tar to produce verbose output
  3. f or --filetells tar that the next parameter will be the tar file to create, extract, append to, list, etc.

More commands for extracting from tar archives