I was wondering if this code was correct or not. I am trying to copy all the files and subdirectories in one directory to a newly created directory.


cp -R dir/*newdir/*


any suggestions?

Dani AI

Generated

The root problems here are a syntax error and shell globbing. 's original string joined source and destination without a space, and using a bare * skips dotfiles. was right to suggest the two-argument form of cp (source then destination); 's wildcard approach works for visible files but will not copy files or directories whose names start with a dot.

A reliable workflow that preserves permissions, timestamps and symlinks, and includes hidden files, is to create the destination and copy in archive mode. For example:

mkdir -p newdir
cp -a source/. newdir/

As an alternative for large, resumable, or remote transfers, use rsync:

rsync -a source/ newdir/

Notes and cautions: cp -a (archive) preserves metadata and copies recursively; using source/. or a trailing slash on the source ensures hidden files are included and that only the contents (not the parent directory itself) are copied. Avoid copying a directory into one of its own subdirectories to prevent recursion. For option details and edge cases see the cp and rsync manuals: cp manual and rsync manual.

Recommended Answers

All 2 Replies

cp -R directoryname newdirectoryname

I was wondering if this code was correct or not. I am trying to copy all the files and subdirectories in one directory to a newly created directory.


cp -R dir/*newdir/*


any suggestions?

cp -r dir/* newdir/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.