How to Fix the `cp: Cannot Create Regular File` Error

The cp: cannot create regular file error means the copy command could not create or open the destination file for writing; the message at the end tells you why. Common causes include a missing destination directory, insufficient permissions, a read-only filesystem, or a running executable that cannot be overwritten. Match the complete error below, then check the destination before changing permissions or using administrator privileges.

Error Ending Likely Cause Appropriate Fix
Permission denied Insufficient access to the destination file or one of its parent directories Check ownership and directory traversal permissions; grant only the access needed
No such file or directory Missing parent directory, incorrect path, or broken symlink Correct the path or create the intended parent directory
Read-only file system Destination filesystem or mount does not allow writes Resolve the mount or storage condition, or choose a writable destination
No space left on device Destination filesystem has exhausted available storage or inodes Free the exhausted resource or use another filesystem
Operation not permitted File attributes, filesystem restrictions, or security controls block the operation Identify and correct the specific restriction
Text file busy Destination is typically an executable currently running Use a controlled service stop or supported deployment procedure

What “Cannot Create Regular File” Means

The destination path and the trailing error determine the fix, not the shared “cannot create regular file” prefix.

A regular file is an ordinary file containing data, rather than a directory, device, or socket. The wording does not mean your file extension or file format is wrong.

For example:

cp: cannot create regular file '/var/www/html/index.html': No such file or directory

This identifies a destination-side failure. If /var/www/html does not exist, granting more permissions will not create it.

By contrast, an error such as cannot stat usually concerns locating or accessing the source. Keep the entire error message: removing the destination or the final explanation removes the most useful diagnostic information.

The examples below assume Linux with GNU Coreutils. Additional diagnostic tools, such as namei and findmnt, may not be installed in minimal containers.

How to Fix the cp: Cannot Create Regular File Error

Identify the intended paths, correct missing parent directories, and apply the narrowest appropriate ownership or permission correction.

Step 1: Identify the Exact Source and Destination

  1. Check your current directory if the failed command used relative paths:
    pwd
  2. Set the source and destination to the paths involved in your failure. Replace these example values before continuing:
    src="$HOME/project/report.txt"
    dest="$HOME/backups/reports/report.txt"

    Here, dest is the complete destination filename, not just its containing directory. If your original command copied into a directory, append the source filename when setting this diagnostic variable.

  3. Derive the parent directory:
    dest_dir=$(dirname -- "$dest")
  4. Inspect the source and the destination path:
    ls -l -- "$src"
    namei -l -- "$dest"

namei walks through the path components, showing ownership, permissions, and symlink resolution. A missing final filename is normal when creating a new file; a missing parent directory is not.

If the error came from a script, inspect the expanded destination argument rather than assuming its configuration value is correct. An empty variable, unexpected working directory, or incorrect architecture string can produce a valid-looking but unintended path.

Step 2: Correct Missing or Misconstructed Directories

  1. If the error ends in No such file or directory, identify the first missing parent component. Check spelling, capitalization, and variable expansion. A quoted path beginning with "~/" does not expand to your home directory. Use $HOME inside double quotes instead.
  2. Confirm that the directory is supposed to exist there. For a personal output directory, create the missing parents:
    mkdir -p -- "$dest_dir"

    The -p option creates missing parent directories and accepts directories that already exist. Ordinary cp does not create a missing destination directory tree.

  3. Check special destinations before creating anything: – Web roots: /var/www/html/ is common, but it is not universal. Confirm your web server’s configured document root before creating it. – Mounted storage: Verify that the intended drive or network share is mounted. Otherwise, creating directories under its mountpoint may place files on the underlying local filesystem. – Symlinks: Inspect where the link leads. An existing symlink can still point through a missing directory.

Do not manufacture a directory simply because an erroneous build script named it. A path containing an unexpected processor value such as unknown calls for correcting the path calculation.

Step 3: Check Ownership and Write Permissions

  1. Identify the account performing the copy:
    id
  2. Inspect the destination directory:
    ls -ld -- "$dest_dir"

    If the destination file already exists, inspect it too:

    ls -l -- "$dest"

Creating a new file requires write and execute/search permission on its parent directory, plus execute/search permission through the preceding directories. Overwriting an existing regular file normally requires write permission on that file and permission to traverse its path. Therefore, a writable file can still be inaccessible through a restricted parent directory.

  1. Apply the narrowest appropriate correction: – You own the destination directory, but its owner permissions block creation: add the required owner permissions.
    chmod u+wx -- "$dest_dir"

    – You own an existing destination file, but it is not writable: grant owner write access.

    chmod u+w -- "$dest"

    – A personal output directory was accidentally created under another account: if you are authorized to correct its ownership, change that directory only.

    sudo chown -- "$(id -un):$(id -gn)" "$dest_dir"

    Recheck its permissions afterward. This does not change the ownership of existing files inside it. – The destination is intentionally administrator-owned: use elevated privileges only for an approved copy into that location.

    sudo cp -i -- "$src" "$dest"

    The interactive option asks before overwriting an existing destination. Verify the copied contents before restarting any dependent service.

