Due to an accidental data mix-up, user data was unintentionally mingled on Nautilus App Server 1 at the /home/usersdata location by the Nautilus production support team in Stratos DC. To rectify this, specific user data needs to be filtered and relocated. Here are the details:
Locate all files (excluding directories) owned by user kareem within the /home/usersdata directory on App Server 1. Copy these files while preserving the directory structure to the /ecommerce directory.
Solution
Step 1: Connect to App Server 1 (stapp01)
ssh tony@stapp01
# Password: Ir0nM@n
Step 2: Switch to root or use sudo
sudo su -
# Password: Ir0nM@n
Step 3: Create the destination directory (if it doesn't exist)
mkdir -p /ecommerce
Step 4: Find and copy files owned by kareem
Use the find command with -type f to locate only files (not directories) and cpio or rsync to copy while preserving directory structure:
Method 1: Using find with cpio (Recommended)
find /home/usersdata -type f -user kareem -print0 | cpio -pdmv0 /ecommerce
Command breakdown:
-
find /home/usersdata -type f -user kareem: Find all files owned by kareem -
-print0: Use null character as separator (handles spaces in filenames) -
cpio -pdmv0: Copy files in pass-through mode-
-p: Pass-through mode (copy files) -
-d: Create directories as needed -
-m: Preserve modification times -
-v: Verbose output -
-0: Read null-terminated filenames
-
Method 2: Using find with tar (Alternative)
find /home/usersdata -type f -user kareem -print0 | tar -cf - --null -T - | tar -xf - -C /ecommerce
Method 3: Using find with rsync (if available)
find /home/usersdata -type f -user kareem -print0 | rsync -av --files-from=- --from0 / /ecommerce/
Step 5: Verify the copy
# Count files copied
find /ecommerce/home/usersdata -type f -user kareem 2>/dev/null | wc -l
# List some files to verify
find /ecommerce/home/usersdata -type f -user kareem -ls 2>/dev/null | head -20
# Check directory structure preserved
ls -la /ecommerce/home/usersdata/
# Verify ownership preserved
find /ecommerce -type f -user kareem -ls 2>/dev/null
Complete One-Line Commands
From jump host with password:
echo 'Ir0nM@n' | ssh tony@stapp01 "sudo -S bash -c 'mkdir -p /ecommerce && find /home/usersdata -type f -user kareem -print0 | cpio -pdmv0 /ecommerce'"
From jump host using heredoc:
ssh tony@stapp01 << 'EOF'
echo 'Ir0nM@n' | sudo -S bash -c '
mkdir -p /ecommerce
echo "Finding and copying files owned by kareem..."
find /home/usersdata -type f -user kareem -print0 | cpio -pdmv0 /ecommerce
echo ""
echo "Verification:"
echo "Files copied:"
find /ecommerce/home/usersdata -type f -user kareem 2>/dev/null | wc -l
echo ""
echo "Sample of copied files:"
find /ecommerce/home/usersdata -type f -user kareem 2>/dev/null | head -10
'
EOF
Step-by-Step Interactive Commands
# Connect to stapp01
ssh tony@stapp01
# Enter password: Ir0nM@n
# Become root
sudo su -
# Enter password: Ir0nM@n
# Create destination directory
mkdir -p /ecommerce
# Find all files owned by kareem and copy preserving structure
find /home/usersdata -type f -user kareem -print0 | cpio -pdmv0 /ecommerce
# Verify the copy
echo "Number of files copied:"
find /ecommerce/home/usersdata -type f -user kareem 2>/dev/null | wc -l
echo "Directory structure preserved:"
ls -la /ecommerce/home/usersdata/
echo "Sample files copied:"
find /ecommerce/home/usersdata -type f -user kareem 2>/dev/null | head -10
# Exit back
exit
exit
Understanding the Commands
find options explained:
-
/home/usersdata- Starting directory to search -
-type f- Only find regular files (not directories) -
-user kareem- Only files owned by user kareem -
-print0- Print with null character separator (handles spaces)
cpio options explained:
-
-p- Pass-through mode (copy files) -
-d- Create leading directories where needed -
-m- Preserve file modification times -
-v- Verbose (shows what's being copied) -
-0- Read null-terminated filenames from stdin
Alternative Methods
Method 4: Using find with xargs and cp
find /home/usersdata -type f -user kareem -print0 | xargs -0 -I {} cp --parents {} /ecommerce/
Method 5: Using a simple script
#!/bin/bash
DEST="/ecommerce"
SRC="/home/usersdata"
mkdir -p "$DEST"
find "$SRC" -type f -user kareem | while read -r file; do
# Get relative path
rel_path="${file#$SRC/}"
# Create destination directory
mkdir -p "$DEST/$(dirname "$rel_path")"
# Copy file
cp "$file" "$DEST/$rel_path"
echo "Copied: $rel_path"
done
Method 6: Using tar directly
find /home/usersdata -type f -user kareem -exec tar -rf /tmp/kareem_files.tar {} \;
mkdir -p /ecommerce
tar -xf /tmp/kareem_files.tar -C /ecommerce
rm /tmp/kareem_files.tar
Verification Commands
Run these to verify the operation:
# 1. Check total files copied
echo "Total files copied:"
find /ecommerce/home/usersdata -type f -user kareem 2>/dev/null | wc -l
# 2. Compare with original count
echo "Original files count:"
find /home/usersdata -type f -user kareem | wc -l
# 3. Check directory structure
echo "Directory structure:"
find /ecommerce/home/usersdata -type d 2>/dev/null
# 4. Check file ownership preserved
echo "Ownership verification:"
find /ecommerce -type f -user kareem -ls 2>/dev/null | head -5
# 5. Verify no directories were copied (only files)
echo "Checking no directories were copied:"
find /ecommerce -type d -user kareem 2>/dev/null | wc -l
# Should be 0 or minimal (only directory structure)
# 6. Check for any directories incorrectly copied as files
find /ecommerce -type f -user kareem -exec file {} \; | grep directory
# Should return no results
Expected Output
[root@stapp01 ~]# mkdir -p /ecommerce
[root@stapp01 ~]# find /home/usersdata -type f -user kareem -print0 | cpio -pdmv0 /ecommerce
/home/usersdata/docs/report.txt
/home/usersdata/projects/project1/config.yml
/home/usersdata/backup/data_backup.tar.gz
...
12 blocks
[root@stapp01 ~]# find /ecommerce/home/usersdata -type f -user kareem | wc -l
42
[root@stapp01 ~]# ls -la /ecommerce/home/usersdata/
total 12
drwxr-xr-x 3 root root 4096 Jul 01 10:00 .
drwxr-xr-x 3 root root 4096 Jul 01 10:00 ..
drwxr-xr-x 4 root root 4096 Jul 01 10:00 docs
drwxr-xr-x 3 root root 4096 Jul 01 10:00 projects
...
Troubleshooting
-
"No such file or directory": Ensure
/home/usersdataexists
ls -la /home/usersdata
- "Permission denied": Use sudo or run as root
sudo su -
- "cpio: Not enough memory": Use tar method instead
find /home/usersdata -type f -user kareem -exec tar -rf /tmp/kareem_files.tar {} \;
- "Invalid option": Check cpio version and options
cpio --version
# If issues, use tar method
-
Spaces in filenames: Ensure you use
-print0and-0options
Complete Solution Summary
The task has been completed on App Server 1 (stapp01):
- ✅ Located all files (excluding directories) owned by user
kareemin/home/usersdata - ✅ Copied files to
/ecommercedirectory - ✅ Preserved the directory structure
- ✅ Verified files were copied correctly
- ✅ Ownership maintained
The accidental data mix-up has been rectified by filtering and relocating only the files owned by user kareem to the /ecommerce directory while maintaining the original directory structure.
Top comments (0)