Solved

Unix delete / remove files

Posted on 2008-06-25
12
530 Views
Last Modified: 2013-12-20
Hi All,
I like to know how I can delete all my files in a UNIX directory. I do not want to delete others. For example to list all my files I did `ls -l | grep username` but I am not sure how to delete them.

I want to delete the files that are older than 3months. It will be nice if I have an option to parameterize the time frame.

Appreciate all the time and inputs.

Thanks,
0
Comment
Question by:cutie_smily
[X]
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
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 21871098
>> I do not want to delete others. For example to list all my files I did `ls -l | grep username` but I am not sure how to delete them.

type in:

rm `ls -l | awk '{print $9}' `

Note: once you type in the command, all the files in the current dir will be gone forever, make sure it is what you want to do.

>>I want to delete the files that are older than 3months. It will be nice if I have an option to parameterize the time frame.

to can use find + rm command to do the job, eg:

find . -mtime +90 -type f -exec rm {} \;

will delete all the files older than 90 days under the current dir.
or

find /path-to/mydir mtime +90 -type f -exec rm {} \;

will delete all the files older than 90 days under /path-to/mydir
man find
to learn more details.
0
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 50 total points
ID: 21871169
>> rm `ls -l | awk '{print $9}' `
that is useless use of ls and awk. rm knows about shell wildcards and expansion. For the record, it might be as simple as

rm username*

@OP, use the find command

find /path -type f -name "*username*" -mtime +90  -exec rm {} \;
0
 
LVL 84

Expert Comment

by:ozo
ID: 21872043
ls -l | grep username | awk '{print $NF}' | xargs rm

did you expect the grep to match username on any particular part of the ls -l list?
0
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 
LVL 14

Assisted Solution

by:agriesser
agriesser earned 100 total points
ID: 21872495
If you want to delete your files (e.g. files that are owned by you in directory /dir), you can use find and xargs.

Make sure to know your userid before doing this. To get your UID, issue the command `id`, it should print something like:

tuxx@vi-edv003:~$ id
uid=1000(tuxx) gid=1000(tuxx) groups=6(disk),24(cdrom),29(audio),44(video),1000(tuxx)
tuxx@vi-edv003:

Looking at this example, your userid would be 1000.

find /dir -type f -uid 1000 -print0 | xargs rm

You said that you want to delete files older than 3 months. What do you mean with older? The data in the files has been last modified 3 months ago, the file has been accessed the last time 3 months ago or the file has been created 3 months ago?

Depending on this answer, you can then use the -mtime, -atime and -ctime parameters of find to narrow down your search.

If you want to make sure that these files get deleted immediately (read-only files will not get removed as well as errors will be printed out for several files if the permissions are incorrect), add "-f" to the rm command above.
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 21872544
>> ls -l | grep username | awk '{print $NF}' | xargs rm

this will not work for files with spaces. a simple rm *username* will do.
0
 
LVL 84

Expert Comment

by:ozo
ID: 21872648
Assuming that username is meant to match only in the filename part of the ls -l
0
 
LVL 16

Accepted Solution

by:
Hanno P.S. earned 200 total points
ID: 21872739
Most modern Unix variants (Solaris, HP-ux, etc.) allow user names as well as the user ID for find:

To delete all files (not directories) owned by user "username", use:
  # find /path/to/start/dir -type f -user username -exec rm -f {} \;

To delete all files older than (not modified in the last) "n" days, use:
  # find /path/to/start/dir -type f -mtime +n -exec rm -f {} \;
"n" has to be a number.

You can of course combine the two to delete all files older than "n" days owned by "username":
  # find /path/to/start/dir -type f -user username -mtime +n -exec rm -f {} \;

Alternatively, you can use the creation date or the date of last access instead of the date of the last modification of the files:
  # find /path/to/start/dir -type f -ctime +n -exec rm -f {} \;
  # find /path/to/start/dir -type f -atime +n -exec rm -f {} \;

To learn more, use "man find" on your specific Unix variant.
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 21872760
>> Assuming that username is meant to match only in the filename part of the ls -l
yes but assuming the file name is  "blah1 username blah2" (with spaces). $NF will be "blah2", not username
0
 

Author Comment

by:cutie_smily
ID: 21876012
I am very sorry for not being clear at all. I want to delete all the files owned by me in a specific directory with older than 90 days and I should be able to change the 90 days option to one or two years old.

Sorry again..raised the points.
0
 

Author Comment

by:cutie_smily
ID: 21876066
I think JustUNIX suggestion solves my problem.

Thanks for all the inputs.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 21892471
JustUNIX, I'm sure you must know that ctime is not the creation time, it's the change time.
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 21897543
Tintin,
you're right:
  atime       access time
  mtime      modification time
  ctime       change time (of file's inode), status change time

I was a little too quick typing my answer -- sorry if I confused anybody!
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

623 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