Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux Copy Files but Not Directories: Commands That Work

· 5 min read · Home Tech

In this article
  1. Why a plain cp command may include directories
  2. Copy only files directly inside a directory
  3. Copy files at every depth without copying directories
  4. Handle hidden files and shell wildcards
  5. Choose cp, find, or rsync
  6. Common errors and safe checks
  7. Frequently Asked Questions

Yes. To copy regular files without copying directories, use find to select files and pass them to cp. For a simple, non-recursive copy, a shell glob such as cp source/* destination/ may also work, but it can include directories and hidden files need separate handling.

Why a plain cp command may include directories

The cp command copies files by default, but it does not copy a directory’s contents recursively unless you ask it to with an option such as -R or -r. A command like cp source/* destination/ expands the wildcard in the shell before cp runs. That expansion can include both files and directory names. If a directory is among the arguments, cp reports that it omitted the directory unless recursive copying was requested.

This distinction matters because “copy files but not directories” can mean either copying only files directly inside one folder or finding files at any depth while excluding directory structure. Choose the method based on which result you need.

Copy only files directly inside a directory

For a flat source directory, use a glob and tell cp to skip directories:

cp -p -t /path/to/destination/ -- /path/to/source/*

On GNU/Linux, -t specifies the destination before the source arguments, and -p preserves file mode, ownership where permitted, and timestamps. The -- marks the end of options, which helps if a file name begins with a hyphen. If the source contains subdirectories, cp will report that it is omitting them; it will not copy their contents.

To avoid those messages and explicitly select regular files, use find:

find /path/to/source -maxdepth 1 -type f -exec cp -p -t /path/to/destination/ -- {} +

-maxdepth 1 limits the search to the source directory itself, and -type f selects regular files. The {} + form batches multiple file names into each cp invocation, while safely handling spaces, tabs, and newlines in names.

Copy files at every depth without copying directories

To find regular files in the source tree and put all of them directly into one destination folder, remove the depth limit:

find /path/to/source -type f -exec cp -p -t /path/to/destination/ -- {} +

This copies files from nested directories but flattens the result: the destination directory structure is not recreated. If two different source folders contain a file with the same name, they target the same destination path. One copy may overwrite another, so check for duplicate names before using this approach on important data.

To retain relative folder structure while excluding directories as copied objects, use a tool designed to preserve the tree, such as rsync with suitable filters. That is a different goal from putting every file in one flat folder.

Handle hidden files and shell wildcards

In typical Bash settings, * does not match names beginning with a dot, such as .config. It also does not search inside subdirectories. So cp source/* destination/ may leave hidden files behind even when it copies visible files.

The find commands above include hidden files because find searches directory entries regardless of whether their names begin with a dot. To select only non-hidden files in a flat directory, a shell glob can be convenient, but its behavior depends on shell settings. For predictable selection, use find and add a name condition, for example:

find /path/to/source -maxdepth 1 -type f ! -name '.*' -exec cp -p -t /path/to/destination/ -- {} +

Choose cp, find, or rsync

  • Use cp with a glob for a quick, flat copy when the source is simple and you understand how your shell expands the pattern.
  • Use find with -type f when you need an explicit rule, need hidden files included, or need to search through subdirectories.
  • Use rsync when you need repeatable synchronization, exclusions, or control over whether directory structure is kept.

For example, this rsync command copies regular files found in a tree into one destination folder:

find /path/to/source -type f -print0 | xargs -0 -I{} cp -p -- '{}' /path/to/destination/

That example is safe for unusual file names, but it has the same flattening and duplicate-name concerns as the earlier recursive find command. rsync itself can preserve relative paths and apply include or exclude rules, but its filters need to be written for the desired layout; do not assume that excluding directories automatically means copying every nested file into a flat destination.

Common errors and safe checks

Make sure the destination exists before copying. Create it with mkdir -p /path/to/destination if appropriate. Also confirm that the source and destination are not the same location, and check available space when copying many or large files.

Take extra care with overwrites. By default, cp can replace an existing destination file with the same name. Add -n to GNU cp to avoid overwriting existing files, or use -i to ask before replacing them. Behavior and available options can vary on non-GNU systems, so check cp --help or the local manual page. A dry run is not built into these basic commands; inspect the selected paths first, for example with find ... -type f -print, before running the copy.

Also note that -type f selects regular files, not directories, symbolic links, sockets, or other special file types. If you need to copy symbolic links as links, decide that explicitly: find normally does not follow symlinks when searching, and copying a symlink can have different results depending on the cp options used.

Frequently Asked Questions

How do I copy only files and not folders in Linux?
For files directly inside a folder, use find /source -maxdepth 1 -type f -exec cp -t /destination -- {} +. Remove -maxdepth 1 to include files in subfolders.

Does cp copy directories by default?
No. Recursive copying requires an option such as -r or -R. A wildcard can still pass directory names to cp, but without recursion those directories are not copied.

Does cp source/* include hidden files?
Usually not in Bash: a leading-dot name is not matched by * under the default settings. Use find when you want hidden files included reliably.

How can I copy files from subfolders into one folder?
Use find /source -type f -exec cp -t /destination -- {} +. This flattens the files into one destination, so different files with the same name can conflict.

How do I prevent files from being overwritten?
On GNU/Linux, add -n to cp to skip existing destination files, or use -i for a confirmation prompt. Verify the options supported by your system.