Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm a beginner in programming and I'm having trouble writing a script.
Write a script with two parameters: file name mask and directory name.
The script's task is to display a list of file names from the given directory, matching the given mask together with the name of the owner and his UID number
I am asking for advice on the execution of the script.
Regards. :)

What I have tried:

<pre>! # / Bin / bash
find "$ 1" -name "*. $ 2" -exec stat -c "% N% u% U" '{}' \;
Posted
Updated 18-Jan-20 9:11am
Comments
Richard MacCutchan 19-Jan-20 3:35am    
The use of "*.$2" is probably wrong, since the second parameter is the complete filename mask according to your question.

1 solution

Pay attention to your spaces and capital letters
What you want is something more like
Bash
#!/bin/bash
find "$1" -name "*.$2" -exec stat -c "% N % u% U" {} \;

I have not tried this to find out if it does what you want, or to find out if the call to stat is right, but note the lack of spaces and that the hash-bang (#!) is followed by /bin/bash not / Bin / bash

Your solution does have one possible improvement I might recommend. Using -exec means that a new process is spawned for every file found by find. If you have thousands of matches, then this could take a very long time. A better solution might be to use xargs e.g.
find "$1" -name "*.$2" | xargs stat -c "% N % u% U"
See man xargs for details

I'm also not sure about your second arg to find. I think what you have matches only on the file extension (e.g. foo.txt). If you want to pass in a file glob to match, say "*a*.bin", to find bar.bin, max.bin apple.bin, etc, then you could probably just use "$2" as the argument to -name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900