631 Stimmen

Linux: Kopieren und Erstellen des Zielverzeichnisses, wenn es nicht existiert

Ich möchte einen Befehl (oder wahrscheinlich eine Option für cp), der das Zielverzeichnis erstellt, wenn es nicht existiert.

Beispiel:

cp -? file /path/to/copy/file/to/is/very/deep/there

9 Stimmen

Die akzeptierte Antwort ist falsch (es gibt eine solche Option für cp). Siehe zweite Antwort von Paul Whipp.

1 Stimmen

@Ronenz, ich stimme zu, dass es eine erwähnenswerte Option in GNU gibt cp (wie von Paul Whipp beantwortet), aber es ist nicht so allgemein, wie es der OP wollte. Sie kann kopieren a/foo/bar/file まで b/foo/bar/file aber er kann nicht kopieren file まで b/foo/bar/file . (d. h. es funktioniert nur, um übergeordnete Verzeichnisse zu erstellen, die im Fall der ersten Datei bereits vorhanden waren, aber es kann keine beliebigen Verzeichnisse erstellen)

8voto

Spongman Punkte 9030

install -D file -m 644 -t /path/to/copy/file/to/is/very/deep/there

1 Stimmen

Eigentlich ist es überhaupt nicht dasselbe. Bei ihm müssen Sie den Dateinamen zweimal übergeben (daher das '-t'-Flag) und er setzt die Ausführungsbits der Datei (daher das '-m'-Beispiel). bei ihm sollte no die beste Antwort sein, da sie die Frage nicht wirklich beantwortet - meine hingegen schon.

2 Stimmen

Dies funktioniert für eine einzelne Datei. Berücksichtigen Sie einfach, dass there ist die endgültige Datei und nicht das endgültige Unterverzeichnis.

6voto

SD. Punkte 1342

Fügen Sie einfach das Folgende in Ihre .bashrc ein und ändern Sie es nach Bedarf. Funktioniert in Ubuntu.

mkcp() {
    test -d "$2" || mkdir -p "$2"
    cp -r "$1" "$2"
}

z.B. Wenn Sie die Datei "test" in das Zielverzeichnis "d" kopieren wollen verwenden,

mkcp test a/b/c/d

mkcp prüft zuerst, ob das Zielverzeichnis existiert oder nicht, wenn nicht, wird es erstellt und die Quelldatei/das Quellverzeichnis kopiert.

2 Stimmen

Wie schon in der anderen Antwort erwähnt, brauchen Sie nicht zu prüfen, ob das Verzeichnis existiert, bevor Sie mkdir -p . Sie wird erfolgreich sein, auch wenn sie bereits existiert.

6voto

Kaalahaan Punkte 61

Um es noch einmal zusammenzufassen und eine vollständige, funktionierende Lösung in einer Zeile zu geben. Seien Sie vorsichtig, wenn Sie Ihre Datei umbenennen wollen, sollten Sie eine Möglichkeit einbauen, einen sauberen Pfad zu mkdir anzugeben. $fdst kann file oder dir sein. Der nächste Code sollte in jedem Fall funktionieren.

fsrc=/tmp/myfile.unk
fdst=/tmp/dir1/dir2/dir3/myfile.txt
mkdir -p $(dirname ${fdst}) && cp -p ${fsrc} ${fdst}

oder bash-spezifisch

