Need Help with scripting FINDING USER Files and output to file

joejtm260

New Member
Joined
Nov 15, 2023
Messages
2
Reaction score
0
Credits
28
All - 1st post. Looking forward to posting more.

I am trying to build a script that will go out to the users directories and find files that are older than 30 days and then return the following information:
- owner
  • date of file
  • and the path to that file
  • owner

I want to be able to pull and write that information to text file.

I have this line of code - find . -name "*" -mtime -30 -exec realpath {} \; that gets me part of the information needed. I am not clear on how to return the list of parameters and write to a file.

Appreciate any help or comments.

Joe
 


Code:
[flip@flop ~]$ find . -name "*" -mtime +30 -print | xargs realpath | xargs ls -al
-rw-r--r-- 1 flip flip   719 Aug 11 17:10 /home/flip/lemon/file-exp/anoth/file2
-rw-r--r-- 1 flip flip    40 Sep 23 18:40 /home/flip/lemon/file-exp/anoth/file3
-rw-r--r-- 1 flip flip     9 Oct 10 16:52 /home/flip/lemon/file-exp/anoth/file4
-rw-r--r-- 1 flip flip    44 Oct 14 08:16 /home/flip/lemon/file-exp/anoth/filea
-rw-r--r-- 1 flip flip    44 Oct 14 08:17 /home/flip/lemon/file-exp/anoth/fileb
-rw-r--r-- 1 flip flip    18 Oct 14 09:20 /home/flip/lemon/file-exp/anoth/filec
To write to a file, redirect output with > to file, e.g.:
Code:
find . -name "*" -mtime +30 -print | xargs realpath | xargs ls -al > filename.txt
 
Last edited:
Moving this to Command Line, where scripting inquiries are covered.

Welcome @joejtm260

Chris Turner
wizardfromoz
 
@wizardfromoz thank you for moving to appropriate location.

@osprey - Thank you for the replay and does give me what I was looking for. The thing that I noticed was it doesn't return files that are in this format - file name.txt. So if the user created a filename with spaces in it are not returned in the search. Also the code that you posted is returning a row to a dir within. Example below. I tried to modify the find you sent but was not able get it to return just the file information

Return if there is a directory below users dir.

/opt/sas/SAS_env/users/jpjoyce/TEST_ARTIFACTS/LOG:
total 164
drwxr-xr-x 2 jpjoyce UX_R2005288 84 Jul 11 18:54 .
drwxr-xr-x 3 jpjoyce UX_R2005288 297 Aug 28 07:46 ..
-rw-r--r-- 1 jpjoyce UX_R2005288 89639 Jun 22 12:32 CR_SALES_RPT_BASE.log
-rw-r--r-- 1 jpjoyce UX_R2005288 75393 Jul 2 20:54 CR_SECTOR_RPT_BASE_202208.log

Just looking to return:

-rw-r--r-- 1 jpjoyce UX_R2005288 89639 Jun 22 12:32 CR_SALES_RPT_BASE.log
-rw-r--r-- 1 jpjoyce UX_R2005288 75393 Jul 2 20:54 CR_SECTOR_RPT_BASE_202208.log
 
With the swith ' -type f' on find?
Eg:
find . -type f -name "*" -mtime +30 -print |......
 
To print just the name of the files:

Code:
[flip@flop ~ ]$ find . -name "*" -mtime +30 -print | xargs realpath | xargs ls -al | awk '{print $9}'
/home/flip/lemon/file-exp/anoth/file2
/home/flip/lemon/file-exp/anoth/file3
/home/flip/lemon/file-exp/anoth/file4
/home/flip/lemon/file-exp/anoth/filea
/home/flip/lemon/file-exp/anoth/fileb
/home/flip/lemon/file-exp/anoth/filec

To convert spaces to underscores:

Code:
[flip@flop ~]$ ls
'file 1'  'file 2'  'file 3'
[flip@flop ~]$ find . -name "* *" -type f | rename 's/ /_/g'
[flip@flop]$ ls
file_1  file_2  file_3

To get the form: -rw-r--r-- 1 flip flip 719 Aug 11 17:10 file_2, with underscores, just run the space-to-underscore converting command, and then: ls -l or ls -Gl.
 
Last edited:
To write to a file, redirect output with > to file, e.g.:
Code:
find . -name "*" -mtime +30 -print | xargs realpath | xargs ls -al > filename.txt
Though it should be noted that this solution will not work if the path contains spaces. When you get to the ls command, you'll get an error for each file with a space in it's path/filename.

I'd recommend using finds -exec option to run ls -al and then pipe the output of ls to awk to filter out the fields you want.
e.g.:
Bash:
find $HOME -type f -mtime +30 -exec ls -al {} + | awk '{print $6 " " $7 " " $8 " " $9 " " $3}' > /path/to/outputfile
NOTE: Where /path/to/outputfile is the path to the output file you want to create.

Also - I'm on my phone ATM, so I'm unable to test the above command. But if I remember the order in which the fields are output by ls, it should be more or less correct. 6, 7, and 8 should be the date, 9 should be the path/filename and 3 should be the owner. But I could be wrong. If the output file contains the wrong fields - you'll just need to change the field numbers.
 

Members online


Top