This question may be silly and super easy for linux connaisseurs, but I was just wondering, for instance, I want to use the >find command to search for a file and send the results to a text file, anyone knows how I can do this? Im using lucid lynx btw


You want to use redirection for this.

find -somecriteria > somefile.txt
  • How can we search a file like saveuser.php on whole server in linux ,is this is the same command will run on ubuntu? – Rahul Mehta Nov 26 '10 at 6:30
  • 1
    @Rahul: All GNU/Linux and BSD systems have find available in one form or another. – Ignacio Vazquez-Abrams Nov 26 '10 at 6:34
  • How can we search a file like saveuser.php on whole server in ubuntu – Rahul Mehta Nov 26 '10 at 6:35
  • 1
    find $doc_root -name saveuser.php -- if you don't know your web server's doc_root, use / – glenn jackman Nov 26 '10 at 12:06
  • You may also want to look at the 'tee' command, which will write it to a file and print to stdout at the same time. – Bron Gondwana Jul 25 '12 at 12:06

IF you just want to find certain files. As Ignacio answered:

find -somecriteria > somefile.txt

If you want to find certain files, and output the content of these files.

find -somecriteria -exec cat {} + > somefile.txt
  • Thanks this works if the file exists, but if it doesnt then it doesnt save the message, do u know how to do that?? – Zloy Smiertniy Nov 26 '10 at 6:54
  • Sorry, i didn't understand u. u mean the command upper or lower? – wliao Nov 26 '10 at 8:03

You could run the commands by Python, using it's subprocess module, and save the results in text files. Redirection also works(as shown by @Ignacio), but contents of the file will over-written every time the command is run.

  • 1
    If he wants to append to a file, he can use the >> operator. – dennycrane Nov 26 '10 at 6:15

Seems your need is to find the file by FILENAME rather than some other criteria.

There's a much better utility for this than 'find' available, called 'locate'. It uses the locate database to find files by name, so it's much faster than searching over the filesystem.

First, run 'updatedb' as root to ensure your locate database is up-to-date - and possibly add a CRON job to run that nightly if there isn't one already.

Then, use 'locate filename' or 'locate -r regexp' to locate files.

I mention the second option because 'locate abc.log' will find for example abc.log.1 and abc.log.1.gz and whatever else matches. So, 'locate -r abc.log\$' in this case will locate ONLY files named abc.log

Finally, as has already been mentioned, to put the resulting output of the command into a file, use > as in:

locate -r \^abc.log\$ > /home/me/all.abc.log.files

Yes, you can use redirection:

$ find . > some_file_name.txt
$ cat some_file_name.txt

The > symbol toward the end of a command redirects any output that would normally go to the console to instead be written a file, the name of which is after the >.

  • 4
    That's not a pipe. – Ignacio Vazquez-Abrams Nov 26 '10 at 6:12
  • I think it is :-) I may be wrong<br/>google.com.au/… – kjtl Nov 26 '10 at 6:15
  • 2
    You are. From the first hit returned by your linked search: "You will notice that a pipe is defined by the | symbol -- not an uppercase i or the number one, but a vertical bar.", just before it presents pipes as a better way of doing things than using redirection (with < and >) to write one command's output to a file and then read that file as input to another command. – Dave Sherohman Nov 26 '10 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.