Rm command

Hello

I'm new to the Forum, so it in OS UNIX.

I ask your support so that I indicate as I can delete files of specific dates. These files are temporary and I have to debug the filesystem leaving some days.

files are generated in the following manner:

These files are generated every day and only left 15 days of backup.

I tried to delete them with the following commands, but will not let me:confused::

rm - rf 20151120 *
RM - R 20151120*.dat

Thank you for your support

Hi Andrshdz

If you add the warning/error messages you get it would be a bit more helpful.

You need to be careful with * . Depending of where you are in the filesystem and the permissions you have you could wipe out your whole system.
rm - rf 20151120 * : That deletes everything under the current directory, not just the file 20151120 You made the classic mistake of adding a space between the two terms. Also the single - floating alone has a different meaning that -rf or -R
The correct command would be rm -rf 20151120*.dat no spaces between the dash and rf or filename and * and a .dat for extra assurance

RM - R 20151120*.dat

Commands are case sensitive. rm , RM , Rm , rM would be all different commands.

1 Like

Hello, thanks for your reply.

what I want to do is delete all files of the day 20. that is why the images that I put. delete all files of the day 20, day 21 etc. so ire after leaving the most recent.

to execute the command get me this error.

6> rm -rf 20151120*.dat
No match.

Welcome to the forum, Andrshdz.

First off, a few pointers on how to post here so that you get the best possible answers:

1) Please post data in text form, not as graphics. On the one hand this makes it hard to read for people with alternative devices (like smartphones, handhelds and so on), on the other hand graphics take up a lot of bandwidth while transmitting text is very "cheap". Cut and paste your data off the screen and put it between CODE-tags, so that it will look like this:

$ print - "This is an example."
This is an example.

2) Always be as precise as possible. We cannot see what is on your screen and we do not know your system. Yes, you said "UNIX", but "UNIX" is a whole family of operating systems. State exactly which UNIX and which version (like "IBM AIX, version 7.1.3" or "Ubuntu 12.04 LTS with kernel 3.19"). If unsure, post the output of the command uname -a . This way you can skip the question in return "which OS?" and get a direct answer immediately. Also post all the error messages you get when you executed your command. UNIX is usually very clear in stating why it can't do what the user wants. We do not expect you to be able to always make sense of the error, but experts usually can - and they can tell you what went wrong if they only know what the system returned as diagnostic message.

Pheeewww, after this lengthy introduction lets get to your problem:

First, as Aia correctly pointed out, you were lucky that your command didn't work because you could have wiped your system. Notice: UNIX (every UNIX) is a system from experts for experts. There is no such thing as "Are you sure you want ....?"-second-guessing. Give the command to do something terminally fatal and if you have the right to do it the system will do it without further ado.

That doesn't mean you shouldn't try: experience is what you get in the moment right after the one you'd have needed it and most of todays experts have done something extremely silly in their career. Most of us have done so more than once. After all, dealing with the consequences of our silliness made us grow into the resourceful tricky devils we are today.

If i understood your problem correctly you want to delete files in a directory based on their date. For this you need the rm command, but you also need a tool to provide the filenames of the files to be deleted. For this there is find .

I do not want to spoil your chance of finding out how that works (UNIX is always about problem-solving so you may want to exercise and train this skill quickly), so only a general pointer:

If you want to understand how a command works use the man (manual) command. Enter:

$ man find

to get a very thorough explanation of how find works. You will eventually come to the -exec -clause of find and if you search this forum you will find several examples on how to use it. Replace the possibly harmful rm -command with a harmless echo for testing purposes. echo will just display everything following it so that nothing is done. Only after thoroughly testing what you have written replace the echo with rm again.

If you still have questions or get stuck: don't be shy and ask us. We will be glad to help you along as long as we do not have the impression of our help going to waste and you are trying to learn from what we show you.

I hope this helps.

bakunin

2 Likes

I'm a bit surprised about this message. On the systems I know (admittedly a limited experience), it would consistently look like

rm -r 20151120*.dat
rm: 20151120*.dat: No such file or directory

On top, the 6> looks like a continuation prompt (PS2 in bash ).

Are you sure you're executing /bin/rm ? Or is your rm aliased?
Are you sure you're in the correct directory holding the 20151120... files when executing the command?

1 Like

Hello, Many thanks to all.

@bakunin take your advice to my publications.

I have OS is AIX HOSTNAME 1 7 000DEE2BD400, I could delete the files of the day 20 with the following command:

