Need to create a shell script that copies all files owned by a user
So I need to create a shell script that looks into the /shared catalog, and then archives all files owned by a specific user, and puts that archive in his home folder. The script is supposed to copy the entire /shared catalog basically, but it needs to ignore any sub catalogs that doesn't contain files that he owns.
This is actually a school assignment, but searching for help online is allowed. Here are the hints from the assignment • Combine the power of find, cp and tar in a shell script • Have a look at the -exec parameter to find • Have a look at the $PATH shell variable I've tried to understand the -exec param, but the only thing I get to work is find -executable, I don't understand how to combine the commands so that it actually copies the files that it found. I looked in the man page, but I didn't really get it |
Hello and Welcome to LinuxQuestions,
Let me thank you upfront for indicating this is a school assignment. It's a bit of custom that LQ users don't offer ready made solutions, especially not when homework is involved. You'll find us at our best if you show us what you already have 'invented/found out' by yourself and where your code is failing. Then we'll point you in the right direction. You'll learn a lot more that way then when someone provides you a working solution. So, show us what you've got and we'll help you out fixing what's missing. Looking forward to your participation in the forums. Have fun with Linux. Kind regards, Eric |
I know how to make an archive with the tar command. According to the instructions in the assignment, I suppose that it's easier to combine find with cp, and make a tar out of a temporary directory. Then removing that directory of course, wich is why I believe they put "the script should remove any temporary files or directories that it created" in the instructions. Doing said actions is not a problem for me, the problem is how to only copy executable files, and skip empty folders. I have read the man page, and as far as I understand, I'm supposed to use
Code:
find -executable cp /target/directory{} + I maybe should mention this is a task required for a higher grade,thus we are required to search for the information ourselfs, since the lectures only gives us information on how to perform basic tasks. |
Hello,
I think you're confusing -executable with -exec which is completely different. If you run for example: Code:
find . -type f -executable Code:
find . -executable This is what the man page says for -executable: Quote:
Code:
find . -type f -name 'test*' -exec cp /target/directory '{}' \; Quote:
Quote:
Kind regards, Eric |
Quote:
Code:
find -executable (since I wan't executable files right) -exec cp -r /target/directory '{}' \; |
Why would you copy the tree when tar accepts files on the command line like 'tar -f /home/username/archive.tar `find /shared_catalog -type f -user username 2>/dev/null`' so you can put the archive in /home like required w/o requiring any intermediate steps?
|
Quote:
I assume that your target directory is inside the directory that you are searching, right? In this case use the -path and -prune option so that it won't try to copy itself to itself. Also use '-type f' to only copy files and not directories. Finally, correct your cp command: Code:
cp SOURCE DEST |
Quote:
It would be very helpful to us if you'd post what you've written so far in order to help you troubleshoot. It's kind of walking in the dark without your code. Looking at parts of a script or separate commands without seeing the entire picture is a bad idea since we don't know what you're doing next in your script. Kind regards, Eric |
Thanks everyone for helping me out. And sorry for maybe acting like such a noob, I am one when it comes to Linux though ;)
This command acutally helped, it fulfills all the requirements. Only owned files, no empty directories, no trash left behind. Quote:
|
Quote:
|
All times are GMT -5. The time now is 10:05 AM. |