How to separate files owned by a specific user in Linux

Muhammad Ali Ahmed - Aug 23 - - Dev Community

## 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

for ubuntu

sudo apt update
sudo apt install rsync
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
. .
Terabox Video Player