Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Linux find command script

Posted on 2006-11-21
11
399 Views
Last Modified: 2010-05-18
I have 200 users that place files in the /tmp directory. These users all have a username that starts with user1. So for example:

user101
user102
.....
user130
user131
.... etc

There are other users that also write files to this directory, so I only want to this script to run on files owned by users who's username starts with user1.

The files user1* users create all start with DBF. I need a script that will check to see if any DBF* files exist by the same user in the /tmp directory, and delete all but the most recent one. So if user123 has three files: DBF1A modified at 12:55, DBF1B modified at 12:57, and DBF1C modofied at 12:59, the script needs to delete all except the DBF1C file because it is newest.

What I have so far is this (thanks to an EE expert "dnb"):

find /tmp -name 'DBF*' -printf '%u:%f\n' | grep '^user1' | cut -d: -f2- | xargs rm

The problem is that this script will delete ALL the DBF files that it finds. How can I make it keep only the most recent version?
0
Comment
Question by:bfilipek
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 17990137
Do the script is about too keep newest DBF for each user? I assume so.

grep "^user1" /etc/passwd | while read line; do # do it for each user from /etc/passwd
 UUID=$( echo -n "$line" | cut -d: -f3 )
 find /tmp -type f -uid $UUID -name 'DBF*' -printf '%T@:%p\n' | sort -n | head -n -1 | cut -d: -f2-
done

If it's fine, add '| xargs rm -fv' at the end of already long line
0
 

Author Comment

by:bfilipek
ID: 17990954
It comes back with a bunch of lines that say:

head: -1: invalid number of lines
head: -1: invalid number of lines
head: -1: invalid number of lines
head: -1: invalid number of lines
0
 
LVL 58

Accepted Solution

by:
amit_g earned 500 total points
ID: 17991033
Run

ls -l -t /tmp/DBF* | grep ' user1' | awk 'NR > 2 {print NR $9}'

does it give correct result? If so change it to

ls -l -t /tmp/DBF* | grep ' user1' | awk 'NR > 2 {print NR $9}' | xargs rm -f

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 43

Expert Comment

by:ravenpl
ID: 17993863
> head: -1: invalid number of lines
What linux do You use where head supports no '-n -1' ??
0
 

Author Comment

by:bfilipek
ID: 17999488
amit_q,
Your script worked great. I just had to change the NR> 2 to NR > 1 to return the correct results.

Now I have another similar question posted here: http://www.experts-exchange.com/Operating_Systems/Linux/Q_22070085.html 

0
 

Author Comment

by:bfilipek
ID: 17999569
amit_q,

One last thing on this script. I do not want it to delete a DBF* file if it is the only one for the user. Most users have multiple DBF files, but a few only have one. Right now the script will delete the one. How can I prevent this?
0
 
LVL 58

Expert Comment

by:amit_g
ID: 17999739
I think the one you are using now is not going to work. Try this one...

ls -l -t /tmp/DBF* | grep ' user1' | awk '{if (Store[$3] != 1) {Store[$3] = 1} else {print $9}}'

If that gives correct results, add | xargs rm -f to the command.
0
 
LVL 58

Expert Comment

by:amit_g
ID: 17999744
you might want to test that by

ls -l -t /tmp/DBF* | grep ' user1' | awk '{if (Store[$3] != 1) {Store[$3] = 1} else {print}}'

So that you know what is happening.
0
 

Author Comment

by:bfilipek
ID: 17999883
Woked perfect.

One LAST thing, I swear :). Instead of deleting the files, can I move them to a subdirectoy at /tmp/move? So instead of:

| xargs rm -f

would I use something like

| xargs mv * /tmp/move/
0
 
LVL 84

Expert Comment

by:ozo
ID: 17999929
xargs -J % mv % /tmp/move/
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18000679
Thanks ozo. BTW, just wondering, how did you get to this question as this was already closded. You can't possibly be reading each and every posted question weather open or closed :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can't see Ubuntu desktop on KVM switch unless I reboot the PC 22 83
Can't ping New Linux Servers 40 89
winscp 000webhost.com 6 73
LogmeIn using Linux Ubuntu 16.04 6 61
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question