## In this post we will learn how we can separate files owned by a specific user while maintaining the directory structure in Linux
1. Locate files owned by specific users:
user find command to find files
find /home/usersdata -type f -user ali
Replace /home/usersdata with the directory where you want to search for files
This command will find all files owned by user ali excluding directories.
2. Install Rsync:
To install it
for centos
sudo yum update
sudo yum install rsync
for ubuntu
sudo apt update
sudo apt install rsync
3. Copy Files While Preserving Directory Structure
You can use rsync to copy files while preserving directory structure
rsync -av --files-from=<(find /home/usersdata -type f -user ali)/ /media
Explanation:
rsync -av: Syncs files in "archive" mode, which preserves permissions, timestamps, etc., and provides verbose output.
--files-from=<(find /home/usersdata -type f -user ali): This specifies the list of files to copy, generated by the find command.
/: The source directory (root) to consider the paths relative to.
/media: The destination directory where the files should be copied.
You can also write the list of files to a temporary file and then use rsync
find /home/usersdata -type f -user ali > /tmp/ali_files.txt
rsync -av --files-from=/tmp/ali_files.txt / /media
rm /tmp/ali_files.txt
Top comments (2)
Hidden gem :)
thanks for your appreciation i just started writing on dev.io