Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What do you mean exactly by "ordered output"? What do you mean by "full path file name" (those terms don't quite go together). Give us an actual example of what kind of results you want, and we'll try to help you.
As a first step though, find by default outputs file names appended with the input search path you feed it. So if you use "find ." you'll get output formatted as "./file". If you use "find /full/path/to" then your output will be "/full/path/to/file".
Also, instead of running the command directly, you could create a separate script that does the actual processing, including formatting the filenames, and call that with -exec.
"-exec" is just what it says on the tin. Execute the specified command for each matching file, with "{}" being where the file name goes.
However it's really only designed for simple, one-shot style operations. If you have a series of complex commands to run then it's usually better to write up a shell script of some kind, and only use find to produce the list of files for it to operate on.
Edit: There other exec commands out there, such as the bash built-in command, but those are completely different beasts. Don't confuse them with the find option.
Last edited by David the H.; 05-13-2011 at 07:10 PM.
But this has a side effect of awk printing the filename twice, which wouldn't be good if you're trying to pipe the output into further commands. Also, I think you forgot to change the field separator to "/".
One common way to process sorted input is to use xargs to execute the commands, instead of -exec.
The -print0 option in find separates the output by null characters instead of newlines, which makes it possible to easily handle files with spaces or other reserved characters, and sort's -z and xargs -0 options tell them to process the null-terminated input. Whether you need this or not depends on what kind of filenames you have.
Finally, I added the -h human-numeric-sort option to sort, so that 10 will come after 9 instead of after 1.
As for the full-path-in-the-output thing, I see now that the problem is actually with flac. It only displays the filename when printing the results of -t. There appears to be nothing we can do with find or anything else that can affect that.
The only thing I can think of then is to set up a script to check the test results.
Code:
#!/bin/bash
while read file ; do
flac --totally-silent -t "$file"
if (( $? == 0 )); then
echo "valid: $file"
else
echo "not valid: $file"
fi
done < <( find "$1" -name "*.flac" -print | sort -h )
exit 0
Launch it as "scriptname startdir".
So basically I've turned off flac's default output and instead print valid or invalid depending on the exit code of the test. According to the man page, the exit code will be 0 if the file is valid, and 1 if not. I'm assuming here that you won't need to worry so much about the full path if you can easily spot the bad files in the output, but if you still want to see it, just make sure the starting input contains the full path to the input.
Last edited by David the H.; 05-14-2011 at 01:27 PM.
Reason: minor rewording
Code:
find . -name "*flac" -print | awk '{print $NF, $0}' | sort
But this has a side effect of awk printing the filename twice, which wouldn't be good if you're trying to pipe the output into further commands. Also, I think you forgot to change the field separator to "/".
Actually, putting $NF first means that I need not worry about the field separator.
But what I wanted to discuss was single-quotes (in the OP's use of '*flac' .v. "*flac" .
Actually, putting $NF first means that I need not worry about the field separator.
But what I wanted to discuss was single-quotes (in the OP's use of '*flac' .v. "*flac" .
OK
Ummm ... no. Unless he has lots of filenames with spaces/tabs in them all you
get is the same file printed twice on the same line, because then $NF==1. And
w/ only one field $1 and $0 are the same thing. Now I'm not suggesting that
printing $NF for files w/ spaces should be considered meaningful for sorting... ;D
@AnanthaP, if you want to sort by filename, you have to separate the name from the path. As Tinkster pointed out, if awk's field separator is the default, then each file path will be seen as a single word and the output will be "/path/to/filename /path/to/filename", which is pretty much meaningless.
Of course even if you do change the FS to"/", all you get is an output that looks like "/path/to/filename filename", which is equally useless further down the line.
Try it yourself if you don't believe me. I did.
But this makes me wonder, is it possible to tell sort to sort on the last field of the line, whatever the length? I don't think so, but I could be wrong.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.