Skip to content

Instantly share code, notes, and snippets.

@vy-let
Last active August 26, 2015 06:02
Show Gist options
  • Save vy-let/ea305606ff208bf3ddeb to your computer and use it in GitHub Desktop.
Save vy-let/ea305606ff208bf3ddeb to your computer and use it in GitHub Desktop.
For moving something to the trash from the command line, instead of rm'ing it.
//
// trash.swift
// For moving something to the trash from the command line
// (instead of rm'ing it).
//
// Created by Violet Baddley on 2015-8-25.
// Copyright © 2015 Violet Baddley. All rights reserved.
// Provided under the 2-Clause BSD License.
//
import Foundation
let effectiveArguments = Process.arguments[1..<Process.arguments.count]
let trashFiles = effectiveArguments.map { NSURL.fileURLWithPath($0) }
for tf in trashFiles {
do {
try NSFileManager.defaultManager().trashItemAtURL(tf, resultingItemURL: nil)
} catch let delError as NSError {
if let tfrp = tf.relativePath {
print("Couldn’t trash \(tfrp): \(delError.localizedDescription)")
} else {
print("Couldn’t trash a file because it hasn’t a valid path.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment