Smart Systems for Home

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

Linux Copy File to Current Directory: cp Examples

· 5 min read · Home Tech

In this article
  1. What the Current Directory Means
  2. Basic cp Commands
  3. Copying Several Files
  4. What Happens if a File Already Exists?
  5. Permissions, Hidden Files, and Links
  6. Copying Versus Moving and Other Tools
  7. Frequently Asked Questions

To copy a file into your current directory on Linux, use cp followed by the file’s path and a dot: cp /path/to/file.txt .. The dot means “the directory I am in now,” so this command creates a copy named file.txt here.

What the Current Directory Means

Your current directory, also called the working directory, is the location a shell uses when interpreting relative paths. It is not necessarily your home directory or the directory containing the file you want to copy. Check where you are with:

  • pwd prints the full path of the current directory.
  • ls lists its contents, so you can check whether the copied file is already there.

For example, if pwd prints /home/ana/projects, then cp /tmp/report.pdf . copies the file to /home/ana/projects/report.pdf. The source can be written as an absolute path, such as /tmp/report.pdf, or as a relative path, such as ../report.pdf. The destination dot always refers to the directory in which you run the command.

Basic cp Commands

The general form is cp SOURCE DESTINATION. For a file in another directory, use its path as the source and . as the destination:

  • cp /var/log/app.log . copies one file from /var/log into the current directory.
  • cp ../notes.txt . copies a file from the parent directory.
  • cp ~/Downloads/photo.jpg . copies a file from the current user’s Downloads directory. The shell expands ~ to the home-directory path.

To give the copy a different name, specify that name as the destination instead of using a dot alone. For example, cp /tmp/report.pdf report-backup.pdf writes the copy in the current directory as report-backup.pdf. This is useful when you want to retain the original while making the local copy’s purpose clear.

Copying Several Files

You can provide multiple source files, but the final argument must be the destination directory. Since . is the current directory, this copies both files here:

cp /tmp/one.txt /tmp/two.txt .

Shell wildcards can select groups of files. For example, cp /tmp/*.txt . copies files whose names end in .txt from /tmp, provided the shell expands the pattern to matching files. If there are no matches, behavior depends on the shell; Bash normally leaves the unmatched pattern unchanged, and cp then reports that it cannot find it. Quote a wildcard only when you want it treated literally: cp '/tmp/*.txt' . does not select the matching files.

Ordinary cp copies files, not whole directory trees. To copy a directory and its contents, use a recursive option such as cp -r /path/to/folder .. Check the result before doing this with broad paths or wildcards, since recursion can copy far more data than intended.

What Happens if a File Already Exists?

If the destination directory already contains a file with the same name, cp normally replaces its contents. This can destroy the previous version without asking, depending on how cp is configured. Use -i to prompt before overwriting:

cp -i /path/to/file.txt .

Answer the prompt carefully; the exact wording can vary. To avoid replacing an existing destination, GNU cp supports -n, which skips existing files. Options and details can differ among Linux distributions and other Unix-like systems, so consult cp --help or man cp for the version installed on your system.

For a one-off copy where you want to ensure the target name is unused, check it first with ls -l ./file.txt. A “file exists” error or overwrite prompt is a reason to confirm which file you intend to keep, not to repeat the command blindly.

The user running cp must be able to read the source and write to the destination directory. A “Permission denied” message may mean the source is unreadable or the current directory is not writable. Confirm the paths and permissions with ls -l /path/to/file and ls -ld .. Do not add sudo automatically: elevated privileges can create a root-owned copy or overwrite a system file. Use them only when you understand why ordinary permissions are insufficient.

Names beginning with a dot, such as .config, are hidden by convention in Linux. They are still ordinary files and can be copied by naming them explicitly: cp /path/to/.config .. A wildcard such as * generally does not match hidden names in common shells unless the shell’s hidden-file behavior has been changed.

By default, copying a symbolic link usually copies the link itself or follows it according to the implementation and command options. If the distinction matters—for example, when a link points to a file outside the source directory—check the local cp manual and use an explicit option rather than assuming the link’s target will be copied.

Copying Versus Moving and Other Tools

cp leaves the original in place and creates another file. mv /path/to/file.txt . moves the file instead, removing it from its former location when the move succeeds. Use cp when you need both copies; use mv when you want to relocate the file.

For an ordinary local file, cp is usually the simplest choice. For remote transfers, use a tool intended for that job, such as scp or rsync. For a large directory tree or repeatable backups, rsync offers options to transfer only differences and preserve selected metadata. Those tools solve related problems, but they are not needed merely to copy one local file into the working directory.

Frequently Asked Questions

How do I copy a file from another folder to the current folder?
Run cp /path/to/file ., replacing the example path with the file’s actual path. Use pwd if you need to confirm the destination directory.

What does the dot mean in cp commands?
A single dot, ., is a path shorthand for the current directory. In cp file ., it tells cp where to place the copy.

How do I copy a file and rename it?
Give the desired destination filename: cp /path/to/file.txt new-name.txt. Because the destination is a relative path, the new file is created in the current directory.

How do I copy a file without overwriting an existing one?
On GNU/Linux, try cp -n /path/to/file . to skip an existing destination, or use cp -i /path/to/file . to be prompted. Check man cp because option support can vary.

How do I copy a folder into the current directory?
Use cp -r /path/to/folder . to copy the directory tree. Verify the source path first, especially if it contains many files.