How To Copy Paste File In Linux Terminal – Using Absolute Path For File Operations

Moving a file from one folder to another in the Linux terminal involves typing the cp command followed by the source and destination paths. If you are wondering how to copy paste file in linux terminal, the process is simpler than you think and relies on a few core commands that every user should know.

Copying files in the terminal is a fundamental skill. It lets you duplicate data without using a mouse or graphical interface. This guide will walk you through every step, from basic commands to advanced options.

How To Copy Paste File In Linux Terminal

The cp command is your primary tool. It stands for “copy” and works with two main arguments: the file you want to copy and where you want to put it.

Here is the basic syntax:

cp [options] source_file destination

For example, to copy a file named report.txt from your current folder to a folder called backup, you would type:

cp report.txt backup/

This creates a copy of report.txt inside the backup directory. The original file remains untouched.

Copying A File To A Different Name

You can also copy a file and rename it at the same time. Just specify the new name as the destination.

cp report.txt backup/report_backup.txt

This copies report.txt into the backup folder and names the copy report_backup.txt.

Copying Multiple Files At Once

To copy several files to the same directory, list them all before the destination folder.

cp file1.txt file2.txt file3.txt backup/

All three files are copied into the backup directory. The destination must be a folder, not a file name.

Copying Directories Recursively

Copying a folder requires the -r (recursive) option. Without it, cp will skip directories.

cp -r my_folder/ backup/

This copies the entire my_folder directory and all its contents into the backup directory. If backup already exists, the folder is placed inside it.

Preserving File Attributes

Use the -p flag to preserve the original file’s timestamps, ownership, and permissions.

cp -p report.txt backup/

This is usefull when you want the copy to have the same modification time and access rights as the original.

Interactive Copying To Avoid Overwrites

The -i (interactive) flag prompts you before overwriting an existing file. This prevents accidental data loss.

cp -i report.txt backup/

If backup/report.txt already exists, the terminal will ask for confirmation. Type y to overwrite or n to skip.

Verbose Output For Confirmation

Add the -v (verbose) flag to see what the command is doing. It prints each file name as it is copied.

cp -v report.txt backup/

Output might look like:

'report.txt' -> 'backup/report.txt'

This is helpfull for debugging or when copying many files.

Using Absolute And Relative Paths

You can use either absolute paths (starting from the root /) or relative paths (based on your current location).

An absolute path example:

cp /home/user/documents/report.txt /home/user/backup/

A relative path example (assuming you are in /home/user):

cp documents/report.txt backup/

Both achieve the same result. Relative paths are shorter and easier to type when you are already in the right area.

Using The Tilde For Home Directory

The tilde ~ is a shortcut for your home directory. You can use it in paths.

cp report.txt ~/backup/

This copies the file to the backup folder inside your home directory.

Copying Files With Wildcards

Wildcards let you copy multiple files that match a pattern. The asterisk * matches any number of characters.

To copy all .txt files:

cp *.txt backup/

To copy files starting with “report”:

cp report* backup/

The question mark ? matches a single character. For example, file?.txt matches file1.txt but not file10.txt.

Copying Hidden Files

Hidden files start with a dot (like .bashrc). To copy them, you need to include the dot explicitly.

cp .bashrc backup/

To copy all hidden files in a directory, use .*:

cp .* backup/

Be careful with this pattern because it also matches the current directory . and parent directory ... You may get an error message, but the files will still copy.

Copying Files Over SSH

If you need to copy a file to a remote server, use the scp command (secure copy). It works similarly to cp but over SSH.

scp report.txt user@server:/path/to/destination/

To copy from a remote server to your local machine:

scp user@server:/path/to/file.txt local_folder/

You will be prompted for the remote user’s password. For frequent transfers, consider setting up SSH keys.

Copying With Rsync For Large Files

The rsync command is ideal for copying large files or directories, especially over a network. It only transfers differences, making it faster for repeated copies.

rsync -avh report.txt backup/

The -a flag preserves attributes, -v gives verbose output, and -h makes sizes human-readable.

To sync an entire directory:

rsync -avh my_folder/ backup/

Note the trailing slash on my_folder/ – it copies the contents, not the folder itself.

Common Mistakes And How To Avoid Them

Here are frequent errors beginners make when learning how to copy paste file in linux terminal:

  • Forgetting the destination: If you omit the destination, cp will show an error. Always specify where to put the copy.
  • Missing the recursive flag: Trying to copy a directory without -r gives a “omitting directory” error.
  • Overwriting files accidentally: Use -i to get a prompt before overwriting.
  • Using wrong path: Double-check your paths, especially relative ones. Use pwd to see your current directory.
  • Spaces in file names: If a file name has spaces, enclose it in quotes or use a backslash to escape the space. For example: cp "my file.txt" backup/ or cp my\ file.txt backup/.