rm -rf *20151120*.dat

correct this? I worked and deleted all the files that begin with the name "20151120" and have extension. dat.

I'll be visiting much this forum and hope find manuals and learn a lot.

Thank you

That's strange and doesn't match your listings in post#1. Please post the result of

ls -lb

(if available in your system) limited to a few lines.

OK, your OS is IBMs AIX and the version you run is 7.1. Good!

Yes, this is - more or less - correct. You won't need the leading "*" and you won't need the "r", so your command should read:

rm -f 20151120*.dat

but this is a detail. As long as there are no directories named "*20151120*.dat" and no files named "<something>20151120*.dat" it will make no difference. Notice, though, that AIX (like all other UNIXes) does NOT have an "undelete" feature, therefore one tends to be very careful with deletion. It is easier to execute rm a second time because one command didn't delete all the files than it is to restore an accidentally deleted file from the backup (you DO HAVE backups, don't you?).

But by doing it this way you base your solution on the filename instead of the filedate. Have a look at this example where we want to delete files from Nov 22nd:

$ ls -l
total 20
-rw-rw-rw-  2 bakunin staff 4096 Nov 21 10:00 somefile
-rw-rw-rw-  2 bakunin staff 4096 Nov 22 23:10 20151122foo.dat
-rw-rw-rw-  2 bakunin staff 4096 Nov 22 23:10 myfile
-rw-rw-rw-  2 bakunin staff 4096 Nov 23 10:00 otherfile

Using your solution rm -f 20151122*.dat would get you the first file, because it is named "20151122<something>.dat" and actually its file date says it is from the Nov 22nd too. The second file would not be caught by your solution because it does not follow this naming convention. Still, its filedate is "Nov 22 23:10" and this could be utilized too. Using the find -command i suggested would allow to base the solution on the filedate instead of the name. It would catch the second and third file while leaving the first and the fourth file.

(Hint: you will either need the -newer clause of find or the -ctime clause.)

I hope this helps.

bakunin

In post #2 I did not want to deviate from what you had already posted, so there was no confusion in what I was trying to point out. Now, I think it would be good to expand a bit more upon what you have experienced, already.

In reality, you only need rm 20151120*.dat , however this might prompt you to confirm for each file it tries to delete. Adding the -f (force) would suppress any prompting and it will ignore if it doesn't find any file matching that pattern.
The -r is intended for directory. It will descend any directory that matches the pattern and it will delete every files in it and the directory afterward. Thus, you do not want to use it unless necessary.
Otherwise it might delete a directory named 20151120.dat or one that matches the pattern.

Hello,
why don't you delete all files older than ? by using crontab ?

For example :

find /Path -name "tmp*" -mtime +14 -exec rm -f {} \;

(Delete all tmp files older than 14 days )

Regards

If you look up the thread this is roundabout what i tried the threads o/p to get to. The only problem is that the threads o/p wanted to delete filex exactly X days old, not older than X days. It is also not clear if s/he wants to base it on creatime time ( -ctime ), modifying time ( -mtime ), or last access time ( -atime ). In any case you need to concatenate 2 such clauses (i.e \( -mtime +14 \) -a \( ! -mtime +15 \) ) or 2 reference files to base a -newer -clause upon (i.e. \( -newer /path/to/fileA \) -a \( ! -newer /path/to/fileB \) ).

I hope this helps.

bakunin

The OP clearly was referring to the name of the files when mentioning any indication of day. The introduction of the find command in this thread has become an artifact of ignoring this indication.

Actually he wasn't:

To "leave some days" means - IMHO - to leave the files generated at these days, which means a data-based selection. That the file names coincide with the file dates (which you can see in his original listing above) might have brought him to use the former instead of the date as a selection criterium, but since this is a forum about learning something i pointed him to find , which allows both: base the file selection upon names or dates (or even both, if desired).

How this - which was clearly pointed out, btw. - is "ignoring" anything, much less an indication that never has been given in first place, eludes me.

bakunin

Hi, bakunin

It is not quite certain to me, if your latest post is an invitation to a follow up on my part. If so, do not get offended if I do not wish to discuss with you your latest statement. Mine was intended for the general reader.

Take care

No match. is the fatal error handler in csh/tcsh. You can switch to the standard shell behavior with

set nonomatch

To be honest, I have not understood your problem. Maybe a range can help you?

rm -f 2015112[0-2]*.dat