1828270 Members
3442 Online
109975 Solutions
New Discussion

Rm commands in HP-UX

 
Andy Cole_1
Frequent Advisor

Rm commands in HP-UX

Hi, i am new to unix so pardon me if i ask those silly and stupid qns.

May i know how to remove files on a specific date? For example, i want to remove all the files in the directory that is dated 2 July. What is the command to used?

Becos i found out that when i do a ls on the root directory. I saw quite a number of garbage files on that directory on that specific date. Thanks
13 REPLIES 13
John Kittel
Trusted Contributor

Re: Rm commands in HP-UX

Since you say you are new to unix, I strongly advise you not try to remove all files with specific date from root directory, especially with a single command. Perhaps in some circumstances it could and should be done, but you'd want to be very sure you know exactly what you are doing.

For one thing, the dates you're probably seeing with the ls command are modification times, and depending on how your system has been managed in the past, up to this time, there could well be legitimate, necessary files in root dir with modification times the same as any "garbage" files.

Start with reading the man page for the rm command. The probably, if you're sure you want to remove some files, remove each of them individually. Single commands to do a lot of things are nice, elegant, but brute force has its place, and this may be one of them.
Pete Randall
Outstanding Contributor

Re: Rm commands in HP-UX

Andy,

You could use the find and touch commands thusly:

touch 20040701 ref_file1
touch 20040703 ref_file2
find /start_dir -newer ref_file1 ! -newer ref_file2 -exec rm {} \;


Pete

Pete
A. Clay Stephenson
Acclaimed Contributor

Re: Rm commands in HP-UX

Here is one technique that uses the find command.

First create a pair of reference files that will store you modification time ranges (the first and last seconds of 2-Jul-2004):

touch -t 200407020000.00 /var/tmp/file1
touch -t 200407022359.59 /var/tmp/file2

Now we want to remove all regular files newer than file1 and not newer than file2.

cd to desired directory
find . -type f \( -newer /var/tmp/file1 -a ! -newer /var/tmp/file2 \) -exec rm {} \;

Man touch and find for details. I would first test this with a "safe" command like "-exec ls -l {} \;" in lieu of the "-exec rm {} \;".

You could also simply use the -mtime find option as well but this is a more general technique.

If it ain't broke, I can fix that.
YoungHwan, Ko
Valued Contributor

Re: Rm commands in HP-UX

#find / -mtime +0 | awk '{print "rm " $1}' | sh

This command are useful to delete the file that creates within 24 hours.
Todd McDaniel_1
Honored Contributor

Re: Rm commands in HP-UX

/bonk Andy... welcome to Unix.

My suggestion when workingn with RM is to make a dir under your home directory for testing...

cd ~myhome
mkdir test
cd test

Then make some files and modify the date stamps and play with it there before you do it live...

also the use of a reference file is a very good idea... I have used that to perform tar backups of data when the time matters, say after a recent backup but you want to do work and need to make sure that ALL data is backed up and is absolutely current since the last automated backup.

Hope this helps.
Unix, the other white meat.
John Kittel
Trusted Contributor

Re: Rm commands in HP-UX

Pete, Clay, you guys are obviously way more expert than me, so excuse me for questioning, but aren't you going to have this poor person removing every file on his entire system that was modified on July 2? And don't you think there could be something useful that was modified then? Sorry if I'm off base here, just wondering...

- John
Patrick Wallek
Honored Contributor

Re: Rm commands in HP-UX

John,

If you'll notice both Pete's and Clay's find command, you will see that neither of them says to start the find in the / directory. Pete says so do 'find /startdir' and Clay says to do 'find .'. With Clay's command you are starting from whatever directory you are in. If you happen to be in /, then yes that could be a problem. With Pete's command it will only be a problem if you specify the wrong directory.

Yes, it could be a hard lesson if there were critical files modified on July 2.

My recommendation would be to execute each of their commands first with an 'll -d' rather than the 'rm' in the exec portion of the find command. That way you can see exactly what you are going to rm.
John Kittel
Trusted Contributor

Re: Rm commands in HP-UX

ok Patrick, but the original question says the files he wants to remove are in /.
Patrick Wallek
Honored Contributor

Re: Rm commands in HP-UX

OK, yes it IS possible that the results could be catastrophic.

That's why I recommend first substituting 'll -d' for rm in the find command.

This can also be a good lesson in being VERY VERY careful about what you do where on a unix machine.

Andy Cole_1
Frequent Advisor

Re: Rm commands in HP-UX

thks for all your help. Will go and try out. My server is a new test server and not sure how come there is a number of garbage files, example %$*| in my root directory. So i will use a touch and compare then remove away from it.

Btw, how to check for the version of the OS i am in. Mine is HP UX 11.0
Patrick Wallek
Honored Contributor

Re: Rm commands in HP-UX

Use the uname command to check your version.

# uname -a

Look for the string like B.11.11 or something similar.
Muthukumar_5
Honored Contributor

Re: Rm commands in HP-UX

Hai,

All experts gave their response.

1. File removal.
================
Create the two files to indicate the end of july 1 and starting of july2 with touch command.

File's time may be differed with creation time, access time or modified time.

find -type f -newer !-newer -exec rm {}\;

It will check only the files not directory or char or device files. If you want to remove only in one directory,use the ll with awk command as

rm -f `ll | awk '{
if ($6=="Jun")
if ($7==18)
printf "%s ",$9
}'`

2. O/S version check:
=====================

uname -v it will give the o/s version.
We can get the system informations with uname -a option.

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
R. Allan Hicks
Trusted Contributor

Re: Rm commands in HP-UX

One thing that you might consider is a two step removal.

1.) Copy the directory off to tape. (First of computer science -- Never do anything you can't undo)

2.) Create a directory called trash or any other meaningful name.

3.) use any of the search techniques to identify the files.

example ll *|awk '{if (($6=='Jul')&&($7==2)){printf "mv %s trash/ \n"}}'>clean_up

4.) chmod +x clean_up

5. ./clean_up

The result is that the files are moved into the trash directory.

Now, look at the trash directory _very_ _very_ closely. Are you absolutely positively sure that you want to remove those files?

If no, put the ones you want to keep back on the source directory.

If yes, do a a ls on the current directory.

Does it list the files you really really want to delete?

Are you really really sure?

BTW rm -i is usually a good way unless there are hundreds of files.

Then use the rm command. Just know that once a file in UNIX is gone, it is really gone.
Unless your are a spy agency chances are pretty good that it will not come back.

cd out of the trash directory and remove it

cd ../
rmdir trash


--Good Luck be careful
No warranty is expressed or implied.
"Only he who attempts the absurd is capable of achieving the impossible