Pass ls command output to diff

Hi ,

Can some one help me how to pass ls command output to diff command

ex : - ls *.xml will return files which have time stamps

         abc-<time-stamp>.xml
        xyz-<time-stamp>.xml

           diff   abc-<time-stamp>.xml  xyz-<time-stamp>.xml >> newfile.txt

we need to check this some 30 directories , so we need cd to directory and do ls and then pass file names from ls to diff

---------- Post updated at 07:04 AM ---------- Previous update was at 06:30 AM ----------

Can some one respond quickly

Using terms like "urgent" or "quickly" and/or bumping up posts is not THAT well received in these forums as people are volunteers spending their free time trying to help people solve their problems.

You can help speed up things from your side by giving an excellent specification or description of the problem, respectively, including details of your system and using code tags as required by forum rules.

Assuming you are using a modern bourne type shell, you can use "command substitution" to supply a command's output to e.g. diff . To give a more taylored answer, more info is needed, like

  • how many files are in each of those directories?
  • check every files against each of the other files?
  • all results to the same newfile?
  • any dirs in that path to be excluded?

Do you want files that have same name - time-stamp is the same value on both files?
Otherwise you need to be more exact in your requirement - by that I mean: please explain what you are doing -NOT how you think it should be done

If you do differences on every file from each of 30 directories, you will have a mountain of output to deal with. After a long wait. That would be true even if you had only 3 files in each directory.

@ Rudic

  • how many files are in each of those directories?
    we will have many other files , we need to diff with the first file that was created in each directory of that day and with latest last file at the time of doing diff
  • check every files against each of the other files?
    No , need to diff from first file that was created in each directory of that day and with latest last file at the time of doing diff
  • all results to the same newfile?
    yes all results will be same file

we can put come thing echo "========= dir1 ============" in between

  • any dirs in that path to be excluded?
    no all dirs need to be checked

@ Jim

we need to diff with the first file that was created in each directory of that day and with latest last file at the time of doing diff and output should go single file

So - where lies your problem? Using ls (together with other commands) to gain the two desired filenames, or presenting them to diff ?
And, what you specify in post#4 will need a script, more or less, which goes beyond what you said in post#1 and the title. Why don't you start over and give us a detailed spec accompanied with meaningful input and desired output samples? We still don't know your OS nor shell version...

yeah we need script

[LEFT]detailed spec would be like

i need to cd to 30 to 40 dirs and need to ls for ls *.xml and we need to diff the first file that was created in each directory of that day and with latest last file at the time of doing diff[/LEFT]
im looking how we can present the desired files to diff , i hope specification clear

its Redhat 6 OS being used

Is this a homework assignment? Homework assignments must be filed in the Homework & Coursework Questions forum and must include a fully filled out homework template from the Rules for Homework & Coursework Questions forum.

What shell are you using?

Why are you restricting our options by just using ls to get a list of file when find would simplify this task?

Dan ,

im  using bash here , what ever the best  possible way we can go with , here is sample snippet
For entry in /mydir/*
Do
If [-d "$entry" ,]; then
 Echo "$entry"
 CD $entry
ls *xml 
# we need to diff the first file that was created in each directory of  that day and with latest last file at the time of doing diff
 Diff file1.xml  file2.xml >> diff.txt
Fi
Done

Did you bother to look up "command substitution"?

Try

diff $(ls -t *.xml | head -1) $(ls -t *.xml | tail -1)

That will give him the oldest *.xml file in the current directory as a 1st operand; not the oldest file created today. And, it isn't clear whether the submitter wants the oldest modification time timestamp today on a file or the oldest timestamp today encoded in the files' names. Note that the specified files to be examined (according to post #1 in this thread) have names like:

         abc-<time-stamp>.xml
        xyz-<time-stamp>.xml

with varying numbers of leading <blank> characters, three lowercase alphabetic characters, a hyphen, a less-than symbol character, an unspecified time-stamp format string, and a greater-than symbol character, followed by the string .xml .

Note also that the homework question has been asked, and the submitter has not responded to that question! Until that question is answered, please refrain from providing possible solutions in this thread.