Dockerfile: COPY / ADD with space character in path (Windows) - windows

In Windows 10 (cmd) I'm trying to copy a file from a subfolder containing a space character in its name.
First I tried to use quotation marks:
FROM jfloff/alpine-python:2.7
COPY "Folder 1/File.txt" "Dir 1"
Error message:
failed to process "\"Folder": unexpected end of statement while looking for matching double-quote
"JSON" format (skipped the first line):
COPY ["Folder 1/File.txt" "Dir 1"]
Error message:
failed to process "[\"Folder": unexpected end of statement while looking for matching double-quote
Trying to escape with a single backslash:
COPY "Folder\ 1/File.txt" "Dir\ 1"
Error message:
failed to process "\"Folder\\": unexpected end of statement while looking for matching double-quote
Trying to escape with a double backslash:
COPY "Folder\\ 1/File.txt" "Dir\\ 1"
Error message:
failed to process "\"Folder\\\\": unexpected end of statement while looking for matching double-quote
Also tried a suggestion to use %20 instead of space:
COPY ["Folder%201/File.txt" "Dir%201"]
Error message:
COPY failed: no source File
Escape character replacement:
# escape=`
COPY "Folder` 1/File.txt" "Dir 1"
Error message:
failed to process "\"Folder`": unexpected end of statement while looking for matching double-quote
The same, but without quotes:
#escape=`
COPY Folder` 1/File.txt Dir` 1
Error message:
COPY failed: stat /var/lib/docker/tmp/docker-builder082039614/Folder: no such file or directory
Method with packing / unpacking using a tar archive (I'm not happy with that idea).
It should be possible, shouldn't it?

Maybe you can use ARG to help you, like this:
Dockerfile:
FROM jfloff/alpine-python:2.7
ARG src="Folder 1/File.txt"
ARG target="Dir 1/"
COPY ${src} ${target}
BTW, a / has to be add at the end of Dir 1 if you treat really want to treat it as a folder.
And, JSON format is also ok, just you miss ,, it should be:
FROM jfloff/alpine-python:2.7
COPY ["Folder 1/File.txt", "Dir 1/"]
Update for your comments:
In official guide, it said:
When copying files or directories that contain special characters (such as [ and ]), you need to escape those paths following the Golang rules to prevent them from being treated as a matching pattern.
So, for your case, it should be:
FROM jfloff/alpine-python:2.7
ARG src="[[]Folder 1]/__SLIM_TEMPLATE.mm"
ARG target="[Folder 1]/"
COPY ${src} ${target}
Or:
FROM jfloff/alpine-python:2.7
COPY ["[[]Folder 1]/__SLIM_TEMPLATE.mm", "[Folder 1]/"]

agree it's annoying to get files from docker that have special characters,
had similar experience and after hours of try and errors, I came up with this simple solution that I now use since then.
tar/zip the files and then "docker cp" them easily without having to worry about dozens of []'"..
tar files in your container machine:
#zipped
tar czf myfiles.tar.gz file1.txt dir24 file_xyz
#or unzipped
tar -cvf myfiles.tar file1.txt dir24 file_xyz
Then copy them to local directories in your windows machine.
open cmd in windows
run
# docker cp ABCD_CONTAINER_NAME:PATH/myfiles.tar.gz d:/TARGETDIR/
check if files have been transferred in d:/TARGETDIR/

The solution is the follow:
Docker has updated it's COPY command. So you can use brackets and your filenames can have whitespaces. Read more here (official docs)

Related

Add part of filename as PDF metadata using bash script and exiftool

I have about 600 books in PDF format where the filename is in the format:
AuthorForename AuthorSurname - Title (Date).pdf
For example:
Foo Z. Bar - Writing Scripts for Idiots (2017)
Bar Foo - Fun with PDFs (2016)
The metadata is unfortunately missing for pretty much all of them so when I import them into Calibre the Author field is blank.
I'm trying to write a script that will take everything that appears before the '-', removes the trailing space, and then adds it as the author in the PDF metadata using exiftool.
So far I have the following:
for i in "*.pdf";
do exiftool -author=$(echo $i | sed 's/-.*//' | sed 's/[ \t]*$//') "$i";
done
When trying to run it, however, the following is returned:
Error: File not found - Z.
Error: File not found - Bar
Error: File not found - *.pdf
0 image files updated
3 files weren't updated due to errors
What about the -author= phrase is breaking here? Please could someone enlighten me?
You don't need to script this. In fact, doing so will be much slower than letting exiftool do it by itself as you would require exiftool to startup once for every file.
Try this
exiftool -ext pdf '-author<${filename;s/\s+-.*//}' /path/to/target/directory
Breakdown:
-ext pdf process only PDF files
-author the tag to copy to
< The copy from another tag option. In this case, the filename will be treated as a pseudo-tag
${filename;s/\s+-.*//} Copying from the filename, but first performing a regex on it. In this case, looking for 1 or more spaces, a dash, and the rest of the name and removing it.
Add -r if you want to recurse into subdirectories. Add -overwrite_original to avoid making backupfiles with _original added to the filename.
The error with your first command was that the value you wanted to assign had spaces in it and needed to be enclosed by quotes.

Trick to use file paths with spaces in Mallet (Terminal, OSx)?

Is there a trick to be able to use file paths with spaces in Mallet through the terminal on mac?
For example, all of the following give me errors:
escaping the space
./bin/mallet import-dir --input /Volumes/Macintosh\ HD/Users/MY_NAME/Desktop/en --output /Users/MY_NAME/Desktop/en.mallet --remove-stopwords TRUE --keep-sequence TRUE
double quotes, no escapes
./bin/mallet import-dir --input "/Volumes/Macintosh HD/Users/MY_NAME/Desktop/en" --output /Users/MY_NAME/Desktop/en.mallet --remove-stopwords TRUE --keep-sequence TRUE
and, with double quotes
./bin/mallet import-dir --input "/Volumes/Macintosh\ HD/Users/MY_NAME/Desktop/en" --output /Users/MY_NAME/Desktop/en.mallet --remove-stopwords TRUE --keep-sequence TRUE
and finally with single quotes
./bin/mallet import-dir --input '/Volumes/Macintosh\ HD/Users/MY_NAME/Desktop/en' --output /Users/MY_NAME/Desktop/en.mallet --remove-stopwords TRUE --keep-sequence TRUE
They all want to treat the folder as multiple folders, split on the space:
Labels =
/Volumes/Macintosh\
HD/Users/MY_NAME/Desktop/en
Exception in thread "main" java.lang.IllegalArgumentException: /Volumes/Macintosh\ is not a directory.
at cc.mallet.pipe.iterator.FileIterator.<init>(FileIterator.java:108)
at cc.mallet.pipe.iterator.FileIterator.<init>(FileIterator.java:145)
at cc.mallet.classify.tui.Text2Vectors.main(Text2Vectors.java:322)
Is there anyway around this, other than renaming all of my files with spaces to underscores? (I understand that I don't need to type /Volumes/Macintosh\ HD/... but can just start at /Users. This was just an example.)
The issue is that import-dir is designed to take multiple directories as input. The argument parser would need a way to distinguish this use case from the "escaped space" use case, keeping in mind that Windows paths can end in \.
The best way to support both cases might be to add a --single-input option that would take its argument as a single string.
I also find that the spreadsheet-style import-file command is almost always preferable to working with directories.
As a work around you could:
(1) write some code to read the directory contents and generate a single examples file for use with:
bin/mallet input-file
Here's the mallet quick-start page for importing which describes the input-file version: http://mallet.cs.umass.edu/import.php
(2) Generate a symbolic link to the folder in a location without any spaces in it

bad time argument filename in tmpwatch

While executing the tmpwatch to clean the tmp directory in Unix, we are getting the error:
error: bad time argument filename.
But the same command working in another server. Please suggest ways to resolve the issue.
/etc/cron.daily/tmpwatch is a very picky file that will cry when the syntax is not just right.
Take a look at your exclusion list.
If you have an absolute file name or directory name:
-x /tmp/filename
If you have a wildcard:
-X '/tmp/file*'
Not having the correct switch or/and not having the single quotes around the path with the wildcard in it will produce a bad time argument error.

How to delete files like 'Incoming11781rKD'

I have a programme that is generating files like this "Incoming11781Arp", and there is always Incoming, and there is always 5 numbers, but there are 3 letters/upper-case/lower-case/numbers/special case _ in any way. Like Incoming11781_pi, or Incoming11781rKD.
How can I delete them using a script run from a cron job please? I've tried -
#!/bin/bash
file=~/Mail/Incoming******
rm "$file";
but it failed saying that there was no matching file or directory.
You mustn't double-quote the variable reference for pathname expansion to occur - if you do, the wildcard characters are treated as literals.
Thus:
rm $file
Caveat: ~/Mail/Incoming****** doesn't work the way you think it does and will potentially match more files than intended, as it is equivalent to ~/Mail/Incoming*, meaning that any file that starts with Incoming will match.
To only match files starting with Incoming that are followed by exactly 6 characters, use ~/Mail/Incoming??????, as #Jidder suggests in a comment.
Note that you could make your glob (pattern) even more specific:
file=~/Mail/Incoming[0-9][0-9][0-9][0-9][0-9][[:alpha:]_][[:alpha:]_][[:alpha:]_]
See the bash manual for a description of pathname expansion and pattern syntax: http://www.gnu.org/software/bash/manual/bashref.html#index-pathname-expansion.
You can achieve the same effect with the find command...
$ directory='~/Mail/'
$ file_pattern='Incoming*'
$ find "${directory}" -name "${file_pattern}" -delete
The first two lines define the directory and the file pattern separately, the find command will then proceed to delete any matching files inside that directory.

batch file send current folder inside a quoted argument

I have a batch file for converting images, arguments are quotes, and quotes strings inside the argument are escaped. However, I want to send the current path %CD %also as an argument... but executing this:
"C:\Program Files\GIMP 2\bin\gimp-2.8.exe" -i -b "(python-fu-watermark-folder RUN-INTERACTIVE \"%CD%\" \"%CD%_watermarked\" \"C:/Documents and Settings/Jan/Desktop/watermarking/customEffectsWatermark.png\")" -b "(gimp-quit 0)"
gives me an echoed:
C:\Documents and Settings\Jan\Desktop\watermarking\tester>"C:\Program Files\GIMP2\bin\gimp-2.8.exe" -i -b "(python-fu-watermark-folder RUN-INTERACTIVE \"C:\Documents and Settings\Jan\Desktop\watermarking\tester\" \"C:\Documents and Settings\Jan\Desktop\watermarking\tester_watermarked\" \"C:/Documents and Settings/Jan/Desktop/watermarking/customEffectsWatermark.png\")" -b "(gimp-quit 0)"
which is nice, however, the backslashes from the directory are escape characters! so while executing the plugin I end up with a escaped directory (I dont want that):
WatermarkFolder-Warning: C:Documents and SettingsJanDesktopwatermarking ester
WatermarkFolder-Warning: C:Documents and SettingsJanDesktopwatermarking ester_watermarked
WatermarkFolder-Warning: files to be processed in total: 0
As I have no control over the path, is there a way to escape the escapecharacters?
Thanks!
Lode
Did you try making a variable with the value of %CD% double escaped?
Something like this:
set mycd=%cd:\=\\%
And using %mycd% where you were using %cd%

Resources