Do not recursively take ownership of /usr, /etc, or a web server’s entire directory tree. For shared application directories, use the intended deployment account, group, or access-control policy.

  1. If ordinary permissions look correct, investigate additional restrictions. ACLs, immutable file attributes, SELinux or AppArmor policies, and network-filesystem rules can block writes independently of the basic mode bits. Have the responsible administrator inspect the relevant attributes and denial logs rather than disabling security controls.

Administrator privileges are not a universal bypass: they do not make a read-only filesystem writable or make a running executable safe to overwrite.

Step 4: Check the Destination Filesystem

Use this step when the message identifies a storage problem or when ownership and permissions do not explain the failure.

  1. Inspect the filesystem containing the destination directory:
    findmnt -T "$dest_dir" -o TARGET,SOURCE,FSTYPE,OPTIONS

    If that directory does not exist yet, substitute its nearest existing parent. Confirm both the expected filesystem and its mount options. A destination that unexpectedly resolves to the root filesystem may indicate an absent mount. An ro mount option indicates read-only access.

  2. Check available storage:
    df -h -- "$dest_dir"
  3. Check available inodes:
    df -i -- "$dest_dir"

A filesystem can have free bytes but no available inodes for new files. User or project quotas can also prevent creation even when filesystem-wide space remains available; a quota failure commonly ends with Disk quota exceeded.

  1. Resolve the identified condition: – Remove only known-unneeded data from the affected filesystem, or choose another destination. – For inode exhaustion, identify expendable collections of small files rather than deleting arbitrary system files. – For quota limits, reduce your usage or request an authorized adjustment. – For an intentionally read-only mount, use an approved writable location or have its configuration changed. – If storage unexpectedly became read-only, investigate filesystem or device errors before attempting further writes.

Do not blindly force a read-write remount. A filesystem may have become read-only to limit damage after an error.

Step 5: Retry the Copy and Verify Its Contents

  1. Once the path, access, and filesystem checks pass, retry the copy:
    cp -i -- "$src" "$dest"

    If this is an authorized administrator-owned destination, use the approved elevated invocation instead.

  2. Compare the resulting regular file with the source:
    cmp -- "$src" "$dest"

    A successful cmp with no output means the file contents match. Run the comparison under an account authorized to read both files.

Do not rely solely on a quiet copy command: declining an overwrite prompt can leave the old file in place. A failed copy can also leave a partial destination, so verify it before using it as a deployment artifact or backup.

Special Cases for Scripts, Containers, and Mounted Drives

Check architecture-derived destinations, container mounts and identities, and mounted-volume paths before correcting the copy configuration.

Multiarch pkgconfig Destinations

Architecture-derived paths can be wrong even when the copy runs with administrator privileges.

In AppImageKit build 9df35eb, generated destinations included /usr/lib/unknown-linux-gnu/pkgconfig/ on Debian unstable x86_64 and /usr/lib/athlon-linux-gnu/pkgconfig/ on Ubuntu Trusty i386; those directories did not exist (issue details). The processor-name output selected by uname’s -p option is not a reliable substitute for the distribution’s multiarch directory name.

First, inspect the target system’s existing directories:

