Solved

Unix delete / remove files

Posted on 2008-06-25
12
510 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
  • 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Accepted Solution

by:
Hanno Schröder 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 Schröder
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now