Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

Unix delete / remove files

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
cutie_smily
Asked:
cutie_smily
  • 3
  • 2
  • 2
  • +4
3 Solutions
 
yuzhCommented:
>> 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
 
ghostdog74Commented:
>> 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
 
ozoCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
agriesserCommented:
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
 
ghostdog74Commented:
>> ls -l | grep username | awk '{print $NF}' | xargs rm

this will not work for files with spaces. a simple rm *username* will do.
0
 
ozoCommented:
Assuming that username is meant to match only in the filename part of the ls -l
0
 
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
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
 
ghostdog74Commented:
>> 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
 
cutie_smilyAuthor Commented:
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
 
cutie_smilyAuthor Commented:
I think JustUNIX suggestion solves my problem.

Thanks for all the inputs.
0
 
TintinCommented:
JustUNIX, I'm sure you must know that ctime is not the creation time, it's the change time.
0
 
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now