16 ответов:
если оба из следующих условий:
- вы используете версию GNU
cp(и не, например, версия для Mac), а- вы копируете из какой-то существующей структуры каталогов, и вам просто нужно ее воссоздать
затем вы можете сделать это с помощью
--parentsфлагcp. С информационной страницы (видимой по адресу http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation или с помощьюinfo cpилиman cp):--parents Form the name of each destination file by appending to the target directory a slash and the specified name of the source file. The last argument given to `cp' must be the name of an existing directory. For example, the command: cp --parents a/b/c existing_dir copies the file `a/b/c' to `existing_dir/a/b/c', creating any missing intermediate directories.пример:
/tmp $ mkdir foo /tmp $ mkdir foo/foo /tmp $ touch foo/foo/foo.txt /tmp $ mkdir bar /tmp $ cp --parents foo/foo/foo.txt bar /tmp $ ls bar/foo/foo foo.txt
Короткий Ответ:
скопировать
myfile.txtдо/foo/bar/myfile.txtиспользовать:mkdir -p /foo/bar && cp myfile.txt $_как это работает?
есть несколько компонентов для этого, так что я буду охватывать весь синтаксис шаг за шагом.
The команды mkdir утилиты , как указано в стандарте POSIX, составляет каталоги. Элемент
-pаргумент, согласно документам, вызовет команды mkdir досоздать любой отсутствующий промежуточный путь компоненты
это означает, что при вызове
mkdir -p /foo/bar,команды mkdir создать/fooи/foo/barесли/fooуже не существует. (Без-p, вместо этого он выдаст сообщение об ошибке.The
&&оператор списка, как описано в POSIX standard (или руководство Bash если вы предпочитаете), имеет эффект, чтоcp myfile.txt $_выполняется только еслиmkdir -p /foo/barуспешно выполняет. Это означает, чтоcpкоманда не будет пытаться выполнить, еслиmkdirне для одна из многих причин, по которой он может потерпеть неудачу.наконец,
$_мы передаем в качестве второго аргументаcp- это "специальный параметр", который может быть удобен для предотвращения повторения длинных аргументов (например, путей к файлам) без необходимости хранить их в переменной. Пер руководство Bash это:расширяется до последнего аргумента предыдущей команда
в данном случае, это
/foo/barмы прошли кmkdir. Так что увеличивается доcp myfile.txt /foo/bar, копииmyfile.txtво вновь созданное .обратите внимание, что
$_и не часть стандарта POSIX, поэтому теоретически вариант Unix может иметь оболочку, которая не поддерживает эту конструкцию. Однако я не знаю ни одной современной оболочки, которая не поддерживает$_; конечно, баш, Даш, а ЗШ все делают.
заключительное Примечание: команда, которую я дал в начале этого ответа, предполагает, что имена ваших каталогов не имеют пробелов. Если вы имеете дело с именами с пробелами, вам нужно будет цитировать их, чтобы разные слова не рассматривались как разные аргументы для
mkdirилиcp. Так что ваша команда на самом деле будет выглядеть так:mkdir -p "/my directory/name with/spaces" && cp "my filename with spaces.txt" "$_"
такой старый вопрос, но, может быть, я могу предложить альтернативное решение.
можно использовать
installпрограмма для копирования файла и создания пути назначения "на лету".install -D file /path/to/copy/file/to/is/very/deep/there/file
есть некоторые аспекты, чтобы принять во внимание, хотя:
- вам нужно укажите также имя файла назначения не только конечный путь
- конечный файл будет исполняемый (at крайней мере, насколько я видел из моих тестов)
вы можете легко изменить #2, добавив
-mвозможность установки разрешений на целевой файл (пример:-m 664создаст целевой файл с разрешениямиrw-rw-r--, как создание нового файла сtouch).
и вот это бесстыдная ссылка на ответ меня вдохновила=)
функция оболочки, которая делает то, что вы хотите, называя ее" похоронить " копию, потому что она выкапывает отверстие для файла, чтобы жить в:
bury_copy() { mkdir -p `dirname ` && cp "" ""; }
вот один из способов сделать это:
mkdir -p `dirname /path/to/copy/file/to/is/very/deep/there` \ && cp -r file /path/to/copy/file/to/is/very/deep/there
dirnameдаст вам родительский каталог или файл назначения. mkdir-p ' dirname ...'затем создаст этот каталог, гарантируя, что при вызове cp-r правильный базовый каталог будет на месте.преимущество этого над -- parents заключается в том, что он работает для случая, когда последний элемент в пути назначения является именем файла.
и это будет работать на OS X.
при всем моем уважении к ответам выше, я предпочитаю использовать rsync следующим образом:
$ rsync -a directory_name /path_where_to_inject_your_directory/пример:
$ rsync -a test /usr/local/lib/
просто добавьте следующее в свой .bashrc, настроить, если вам нужно. Работает в Ubuntu.
mkcp() { test -d "" || mkdir -p "" cp -r "" "" }например Если вы хотите скопировать файл 'test' в каталог назначения 'd' Используйте,
mkcp test a/b/c/dmkcp сначала проверит, существует ли каталог назначения или нет, если нет, то сделайте его и скопируйте исходный файл/каталог.
cpимеет несколько использований:$ cp --help Usage: cp [OPTION]... [-T] SOURCE DEST or: cp [OPTION]... SOURCE... DIRECTORY or: cp [OPTION]... -t DIRECTORY SOURCE... Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.ответ@AndyRoss работает для
cp SOURCE DESTстиль
cp, но делает неправильную вещь если вы используетеcp SOURCE... DIRECTORY/стиль
cp.Я думаю, что "DEST" неоднозначен без конечной косой черты в этом использовании (т. е. там, где целевой каталог еще не существует), и, возможно, поэтому
cpникогда не добавлял опцию для этого.Итак, вот моя версия этой функции который обеспечивает заднюю косую черту на dest dir:
cp-p() { last=${@: -1} if [[ $# -ge 2 && "$last" == */ ]] ; then # cp SOURCE... DEST/ mkdir -p "$last" && cp "$@" else echo "cp-p: (copy, creating parent dirs)" echo "cp-p: Usage: cp-p SOURCE... DEST/" fi }
просто возобновить и дать полное рабочее решение, в одной строке. Будьте осторожны, если вы хотите переименовать свой файл, вы должны включить способ предоставления чистого пути dir к mkdir. $fdst может быть файл или каталог. Следующий код должен работать в любом случае.
fsrc=/tmp/myfile.unk fdst=/tmp/dir1/dir2/dir3/myfile.txt mkdir -p $(dirname ${fdst}) && cp -p ${fsrc} ${fdst}или bash specific
fsrc=/tmp/myfile.unk fdst=/tmp/dir1/dir2/dir3/myfile.txt mkdir -p ${fdst%/*} && cp -p ${fsrc} ${fdst}
rsync file /path/to/copy/file/to/is/very/deep/thereЭто может сработать, если у вас есть правильный вид
rsync.
копировать из исходного кода в несуществующий путь
mkdir –p /destination && cp –r /source/ $_Примечание: эта команда копирует все файлы
cp –rдля копирования всех папок и их содержимого
$_работа в качестве пункта назначения, который создается в последнюю команду
Я написал сценарий поддержки для cp, называемый CP (Примечание заглавными буквами), который предназначен именно для этого. Скрипт проверит ошибки в пути, который вы ввели (кроме последнего, который является местом назначения), и если все хорошо, он сделает шаг mkdir-p для создания пути назначения перед запуском копии. На этом этапе обычная утилита cp берет на себя и любые коммутаторы, которые вы используете с CP (например,- r,- p,- rpL передается непосредственно в cp). Прежде чем использовать мой скрипт, есть несколько вещей ты должен понять.
- всю информацию здесь можно получить, выполнив CP -- help. CP --help - все включают в себя переключатели cp.
- обычный cp не будет делать копию, если он не найдет путь назначения. У вас нет такой сети безопасности для опечаток с CP. Вы пункт назначения будет создан, так что если вы неправильно пишете свой пункт назначения как /usrr/share/icons или /usr/share/icon, это то, что будет создано.
- обычный cp имеет тенденцию моделировать его поведение на существующий путь: cp/a /b/c / d будет зависеть от того, существует ли d или нет. если D находится в существующую папку, СР экземпляра B в него, и /С/Д/Б. Если D не существует, Б будут скопированы в C и переименована в D. Если D существует, но файл-это файл, он будет перезаписан б копия. Если c не существует, cp не выполняет копирование и завершает работу.
CP не имеет роскоши принимать сигналы от существующих путей, поэтому он должен иметь некоторые очень твердые модели поведения. CP предполагает, что элемент вы копируете удаляется в пути назначения и не является само назначение (ака, переименованная копия исходного файла/папки). Значение:
- "CP/a /b/c /d" приведет к/c/d / b, Если D-папка
- "СР /А/Б /Ц/Б" приведет к /б/б б в /б-это папка.
- если оба b и d являются файлами: CP/a /b/c /d приведет к/c / d (где d-копия b). То же самое для CP /a/b /c/b в тех же обстоятельствах.
этот поведение CP по умолчанию может быть изменено с помощью переключателя" --rename". В этом случае, предполагается, что
- "СР-переименовать /а/б /ц/д" копирование B в /C и переименовать копию в д.
несколько заключительных замечаний: как и в случае с cp, CP может копировать несколько элементов за раз, причем последний путь, указанный в списке, считается местом назначения. Он также может обрабатывать пути с пробелами, если вы используете кавычки.
CP проверит пути вы кладете внутри и убеждается они существуют до того, как сделать копию. В строгом режиме (доступно через -- strict switch) все копируемые файлы/папки должны существовать или копирование не выполняется. В расслабленном режиме (--relaxed) копирование будет продолжено, если существует хотя бы один из перечисленных элементов. Relaxed mode-это режим по умолчанию, вы можете изменить режим временно с помощью переключателей или постоянно, установив переменную easy_going в начале скрипта.
вот как его установить:
в некорневой терминал, сделайте:
sudo echo > /usr/bin/CP; sudo chmod +x /usr/bin/CP; sudo touch /usr/bin/CP gedit admin:///usr/bin/CPв gedit вставьте утилиту CP и сохраните:
#!/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: ) 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=,,,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
Допустим, вы делаете что-то вроде
СР файл1.txt A/B/C/D / file.txt
где A/B/C / D-каталоги, которые еще не существуют
возможное решение выглядит следующим образом
DIR=$(dirname A/B/C/D/file.txt) # DIR= "A/B/C/D" mkdir -p $DIR cp file1.txt A/B/C/D/file.txtнадеюсь, что помогает!
Comments