I know you can do mkdir
to create a directory and touch
to create a file, but is there no way to do both operations in one go?
i.e. if I want to do the below when the folder other
does not exist:
cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
Error:
cp: cannot create regular file `/my/other/path/here/cpedthing.txt': No such file or directory
Has anyone come up with a function as a workaround for this?
Use &&
to combine two commands in one shell line:
COMMAND1 && COMMAND2
mkdir -p /my/other/path/here/ && touch /my/other/path/here/cpedthing.txt
Note: Previously I recommended usage of ;
to separate the two commands but as pointed out by @trysis it's probably better to use &&
in most situations because in case COMMAND1
fails COMMAND2
won't be executed either. (Otherwise this might lead to issues you might not have been expecting.)