Linux · Lesson

Linux · File Operations

Copy, Move & Sync Files

Add the YouTube embed URL for this lesson in components/linux/linuxLessons.js.

File copy and movement in Linux

Linux provides multiple tools to copy, move and synchronize files. The most commonly used commands are cp, mv and rsync.

Copy (cp)

Used to duplicate files and directories.

Move (mv)

Used to move or rename files and directories.

Sync (rsync)

Efficient tool for backup and synchronization.

Copy files (cp)

1. Copy a file to another location

Use cp to duplicate files into another directory safely.

terminal — bash
linux-lab
[root@localhost ~]# cp anaconda-ks.cfg /mnt
[root@localhost ~]# ls /mnt
anaconda-ks.cfg hgfs

2. Copy a directory recursively

Use -r option to copy directories and their contents.

terminal — bash
linux-lab
[root@localhost ~]# cp -r /etc /mnt
[root@localhost ~]# ls /mnt
etc

3. Copy multiple files

Copy multiple files or directories in a single command.

terminal — bash
linux-lab
[root@localhost ~]# cp -r anaconda-ks.cfg /etc /mnt
[root@localhost ~]# ls /mnt
anaconda-ks.cfg etc

Common cp options

  • -i → prompt before overwrite
  • -r → copy directories recursively
  • -v → verbose output
  • -f → force overwrite
  • -p → preserve permissions and timestamps

Move and rename (mv)

1. Move a file or directory

mv relocates files or directories to another path.

terminal — bash
linux-lab
[root@localhost ~]# mv /mnt/etc /mnt/tmp
[root@localhost ~]# ls /mnt/tmp
etc

2. Rename a file or directory

mv is also used to rename files and folders.

terminal — bash
linux-lab
[root@localhost ~]# mv file1 new_file
[root@localhost ~]# mv dir1 new_dir
[root@localhost ~]# ls
new_file new_dir

Common mv behavior

  • mv is used for both moving and renaming
  • If destination exists, files may be overwritten
  • -i prevents accidental overwrite

Synchronization (rsync)

1. Copy files using rsync

rsync efficiently synchronizes files between locations.

terminal — bash
linux-lab
[root@localhost ~]# rsync -v file-1 file-2 /mnt
file-1
file-2
 
sent 131 bytes received 54 bytes 370.00 bytes/sec

2. Synchronize a directory

Use rsync to sync directory contents efficiently.

terminal — bash
linux-lab
[root@localhost ~]# rsync -iv /root/* /mnt
>f..T...... file-1
>f..T...... file-2
>f+++++++++ file-3

rsync advantages

  • Transfers only changed data
  • Efficient for backups
  • Supports remote transfers over SSH
  • Can be scheduled using cron