Add column that shows how old a file is


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add column that shows how old a file is
# 1  
Old 11-18-2009
Add column that shows how old a file is

I'd like to create a report that is sent to me via email. It's basically just an ls -l of a directory. However, I'd like to add a column to the end of each file listing that is a numeral - telling me at a glance how old the file is.

For example, instead of this:

$ ls -l
total 9830796
-rw-r--r-- 1 voicem voicem 2011396608 Oct 21 03:06 file1.tar
-rw-r--r-- 1 voicem voicem 2010897920 Oct 28 02:32 file2.tar
-rw-r--r-- 1 voicem voicem 2007906816 Nov 4 02:29 file3.tar


I'd like to see this:
$ ls -l
total 9830796
-rw-r--r-- 1 voicem voicem 2011396608 Oct 21 03:06 file1.tar 28
-rw-r--r-- 1 voicem voicem 2010897920 Oct 28 02:32 file2.tar 21
-rw-r--r-- 1 voicem voicem 2007906816 Nov 4 02:29 file3.tar 14

Basically, the new column is an easy way for me to eyeball how old each of those three files are (as opposed to busting out the calendar and calculating how old they are).
# 2  
Old 11-18-2009
if you have GNU date you can get the timestamp of a file and then of "now like this
Code:
TIM1=$(date -r $FILENAME %s)
TIM2=$(date %s)
let DT=TIM2-TIM1 # age of file in seconds
let AGE=DT/86400 #(seconds in a day)

In a uniline way
Code:
let AGE=($(date %s)-$(date -r $FILENAME %s))/86400

Add this where you want in the ouput, that all in a loop like
Code:
for FILENAME in *
do
...
done

# 3  
Old 11-18-2009
This may be something I can use. However, I'm not sure how to tie in the uniline code with the for loop. Is this the syntax?

Code:
for FILENAME in /tmp
do
let AGE=($(date %s)-$(date -r $FILENAME %s))/86400
done


Last edited by cdunavent; 11-18-2009 at 04:26 PM..
# 4  
Old 11-18-2009
in the expression "for FILENAME in *" the "*" will be expanded in all the names of files in the current directory. You can write "for FILE in *.txt" or anything else.
# 5  
Old 11-18-2009
The syntax:

Code:
$ cat test
#!/bin/ksh
for FILENAME in /tmp
do
let AGE=($(date %s)-$(date -r $FILENAME %s))/86400
done



The results of running the script:

$ ./test
./test: syntax error at line 4: `(' unexpected

Last edited by cdunavent; 11-18-2009 at 04:27 PM..
# 6  
Old 11-18-2009
Apologize :
  1. My script lists all the files in the directory but doesn't read a file.
  2. Command expansion by $() is newer than the backticks, try to use them.
Code:
#!/bin/ksh
for FILENAME in /home/voicem/ivr/arivra
do
let AGE=(`date %s`-`date -r $FILENAME %s`)/86400
done

# 7  
Old 11-18-2009
The syntax:

Code:
$ cat test
#!/bin/ksh
for FILENAME in /home/voicem/ivr/arivra
do
let AGE=(`date %s`-`date -r $FILENAME %s`)/86400
done


Results of running script:

$ ./test
./test: syntax error at line 4: `(' unexpected

Last edited by cdunavent; 11-18-2009 at 04:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Add new column from another file

Hi, I have 2 files. file1 contains by lines: hash:salt:id file2 contains by lines: username:hash:salt I would like to add a new coloumn (id) form file1 to file2. The new file should contains: username:hash:salt:id Note: file1 contains less rows than file2. I tried paste -d':' file1... (12 Replies)
Discussion started by: freeroute
12 Replies

2. Shell Programming and Scripting

Add new column in the file

Hi Team I have file as below empno,ename,sal 123,smith,1000 124,adams,2000 Required output: Using AWK empno,ename,sal,deptno 123,smith,1000 124,adams,2000 Thanks, Murali (5 Replies)
Discussion started by: bmk
5 Replies

3. UNIX for Dummies Questions & Answers

Add a column to a file

Hi, I have this data file that contains: 1 A 2 B 3 C 4 D 5 E 6 F 7 G 8 H 9 I I want the results to be: 1 A A 2 B A 3 C A 4 D A 5 E A 6 F A 7 G A 8 H A (8 Replies)
Discussion started by: bobo
8 Replies

4. Shell Programming and Scripting

Add a new column to file

I have file like this b,c 10,20 30,40 50,60 Now I want to add a new column a with fixed values for all the rows a,b,c 60,10,20 60,30,40 60,50,60 Please let me know how can we do this in unix. (4 Replies)
Discussion started by: weknowd
4 Replies

5. Shell Programming and Scripting

Help with add existing file name as new data column in new output file

Input File 1 cat S1.txt MI0043 2731 miR-1 Input File 2 cat S4.txt MI006 310 CiR-1 MI057 10 CiR-24 MI750 5 CiR-24 Desired Output File 1 cat S1.txt.out MI0043 2731 miR-1 S1.txt Desired Output File 2 cat S4.txt.out MI006 310 CiR-1 S4.txt (3 Replies)
Discussion started by: perl_beginner
3 Replies

6. Shell Programming and Scripting

How to add day of week at the end of each line that shows the date?

I have a file that looks like: file1: www_blank_com 20121008153552 www_blank_com 20121008162542 www_blank_com 20121009040540 www_blank_com 20121009041542 www_blank_com 20121010113548 www_blank_com 20121011113551 www_blank_com 20121012113542 I want the new file to show the day of... (3 Replies)
Discussion started by: castrojc
3 Replies

7. Shell Programming and Scripting

How to add a column from other file?

Dear all, Lets say, I've a file a.txt containing two columns, like a1 b1 .. .. .. .. and another file b.txt containg two columns, like a1 c1 .. .. .. .. I need to put c1 column from b.txt file to the a.txt file. So, the output should be a1 b1 c1 .. .. .. .. ... (4 Replies)
Discussion started by: mkg
4 Replies

8. Shell Programming and Scripting

Add column to a file

Hola, How can I add a column to a existing file?? PS: The column which should be added need to be the first column and it will be a parameter from the script. Example: 1 name1 2 name2 3 name3 4 name3 Need to add parameter $file as a first column. $file is a file name with time... (6 Replies)
Discussion started by: Olivia
6 Replies

9. Shell Programming and Scripting

Need to add letters to a column and add in a new column subtracting from another column

So I have this input 1 10327 rs112750067 T C . PASS DP=65;AF=0.208;CB=BC,NCBI 1 10469 rs117577454 C G . PASS DP=2055;AF=0.020;CB=UM,BC,NCBI 1 10492 rs55998931 C T . PASS DP=231;AF=0.167;CB=BC,NCBI 1 10583 rs58108140 G A ... (3 Replies)
Discussion started by: kellywilliams
3 Replies

10. Shell Programming and Scripting

How to add a particular column alone in a file

Hi I have file which contains 5 coulmns i need to add the fifth column value and put it in the desired location in the same column. Here is the sample file.. ashop0004 SQL- 06/14/2009 06/14/2009 00:04:28 SUM ashop0004 SQL- 06/14/2009 06/14/2009 00:00:37 ... (22 Replies)
Discussion started by: cutechaps
22 Replies
Login or Register to Ask a Question