DEV Community

Cover image for How to separate files owned by a specific user in Linux
Muhammad Ali Ahmed
Muhammad Ali Ahmed

Posted on

1

How to separate files owned by a specific user in Linux

## 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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
martinbaun profile image
Martin Baun

Hidden gem :)

Collapse
 
muhammad_aliahmed profile image
Muhammad Ali Ahmed

thanks for your appreciation i just started writing on dev.io

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more