How to Have the Cp Command Create Any Necessary Folders for Copying a File to a Destination

Linux: copy and create destination dir if it does not exist


mkdir -p "$d" && cp file "$d"

(there's no such option for cp).

Is there anyway to copy a file to a directory that does not exist?

before your copy, you need to perform

mkdir -p ./createThisFolder

If you want to know whether or not there are files in the directory, I'd recommend directly checking whether or not your files exist (instead of checking if the directory exists). For instance:

if [ ! -s ./createThisFolder/temp.file ] ; then 
echo "file doesn't exist (or it is empty)"
fi

Copy files to destination and create destination if does not exist

You assume that the last argument must be a directory, and if no such directory exists, it should be created. In zsh, I would do it like this:

mkcp() {
local destdir=$@[-1]
if [[ -f $destdir ]]
then
echo Missing destination directory 2>&1
return 1
else
mkdir -p $destdir
if [[ -d $destdir ]]
then
cp "$@"
else
echo Can not create $destdir
return 1
fi
fi
}

The problem is not the copying (the plain cp command can do it and no loop is needed), so the focus here is on error checking.

Note that with my approach, switches can be passed implicitly to cp, for instance

mkcp -r foo bar baz

copies recursively the subdirectories too, and

mkcp -rv foo bar baz

is in addition printing the names of the files which are copied.

Create path when copying a file in linux

If you have Gnu coreutils (normally you will on linux), you can use:

install -m 0644 -D source_file dest_filename

which automatically creates necessary directories.

The -m 0644 flag sets the permissions of the new file to 0644 (rw-r--r--); you need that because the default is to set the permissions to rxwr-xr-x (on the assumption that the file should be executable, since that's the usual use case) rather than copying the permissions of the original file.

How do I copy folder with files to another folder in Unix/Linux?

The option you're looking for is -R.

cp -R path_to_source path_to_destination/
  • If destination doesn't exist, it will be created.
  • -R means copy directories recursively. You can also use -r since it's case-insensitive.
  • To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use -a flag along with trailing /. in the source (as per @muni764's / @Anton Krug's comment):
cp -a path_to_source/. path_to_destination/

How to use 'cp' command to exclude a specific directory?

rsync is fast and easy:

rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude

You can use --exclude multiples times.

rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude

Note that the dir thefoldertoexclude after --exclude option is relative to the sourcefolder, i.e., sourcefolder/thefoldertoexclude.

Also you can add -n for dry run to see what will be copied before performing real operation, and if everything is ok, remove -n from command line.

Should Copy-Item create the destination directory structure?

The -recurse option only creates a destination folder structure if the source is a directory. When the source is a file, Copy-Item expects the destination to be a file or directory that already exists. Here are a couple ways you can work around that.

Option 1: Copy directories instead of files

$source = "c:\some\path\to\a\dir"; $destination = "c:\a\different\dir"
# No -force is required here, -recurse alone will do
Copy-Item $source $destination -Recurse

Option 2: 'Touch' the file first and then overwrite it

$source = "c:\some\path\to\a\file.txt"; $destination = "c:\a\different\file.txt"
# Create the folder structure and empty destination file, similar to
# the Unix 'touch' command
New-Item -ItemType File -Path $destination -Force
Copy-Item $source $destination -Force

How to copy a directory structure but only include certain files (using windows batch files)

You don't mention if it has to be batch only, but if you can use ROBOCOPY, try this:

ROBOCOPY C:\Source C:\Destination data.zip info.txt /E

EDIT: Changed the /S parameter to /E to include empty folders.



Related Topics



Leave a reply



Submit