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.
for f in $(find . -user yousuf) - Iterate all files owned by yousuf storing each one in variable f
do - start of code within for iteration
mkdir -p "/media/$(dirname $f)" - using the directory portion on the found file, recreate this directory in media if it doesn’t already exist
cp $f /media/$f - Copy the found file to /media, along with it’s directory
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.
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:
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.”
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.
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.