Tutoriel Linux

Copy a Linux folder with hidden files and permissions

Débutant3 min de lecture

Copying a folder may seem trivial until the day the directory contains hidden files, symbolic links, or useful dates for a script. cp -r copies the hierarchy, but cp -a is often the better choice for duplicating a working folder without changing its attributes.

Before deleting the source, create the copy, check it, and open a few files. A successful command does not prove that you aimed at the right path.

Secure copy of two folders on Linux storage
Copy first, then check the files, permissions, and links.

Choosing between cp -r and cp -a

cp -r goes down into subdirectories. Entries that start with a dot are included when you copy the folder itself. Avoid, however, globbing like * if you also expect hidden files: the shell ignores them by default.

cp -a enables archive mode. It preserves permissions, ownerships, dates, symbolic links, and hard links as much as possible. For a local backup or a directory migration, it is generally the command to prefer. It may require elevated rights if you need to preserve an owner that your account cannot define.

cp -a -- /srv/app/ /srv/app-copie/

The -- separates options from paths. It prevents a file starting with a dash from being interpreted as an option. The trailing slash on the source here means you are copying its contents into /srv/app-copie/. If this folder already exists, check the resulting structure before proceeding.

Preparing a destination without overwriting the wrong folder

First, create a clearly named destination, then list both sides. On a server, do not initiate a recursive copy to a manually constructed path in a script without displaying that path in the logs.

mkdir -p -- /srv/app-copie
find /srv/app -maxdepth 1 -printf '%fn' | sort
find /srv/app-copie -maxdepth 1 -printf '%fn' | sort

If you want to refuse any overwriting during an initial attempt, add -n. This option ignores files already present, so it is not suitable for a complete synchronization. To replace existing data, make a backup or work in a new directory, then switch the service to the validated copy.

Checking files, permissions, and links

Start by checking hidden files and permissions. ls -la displays names starting with a dot. stat allows you to compare the permissions and dates of a sensitive file.

ls -la /srv/app
ls -la /srv/app-copie
stat -c '%n %a %U:%G %y' /srv/app/config.ini /srv/app-copie/config.ini
find /srv/app-copie -type l -ls

The last command lists symbolic links. With cp -a, a link normally remains a link. With a less suitable copy, you might end up with the content of its target or a link that has become invalid after moving.

  • count the files with find ... -type f | wc -l;
  • open the most important configuration file;
  • test the program against the copy before deleting the source;
  • keep the source as long as the service or script has not passed its functional check.

To compare the contents of two trees, diff -qr /srv/app /srv/app-copie reports the differing files. It does not replace application validation, but it quickly detects an incomplete copy.

If you also need to correct permissions after duplication, the guide on recursive chmod in Linux explains why a chmod -R launched too quickly can break executables. To locate a file before the copy, also use find in Linux.

sudo apt update && sudo apt upgrade