fsrc=/tmp/myfile.unk
fdst=/tmp/dir1/dir2/dir3/myfile.txt
mkdir -p ${fdst%/*} && cp -p ${fsrc} ${fdst}

0 Stimmen

Danke! Genau das habe ich gebraucht. In meinem Fall weiß ich nicht, ob das Ziel ein Verzeichnis hat oder nicht und ob das Verzeichnis existiert oder nicht, also gibt mir dieser Code das übergeordnete Verzeichnis, das ich dann sicher erstellen kann, wenn es nicht existiert

6voto

Omar Abusabha Punkte 307

Einfach ohne Skripterstellung und mit einfachem Befehl ...

mkdir -p /destination-folder/ && cp file-name /destination-folder/

4voto

thebunnyrules Punkte 1340

Ich habe ein Hilfsskript für cp geschrieben, das CP heißt (man beachte die Großbuchstaben) und genau das tun soll. Das Skript prüft den von Ihnen eingegebenen Pfad auf Fehler (außer dem letzten, der das Ziel ist), und wenn alles in Ordnung ist, führt es einen Schritt mkdir -p aus, um den Zielpfad zu erstellen, bevor es die Kopie startet. An diesem Punkt übernimmt das reguläre cp-Dienstprogramm und alle Schalter, die Sie mit CP verwenden (wie -r, -p, -rpL), werden direkt an cp weitergeleitet. Bevor Sie mein Skript verwenden, müssen Sie einige Dinge verstehen.

  • alle Informationen hier können mit CP --help abgerufen werden. CP --help-all enthält die Schalter von cp.
  • Das normale cp führt die Kopie nicht aus, wenn es den Zielpfad nicht findet. Mit CP haben Sie kein solches Sicherheitsnetz für Tippfehler. Ihr Ziel wird erstellt, wenn Sie also Ihr Ziel als /usrr/share/icons oder /usr/share/icon falsch schreiben, dann wird genau das erstellt.
  • normales cp neigt dazu, sein Verhalten an den vorhandenen Pfad anzupassen: cp /a/b /c/d hängt davon ab, ob d existiert oder nicht. Wenn d ein vorhandener Ordner ist, kopiert cp b in diesen, wodurch /c/d/b entsteht. Wenn d nicht existiert, wird b nach c kopiert und in d umbenannt. Wenn d existiert, aber eine Datei ist und b eine Datei ist, wird sie durch die Kopie von b überschrieben. Wenn c nicht existiert, führt cp die Kopie nicht aus und beendet sich.

CP hat nicht den Luxus, sich an bestehenden Pfaden zu orientieren, daher muss es einige sehr feste Verhaltensmuster haben. CP geht davon aus, dass das Objekt, das Sie kopieren, im Zielpfad abgelegt wird und nicht das Ziel selbst ist (d.h. eine umbenannte Kopie der Quelldatei/des Quellordners). Das bedeutet:

  • "CP /a/b /c/d" ergibt /c/d/b, wenn d ein Ordner ist
  • "CP /a/b /c/b" führt zu /c/b/b, wenn b in /c/b ein Ordner ist.
  • Wenn sowohl b als auch d Dateien sind: CP /a/b /c/d führt zu /c/d (wobei d eine Kopie von b ist). Dasselbe gilt für CP /a/b /c/b unter denselben Umständen.

Dieses Standardverhalten von CP kann mit dem Schalter "--rename" geändert werden. In diesem Fall wird angenommen, dass

  • "CP --rename /a/b /c/d" kopiert b nach /c und benennt die Kopie in d um.

Ein paar abschließende Bemerkungen: Wie bei cp kann CP mehrere Objekte auf einmal kopieren, wobei der zuletzt aufgeführte Pfad als Ziel angenommen wird. Es kann auch Pfade mit Leerzeichen verarbeiten, solange Sie Anführungszeichen verwenden.

CP prüft die von Ihnen eingegebenen Pfade und stellt sicher, dass sie existieren, bevor es die Kopie ausführt. Im strikten Modus (verfügbar über den Schalter --strict) müssen alle zu kopierenden Dateien/Ordner vorhanden sein, sonst wird keine Kopie durchgeführt. Im entspannten Modus (--relaxed) wird der Kopiervorgang fortgesetzt, wenn mindestens eines der aufgeführten Elemente vorhanden ist. Der entspannte Modus ist die Standardeinstellung. Sie können den Modus vorübergehend über die Schalter oder dauerhaft durch Setzen der Variable easy_going am Anfang des Skripts ändern.

So wird sie installiert:

Führen Sie dies in einem Nicht-Root-Terminal aus:

sudo echo > /usr/bin/CP; sudo chmod +x /usr/bin/CP; sudo touch /usr/bin/CP
gedit admin:///usr/bin/CP 

Fügen Sie in gedit das Dienstprogramm CP ein und speichern Sie es:

#!/bin/bash
#Regular cp works with the assumption that the destination path exists and if it doesn't, it will verify that it's parent directory does.

#eg: cp /a/b /c/d will give /c/d/b if folder path /c/d already exists but will give /c/d (where d is renamed copy of b) if /c/d doesn't exists but /c does.

#CP works differently, provided that d in /c/d isn't an existing file, it assumes that you're copying item into a folder path called /c/d and will create it if it doesn't exist. so CP /a/b /c/d will always give /c/d/b unless d is an existing file. If you put the --rename switch, it will assume that you're copying into /c and renaming the singl item you're copying from b to d at the destination. Again, if /c doesn't exist, it will be created. So CP --rename /a/b /c/d will give a /c/d and if there already a folder called /c/d, contents of b will be merged into d. 

#cp+ $source $destination
#mkdir -p /foo/bar && cp myfile "$_"

err=0 # error count
i=0 #item counter, doesn't include destination (starts at 1, ex. item1, item2 etc)
m=0 #cp switch counter (starts at 1, switch 1, switch2, etc)
n=1 # argument counter (aka the arguments inputed into script, those include both switches and items, aka: $1 $2 $3 $4 $5)
count_s=0
count_i=0
easy_going=true #determines how you deal with bad pathes in your copy, true will allow copy to continue provided one of the items being copied exists, false will exit script for one bad path. this setting can also be changed via the custom switches: --strict and --not-strict
verbal="-v"

  help="===============================================================================\
    \n         CREATIVE COPY SCRIPT (CP) -- written by thebunnyrules\
    \n===============================================================================\n
    \n This script (CP, note capital letters) is intended to supplement \
    \n your system's regular cp command (note uncapped letters). \n
    \n Script's function is to check if the destination path exists \
    \n before starting the copy. If it doesn't it will be created.\n    
    \n To make this happen, CP assumes that the item you're copying is \
    \n being dropped in the destination path and is not the destination\
    \n itself (aka, a renamed copy of the source file/folder). Meaning:\n 
    \n * \"CP /a/b /c/d\" will result in /c/d/b \
    \n * even if you write \"CP /a/b /c/b\", CP will create the path /a/b, \
    \n   resulting in /c/b/b. \n
    \n Of course, if /c/b or /c/d are existing files and /a/b is also a\
    \n file, the existing destination file will simply be overwritten. \
    \n This behavior can be changed with the \"--rename\" switch. In this\
    \n case, it's assumed that \"CP --rename /a/b /c/d\" is copying b into /c  \
    \n and renaming the copy to d.\n
    \n===============================================================================\
    \n        CP specific help: Switches and their Usages \
    \n===============================================================================\n
    \
    \n  --rename\tSee above. Ignored if copying more than one item. \n
    \n  --quiet\tCP is verbose by default. This quiets it.\n
    \n  --strict\tIf one+ of your files was not found, CP exits if\
    \n\t\tyou use --rename switch with multiple items, CP \
    \n\t\texits.\n
    \n  --relaxed\tIgnores bad paths unless they're all bad but warns\
    \n\t\tyou about them. Ignores in-appropriate rename switch\
    \n\t\twithout exiting. This is default behavior. You can \
    \n\t\tmake strict the default behavior by editing the \
    \n\t\tCP script and setting: \n
    \n\t\teasy_going=false.\n
    \n  --help-all\tShows help specific to cp (in addition to CP)."

cp_hlp="\n\nRegular cp command's switches will still work when using CP.\
    \nHere is the help out of the original cp command... \
    \n\n===============================================================================\
    \n          cp specific help: \
    \n===============================================================================\n"

outro1="\n******************************************************************************\
    \n******************************************************************************\
    \n******************************************************************************\
    \n        USE THIS SCRIPT WITH CARE, TYPOS WILL GIVE YOU PROBLEMS...\
    \n******************************************************************************\
    \n******************************* HIT q TO EXIT ********************************\
    \n******************************************************************************"

#count and classify arguments that were inputed into script, output help message if needed
while true; do
    eval input="\$$n"
    in_=${input::1}

    if [ -z "$input" -a $n = 1 ]; then input="--help"; fi 

    if [ "$input" = "-h" -o "$input" = "--help" -o "$input" = "-?" -o "$input" = "--help-all" ]; then
        if [ "$input" = "--help-all" ]; then 
            echo -e "$help"$cp_hlp > /tmp/cp.hlp 
            cp --help >> /tmp/cp.hlp
            echo -e "$outro1" >> /tmp/cp.hlp
            cat /tmp/cp.hlp|less
            cat /tmp/cp.hlp
            rm /tmp/cp.hlp
        else
            echo -e "$help" "$outro1"|less
            echo -e "$help" "$outro1"
        fi
        exit
    fi

    if [ -z "$input" ]; then
        count_i=$(expr $count_i - 1 ) # remember, last item is destination and it's not included in cound
        break 
    elif [ "$in_" = "-" ]; then
        count_s=$(expr $count_s + 1 )
    else
        count_i=$(expr $count_i + 1 )
    fi
    n=$(expr $n + 1)
done

#error condition: no items to copy or no destination
    if [ $count_i -lt 0 ]; then 
            echo "Error: You haven't listed any items for copying. Exiting." # you didn't put any items for copying
    elif [ $count_i -lt 1 ]; then
            echo "Error: Copying usually involves a destination. Exiting." # you put one item and no destination
    fi

#reset the counter and grab content of arguments, aka: switches and item paths
n=1
while true; do
        eval input="\$$n" #input=$1,$2,$3,etc...
        in_=${input::1} #first letter of $input

        if [ "$in_" = "-" ]; then
            if [ "$input" = "--rename" ]; then 
                rename=true #my custom switches
            elif [ "$input" = "--strict" ]; then 
                easy_going=false #exit script if even one of the non-destinations item is not found
            elif [ "$input" = "--relaxed" ]; then 
                easy_going=true #continue script if at least one of the non-destination items is found
            elif [ "$input" = "--quiet" ]; then 
                verbal=""
            else
                #m=$(expr $m + 1);eval switch$m="$input" #input is a switch, if it's not one of the above, assume it belongs to cp.
                switch_list="$switch_list \"$input\""
            fi                                  
        elif ! [ -z "$input" ]; then #if it's not a switch and input is not empty, it's a path
                i=$(expr $i + 1)
                if [ ! -f "$input" -a ! -d "$input" -a "$i" -le "$count_i" ]; then 
                    err=$(expr $err + 1 ); error_list="$error_list\npath does not exit: \"b\""
                else
                    if [ "$i" -le "$count_i" ]; then 
                        eval item$i="$input" 
                        item_list="$item_list \"$input\""
                    else
                        destination="$input" #destination is last items entered
                    fi
                fi
        else
            i=0
            m=0
            n=1                     
            break
        fi      
        n=$(expr $n + 1)
done

#error condition: some or all item(s) being copied don't exist. easy_going: continue if at least one item exists, warn about rest, not easy_going: exit.
#echo "err=$err count_i=$count_i"
if [ "$easy_going" != true -a $err -gt 0 -a $err != $count_i ]; then 
    echo "Some of the paths you entered are incorrect. Script is running in strict mode and will therefore exit."
    echo -e "Bad Paths: $err $error_list"
    exit
fi

if [ $err = $count_i ]; then
    echo "ALL THE PATHS you have entered are incorrect! Exiting."
    echo -e "Bad Paths: $err $error_list"
fi

#one item to one destination:
#------------------------------
#assumes that destination is folder, it does't exist, it will create it. (so copying /a/b/c/d/firefox to /e/f/firefox will result in /e/f/firefox/firefox
#if -rename switch is given, will assume that the top element of destination path is the new name for the the item being given.

#multi-item to single destination:
#------------------------------
#assumes destination is a folder, gives error if it exists and it's a file. -rename switch will be ignored.

#ERROR CONDITIONS: 
# - multiple items being sent to a destination and it's a file.
# - if -rename switch was given and multiple items are being copied, rename switch will be ignored (easy_going). if not easy_going, exit.
# - rename option but source is folder, destination is file, exit.
# - rename option but source is file and destination is folder. easy_going: option ignored.

if [ -f "$destination" ]; then
    if [ $count_i -gt 1 ]; then 
        echo "Error: You've selected a single file as a destination and are copying multiple items to it. Exiting."; exit
    elif [ -d "$item1" ]; then
        echo "Error: Your destination is a file but your source is a folder. Exiting."; exit
    fi
fi
if [ "$rename" = true ]; then
    if [ $count_i -gt 1 ]; then
        if [ $easy_going = true ]; then
            echo "Warning: you choose the rename option but are copying multiple items. Ignoring Rename option. Continuing."
        else
            echo "Error: you choose the rename option but are copying multiple items. Script running in strict mode. Exiting."; exit
        fi
    elif [ -d "$destination" -a -f "$item1" ]; then
        echo -n "Warning: you choose the rename option but source is a file and destination is a folder with the same name. "
        if [ $easy_going = true ]; then
            echo "Ignoring Rename option. Continuing."
        else
            echo "Script running in strict mode. Exiting."; exit
        fi
    else
        dest_jr=$(dirname "$destination")
        if [ -d "$destination" ]; then item_list="$item1/*";fi
        mkdir -p "$dest_jr"
    fi
else
    mkdir -p "$destination"
fi

eval cp $switch_list $verbal $item_list "$destination"

cp_err="$?"
if [ "$cp_err" != 0 ]; then 
    echo -e "Something went wrong with the copy operation. \nExit Status: $cp_err"
else 
    echo "Copy operation exited with no errors."
fi

exit

0 Stimmen

Dies ist vielleicht ein Overkill, aber ziemlich effizient, funktioniert wie erwartet, danke, guter Herr.

2 Stimmen

Overengineering vom Feinsten.

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X