I have a process in my Dockerfile
with the follow steps:
- Download the tar file using wget.
- untar the file.
- Perform some operation with the file . In this case, move content to another place.
The follow code works until ffmpeg
version still 3.4.1.
So, today the available version for ffmpeg
is 3.4.2 and the code is not working.
I want to find a way to grab back the filename after the untar process and pass it to the next steps
RUN wget http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz
RUN tar xf ffmpeg-release-64bit-static.tar.xz
RUN mv ffmpeg-3.4.1-64bit-static/ffmpeg /usr/local/bin/
RUN mv ffmpeg-3.4.1-64bit-static/ffprobe /usr/local/bin/
It could be something like this:
RUN DIRNAME = tar xf ffmpeg-release-64bit-static.tar.xz
RUN mv $DIRNAME/ffmpeg /usr/local/bin/
RUN mv $DIRNAME/ffprobe /usr/local/bin/
How could I achieve this?
Don't. Instead, get tar to put the contents in a directory of your choosing.
tar
can remove n components of the path of each file (--strip-components
)tar
can apply arbitrary sed expressions to change the path of each file (--transform
)So I'd do something like:
Here, I made the directory myself, and told tar to extract there (the
-C
option), while removing the first component from the path of the extracted files. Soffmpeg-3.4.1-64bit-static/ffprobe
becomesffprobe
inside the directoryffmpeg
.Or:
Here, I simply changed the first component of the path to
ffmpeg
, so I didn't need to make the directory myself. So,ffmpeg-3.4.1-64bit-static/ffmpeg
becomesffmpeg/ffmpeg
.Or, knowing that you specifically want to dump those particular files in
/usr/local/bin
, use-C
to directly place the files there, while removing all directory components and selecting exactly those files you want to extract:With any tar, you can specify the files to be extracted. With GNU tar, you can also use wildcards.
The command to get the directory name from the
tar.xz
file is:You'll need to:
Explanation how to get the directory name from a
tar
file:Tar parameters:
head -1
- prints the file lineawk '{print $6}'
- print the 6th elements which is the directory nameExamples:
tar.xz
example:tar tJvf coreutils-8.22.tar.xz | head -1 | awk '{print $6}'
Results with:
tar.bz2
example forffmpeg-3.4.2.tar.bz2
tar tjvf ffmpeg-3.4.2.tar.bz2 | head -1 | awk '{print $6}'
Results with:
Like that:
Notice that
GNU tar
does not require-j
,-z
etc., it can detect archive type automatically.