Copying Files With Sudo

Some files are owned by root or other users. To copy them, you may need superuser privileges with sudo.

sudo cp /etc/config.conf ~/backup/

You will be asked for your password. Be careful when using sudo – it can overwrite system files if you are not careful.

Copying To Protected Directories

If the destination directory requires root permissions, use sudo for the entire command.

sudo cp report.txt /root/backup/

This copies the file into the root user’s backup folder.

Using Tab Completion For Faster Typing

Tab completion is a huge time saver. Start typing a file or directory name, then press the Tab key. The terminal will auto-complete the name if it is unique.

If there are multiple matches, press Tab twice to see a list. This reduces typos and speeds up your workflow.

Copying And Pasting In The Terminal Itself

You can also copy and paste text within the terminal using keyboard shortcuts. In most terminal emulators:

  • Copy: Ctrl + Shift + C
  • Paste: Ctrl + Shift + V

This is different from the standard Ctrl + C and Ctrl + V, which send interrupt signals in the terminal. Use the shift versions for copy-paste operations.

Copying Files With A Progress Bar

The standard cp command does not show progress. For large files, use pv (pipe viewer) to see how much has been copied.

pv report.txt > backup/report.txt

This shows a progress bar and transfer speed. Install pv with your package manager if it is not already available.

Another option is rsync with the --progress flag:

rsync --progress report.txt backup/

This gives a real-time update of the transfer.

Copying Files With Date Stamps

You can append the current date to a file name when copying. This helps with version control.

cp report.txt backup/report_$(date +%Y%m%d).txt

This creates a file like report_20250315.txt in the backup folder. The $(date +%Y%m%d) part inserts today’s date.

Using A Loop To Copy Multiple Files With Stamps

For multiple files, use a simple loop:

for file in *.txt; do cp "$file" "backup/${file%.txt}_$(date +%Y%m%d).txt"; done

This copies each .txt file and adds the date before the extension.

Copying Files Between Different Filesystems

When copying between different filesystems (e.g., from ext4 to NTFS), the cp command works fine, but some attributes may not be preserved. Use rsync with the --no-owner and --no-group flags if permissions cause issues.

rsync -avh --no-owner --no-group report.txt /mnt/usb/

This avoids errors when the destination filesystem does not support Linux permissions.

Copying Files With Links Instead Of Duplicates

Sometimes you want to create a reference to a file rather than a full copy. Use hard links or symbolic links.

Hard link:

ln report.txt backup/report_hardlink.txt

This creates a second directory entry pointing to the same data. Changes to one affect the other.

Symbolic link (symlink):

ln -s report.txt backup/report_symlink.txt

This creates a shortcut. If you delete the original, the symlink breaks.

Copying Files With Compression

To save space, you can copy and compress a file at the same time using gzip or bzip2.

cp report.txt backup/ && gzip backup/report.txt

Or use a pipe to compress on the fly:

cat report.txt | gzip > backup/report.txt.gz

This creates a compressed copy. To decompress later, use gunzip.

Copying Files With Verification

To ensure the copy is identical to the original, use the --checksum option with rsync or manually compare checksums.

rsync -avhc report.txt backup/

The -c flag forces a checksum comparison. Alternatively, use md5sum:

md5sum report.txt backup/report.txt

If the outputs match, the files are identical.

Copying Files With Exclusions

When copying directories, you may want to exclude certain files. Use rsync with the --exclude option.

rsync -avh --exclude='*.log' my_folder/ backup/

This copies everything except files ending in .log. You can also exclude entire directories:

rsync -avh --exclude='temp/' my_folder/ backup/

Copying Files With Dry Run

Before executing a copy, use the --dry-run option with rsync to see what would happen.

rsync -avh --dry-run my_folder/ backup/

This prints a list of files that would be copied without actually copying anything. It is a safe way to test your command.

Frequently Asked Questions

How Do I Copy A File In Linux Terminal Without Overwriting?

Use the -n flag with cp to prevent overwriting. For example: cp -n report.txt backup/. If the destination file exists, the copy is skipped.

What Is The Command To Copy A Folder In Linux Terminal?

Use cp -r followed by the folder name and destination. For example: cp -r my_folder/ backup/. The -r flag is required for directories.

How Can I Copy A File From One Server To Another In Linux?

Use scp or rsync over SSH. For example: scp report.txt user@server:/path/. Both commands work over encrypted connections.

Why Does Cp Say “Omitting Directory”?

This error occurs when you try to copy a directory without the -r (recursive) flag. Add -r to copy directories and their contents.

How Do I Copy A File With Spaces In The Name?

Enclose the file name in quotes or use a backslash before each space. For example: cp "my file.txt" backup/ or cp my\ file.txt backup/.

Final Tips For Mastering File Copying

Practice these commands regularly to build muscle