ls -d -- /usr/lib/*/pkgconfig

On Debian unstable x86_64, the usual destination is /usr/lib/x86_64-linux-gnu/pkgconfig. After confirming that exact directory exists and that this is the intended installation, the copy is:

sudo cp -i -- resources/liblz4.pc /usr/lib/x86_64-linux-gnu/pkgconfig/

On Ubuntu Trusty i386, the corresponding destination is /usr/lib/i386-linux-gnu/pkgconfig. Confirm the existing directory before using:

sudo cp -i -- resources/liblz4.pc /usr/lib/i386-linux-gnu/pkgconfig/

These are platform-specific alternatives, not two commands to run on every machine. Other distributions, architectures, and cross-compilation environments can use different layouts.

Update the build script’s destination calculation, rerun the affected build stage, and confirm that liblz4.pc reached the intended directory. Creating a directory named after an incorrect processor value only hides the underlying bug.

Containers and Project-Specific Jobs

A path that exists on the host does not automatically exist inside a container. Likewise, a writable host directory may be mounted read-only inside the container, or its ownership may not match the container process’s numeric user and group IDs.

Inspect these items in the environment where the copy actually runs:

  • The process’s user and group.
  • The expanded source and destination paths.
  • The container’s working directory.
  • The volume or bind-mount destination.
  • Whether the mount or container root filesystem is read-only.

If the output belongs on a writable volume, configure that volume and grant the intended runtime account access to it. Recreate or restart the workload as required by the configuration change, rerun the job, and verify the output through the intended mount. Running the container as root is not a general repair.

For project-specific failures, compare inputs, working directories, output-path configuration, and execution identities before changing shared copy code. A tagging stage can encounter a regular-file creation error in one project while identical code runs successfully in another.

External Volumes With Spaces in Their Names

Spaces must remain part of a single destination argument. For example, after replacing the source and mount path with your actual values:

cp -i -- "$HOME/project/report.txt" "/media/bill/62 gb volume/"

The quotes preserve the volume name, and the trailing slash makes the intended directory destination explicit. If this still ends in Permission denied, quoting is no longer the explanation.

Check the mounted filesystem’s ownership model. FAT and exFAT volumes commonly use mount options to determine Linux-visible ownership and permissions, so changing an individual file’s mode may not provide the access you expect. NTFS behavior depends on the driver and mount configuration.

Have the volume mounted with appropriate access for your account, or copy to a writable destination. Then retry the quoted command and compare the destination file with the source before disconnecting the drive.

Running Executables and RHOCP Backups

On Linux, overwriting an executable that is currently running can fail with Text file busy. Merely having an ordinary document open in an editor is not the same condition.

In Red Hat OpenShift Container Platform (RHOCP) 4, an etcd backup can encounter this failure while copying the etcdctl executable, according to Red Hat. The affected backup invocation is:

/usr/local/bin/cluster-backup.sh /home/core/assets/backup

The failure identifies this destination:

cp: cannot create regular file '/etc/kubernetes/static-pod-resources/bin/etcdctl': Text file busy

Notice that the failing destination is the cluster-managed executable, not the requested backup directory. Creating another backup directory or changing its permissions does not address that error.

For an application you manage directly, use a controlled maintenance sequence: stop the application cleanly, confirm the executable is no longer running, perform the approved replacement, restart it, and check its health.

For RHOCP-managed binaries, collect the full backup output and use the supported Red Hat recovery procedure. Do not delete etcdctl or kill cluster processes merely to make the copy proceed. After the underlying condition is resolved, rerun the backup and verify successful completion and the expected backup artifacts.

Confirm the Result and Prevent Repeat Failures

Before treating the issue as resolved, check more than the disappearance of the error:

  • Correct location: The file is on the intended filesystem, not beneath an absent drive or volume mount.
  • Correct content: The destination is complete and matches the expected source.
  • Correct access: The intended application account can read it, and its ownership and permissions remain appropriate.
  • Complete operation: A script copying multiple files completed every required copy, not just the last visible command.

Content comparison does not verify ownership, permissions, timestamps, or other metadata. Check those separately when deploying application files or preserving backups.

For recurring jobs, log the resolved destination and execution identity, validate required mounts and parent directories, and stop when a copy fails. Avoid interactive overwrite prompts in unattended jobs; define the intended overwrite behavior explicitly. Rerun the original workflow under its normal account to confirm that the repair works without accidental dependence on an administrator shell.

Frequently Asked Questions

What Does -bash: /bin/cp: Permission Denied Mean?

It means Bash could not execute /bin/cp, rather than that cp failed to write the destination. Possible causes include missing execute permission, a noexec filesystem mount, path-access restrictions, or a security-policy denial. Check the executable and its filesystem, and use your distribution’s package verification or repair process if its permissions or contents were changed unexpectedly.

What Does “Cannot Create Directory: Permission Denied” Mean?

It means the account performing the operation cannot create a directory in the selected parent location. Directory creation requires write and execute/search access to the parent, plus traversal access through preceding directories; filesystem and security restrictions can also block it. Use a location your account is allowed to modify or obtain a narrowly scoped permission correction.

How Do I Give a File chmod 777 Permissions?

The following command grants read, write, and execute permission to the owner, group, and everyone else:

chmod 777 -- example.txt

Replace example.txt with the intended filename, and use this setting only when that access is deliberate. It is usually inappropriate for fixing a copy error because it exposes the file unnecessarily and does not repair missing directories, read-only mounts, or incorrect paths.

Can This Error Occur When the Destination File Already Exists?

Yes: “cannot create regular file” does not prove that the destination is absent. GNU cp normally overwrites an existing regular file, but it still needs permission to open that file for writing, and attributes, security controls, or a running executable can prevent that operation. Inspect the existing target rather than deleting it as a first troubleshooting step.