Is there a way to make mv create the directory to be moved to if it doesn’t exist?

How about this one-liner (in bash): mkdir –parents ./some/path/; mv yourfile.txt $_ Breaking that down: mkdir –parents ./some/path # if it doesn’t work; try mkdir -p ./some/path creates the directory (including all intermediate directories), after which: mv yourfile.txt $_ moves the file to that directory ($_ expands to the last argument passed to the previous … Read more

Recursive mkdir() system call on Unix

There is not a system call to do it for you, unfortunately. I’m guessing that’s because there isn’t a way to have really well-defined semantics for what should happen in error cases. Should it leave the directories that have already been created? Delete them? What if the deletions fail? And so on… It is pretty … Read more

mkdir -p functionality in Python [duplicate]

For Python ≥ 3.5, use pathlib.Path.mkdir: import pathlib pathlib.Path(“/tmp/path/to/desired/directory”).mkdir(parents=True, exist_ok=True) The exist_ok parameter was added in Python 3.5. For Python ≥ 3.2, os.makedirs has an optional third argument exist_ok that, when True, enables the mkdir -p functionality—unless mode is provided and the existing directory has different permissions than the intended ones; in that case, OSError … Read more

How do I use filesystem functions in PHP, using UTF-8 strings?

Just urlencode the string desired as a filename. All characters returned from urlencode are valid in filenames (NTFS/HFS/UNIX), then you can just urldecode the filenames back to UTF-8 (or whatever encoding they were in). Caveats (all apply to the solutions below as well): After url-encoding, the filename must be less that 255 characters (probably bytes). … Read more

PHP mkdir with form input and security

You need to use $_POST to get the filename. As has been posted in the comments, you also need to do something with $_POST[‘filename’] to insure that the user is not trying to post a relative path to your script and trying to create folders in locations that you don’t intend. At the very least … Read more