Linux User Files


I am trying to solve the below task:

There was some users data copied on Nautilus App Server 1 at /home/usersdata location by the Nautilus production support team in Stratos DC. Later they found that they mistakenly mixed up different user data there. Now they want to filter out some user data and copy it to another location. Find the details below:

On App Server 1 find all files (not directories) owned by user yousuf inside /home/usersdata directory and copy them all while keeping the folder structure (preserve the directories path) to /media directory.

It says cpio not found. How to fix this? Appreciate your help.


If cpio isn’t present, then try to find a way to do it using cp.
In Linux there is always more than one way to do a task.

I did tried CP command. Same error

cp must be there!


which cp

at the prompt. What does it say?

I see that cp is installed.

Here is the output

Back to the question. I tried with cp command, not able to solve the problem. Any hints?

And which cpio doesn’t find it?

If it does not, this is a mucky way to do it

cd /home/usersdata
sudo -i
for f in $(find . -user yousuf) ; do mkdir -p "/media/$(dirname $f)" ; cp $f /media/$f  ; done

cpio is not there/installed.

I tried your solution. Task not completed. Can you please explain the command?

for f in $(find . -user yousuf) ; do mkdir -p "/media/$(dirname $f)" ; cp $f /media/$f  ; done

My understanding is

  • for f in $(find . -user yousuf) ; - Find the user
  • do mkdir -p “/media/$(dirname $f)” - Create directory with username, in this case yousuf. Also do we need to mention yousuf or dirname?
  • cp $f /media/$f - Copy the created dir to /media

  1. for f in $(find . -user yousuf) - Iterate all files owned by yousuf storing each one in variable f
  2. do - start of code within for iteration
  3. mkdir -p "/media/$(dirname $f)" - using the directory portion on the found file, recreate this directory in media if it doesn’t already exist
  4. cp $f /media/$f - Copy the found file to /media, along with it’s directory
  5. done - end of for loop

; is the command separator in bash

Note that this only works when we cd /home/usersdata first, as we want the relative directory with respect to usersdata

Run find . -user yousuf on its own to see the output being processed by the loop.

cpio would be the much cleaner solution.

Someone else might come up with a better solution using only find, but like I said, there’s always more than one way to do something.

Were you to appear for LFCS or RHCSA, it is the end result that is marked (files are in the right place), not the commands you used to get there - as it is for KKE

And as you probably noticed, each time you redo a task in KKE, some things like name of user may change. Technique to achieve result is always the same.

For clarity, here is the same code formatted as if it were a shell script

cd /home/usersdata
for f in $(find . -user yousuf)
    mkdir -p "/media/$(dirname $f)"
    cp $f /media/$f


These two commands should show the same number of files

find . -user yousuf
ls -lR /media
1 Like

Thanks for your response @Alistair_KodeKloud and appreciate your patience in explaining the commands. I will try these one more time and let you know.

Yes, I am changing user name every time when I redo the task

Hey @Alistair_KodeKloud,

It worked when I "cd /home/usersdata".

for f in $(find . -user yousuf) ; do mkdir -p "/media/$(dirname $f)" ; cp $f /media/$f  ; done

Indeed it did, for the reason I gave initially…

Note that this only works when we cd /home/usersdata first, as we want the relative directory with respect to usersdata

Any idea why is my task failiing repeatedly?

Task: Find all Files inside the /home/usersdata/ (only in current directory) with kareem as the owner and Move them to the /blog directory?

My Solution:
find /home/usersdata/ -maxdepth 1 -user kareem -exec mv {} /blog/ \;
The command runs successfully in the console,

I then verified the find command for files owned by kareem in the source directory, it cant find any files that are owned by kareem. Also verified the contents of the /blog directory, the files have been moved.

However when I check my work, FAILED. it says files not copied. Please help.

The command you provided is a shell command that uses a for loop and several shell utilities to perform a series of operations on files found in the current directory and its subdirectories. Let’s break down the command step by step:

  1. for f in $(find . -user mark) ; do ... ; done:
  • This command starts a for loop. It iterates over the files found by the find command, which looks for files and directories in the current directory and its subdirectories (.) that are owned by the user “mark.”
  1. mkdir -p "/beta/$(dirname $f)":
  • Inside the loop, this command creates directories in the /beta directory based on the directory structure of the files found by find. Here’s what it does:
    • dirname $f: Extracts the directory part of the file path stored in the variable f. For example, if f contains “./folder/file.txt,” dirname $f would yield “./folder.”
    • /beta/$(dirname $f): Prepends /beta/ to the result of the dirname command, creating a new directory path in the /beta directory with the same structure as the original file path.
  • The -p flag with mkdir ensures that parent directories are created as needed, and it won’t throw an error if the directory already exists.
  1. cp $f /beta/$f:
  • This command copies the files found by find from their original location (stored in the variable f) to the corresponding directory structure under /beta. For example, if f contains “./folder/file.txt,” it would copy the file to “/beta/folder/file.txt.”

In summary, this command searches for files owned by the user “mark” in the current directory and its subdirectories. For each found file, it creates the same directory structure under the “/beta” directory (if it doesn’t exist) and then copies the file to the corresponding location in “/beta.” This is useful for moving files owned by a specific user to a different location while preserving their directory structure.