Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using awk to display elements of a directory listing 1

Status
Not open for further replies.

madasafish

Technical User
Jul 18, 2006
78
TH
I have a directory listing with files that keep updating and I need to check the last modify/access time of the directory plus the total number of files in that directory.Keeping the newest/latest modified directory listing at the top.

For example...
[root@instructor home]# ls -ltR mydir*
mydir2:
total 0
-rw-r--r--. 1 root root 0 Aug 11 06:11 file21
-rw-r--r--. 1 root root 0 Aug 11 06:10 file2

mydir3:
total 0
-rw-r--r--. 1 root root 0 Aug 11 06:10 file31
-rw-r--r--. 1 root root 0 Aug 11 06:10 file3

mydir1:
total 0
-rw-r--r--. 1 root root 0 Aug 11 06:10 file12
-rw-r--r--. 1 root root 0 Aug 11 06:09 file1

I need to output
DIRNAME MONTH DAY TIME No_FILES
mydir2 Aug 11 06:11 2
mydir3 Aug 11 06:10 2
mydir1 Aug 11 06:10 2

The following simple command gets the first 4 outputs..

ls -ltd mydir* |awk '{print $9,$6,$7,$8}'

but I am struggling to get the FILETOT. I know I can pipe the first command into "grep -v" to remove the "total" lines but it would be more elegant to have it all done in awk.

Any help appreciated,

Thanks in advance,

Madasafish
 
Like this ?
ls -ltd mydir* |awk '[!]NF>8[/!]{print $9,$6,$7,$8}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV,

I dont think I explaned it properly, when I use your syntax I get the same result as I posted above by using the following command.

ls -ltd mydir* |awk '{print $9,$6,$7,$8}'

As you can see you are printing four arguments. What is missing is the 5th argument which is the total number of files within that directory.
Note: there are no futher subdirectory's below the "mydir*".

I believe the first part would be...

ls -ltR | awk ............etc

displays 5 arguments. the last argument is the total files within that directory.

I hope that makes sense,

thanks in advance,

Madasafish

 
A starting point:
ls -ltd mydir* |awk '{"ls "$9"|wc -l"|getline f;print $9,$6,$7,$8,f}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank-you PHV.

I am sure that would work but it is not exactly what I was looking for. Maybe I should of been more specific. If you did....

ls -ltR > myfile

Now use awk to work on "myfile" to strip out the "total 0"'s and give the results I posted above.

Thanks again.

madasafish

 
should have", not "should of". Sorry, pet hate. :)

Try this?

Code:
awk '
        [olive]function[/olive] printem() { [b]print[/b] dir, latest, c; dir=latest=c=[red]"[/red][purple][/purple][red]"[/red] }
        [green]BEGIN[/green] { [b]print[/b] [red]"[/red][purple]DIRNAME MONTH DAY TIME No_FILES[/purple][red]"[/red] }
        [blue]NF[/blue]==1 { dir=[blue]$1[/blue]; [b]sub[/b]([green]/:$/[/green],[red]"[/red][purple][/purple][red]"[/red],dir) }
        [blue]NF[/blue]>8 && latest==[red]"[/red][purple][/purple][red]"[/red] { latest=[blue]$6[/blue] [blue]OFS[/blue] [blue]$7[/blue] [blue]OFS[/blue] [blue]$8[/blue] }
        [blue]NF[/blue]>8 { c++ }
        [green]/^$/[/green] { printem() }
        [green]END[/green] { printem() }
' myfile

You may want to change the prints to printfs if you want tidy columns...

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top