I am trying to make a script that will copy files from a directory and place the copied files into a new directory.
I know that the cp
command will copy the files and the mkdir
command will create the directory but does anyone know how to combines these 2 commands into a single line?
Пока у меня есть
mkdir /root/newdir/ cp /root/*.doc /root/newdir
This gives the error message
mkdir: cannot create directory 'cp': Files exists
mkdir: cannot create directory '/root/files/wp.doc: File exists
mkdir: cannot create directory 'mkdir' : File exists
mkdir: cannot create directory '/root/files/new dir: file exists
However it does create the directory newdir
4 ответа
mkdir -p /root/newdir/ && cp /root/*.doc /root/newdir/
Это вызовет mkdir
для создания структуры каталогов, проверки успешного выполнения команды и вызова команды cp
, если это так.
mkdir /root/newdir/; cp /root/*.doc /root/newdir
Place semicolon between two commands
This happens because you do not tell the shell where exactly the commands end. In this case:
mkdir /root/newdir/ cp /root/*.doc /root/newdir
Your command cp
will go as an argument to the mkdir
command and shell tries to make the file named cp
. Same happens to all other.
By putting the ;
after commands. It tells the shell that command has been ended and next word is an another command.
Newline (Return key) is also treated as the command seprator. So if you put each command in next line, it also works fine. So you can try either of these:
mkdir /root/newdir/ ; cp /root/*.doc /root/newdir
ИЛИ
mkdir /root/newdir/
cp /root/*.doc /root/newdir
&&
over ;
as the former takes care of the possibility of a mkdir
failure.
Похожие вопросы
Новые вопросы
linux
ВНИМАНИЕ: Все вопросы по Linux должны быть связаны с программированием; те, которые не будут закрыты. Используйте этот тег, только если ваш вопрос касается программирования с использованием API-интерфейсов Linux или поведения, специфичного для Linux, а не только потому, что вы запускаете свой код в Linux. Если вам нужна поддержка Linux, вы можете попробовать https://unix.stackexchange.com или сайт Stack Exchange конкретного дистрибутива Linux, например https://askubuntu.com или https://elementaryos.stackexchange.com/.
cp
по умолчанию не копирует каталоги. Вы должны указать опцию-p
, если хотите. Проверьте содержимое вашего каталога/root
— возможно, вы создали там непреднамеренные каталоги, играя с этими командами.rsync
, it does it for you.