Rm command

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Rm command
# 8  
Old 12-20-2015
Quote:
Originally Posted by Andrshdz
OS is AIX HOSTNAME 1 7
OK, your OS is IBMs AIX and the version you run is 7.1. Good!


Quote:
Originally Posted by Andrshdz
I could delete the files of the day 20 with the following command:

Code:
rm -rf *20151120*.dat

correct this? I worked and deleted all the files that begin with the name "20151120" and have extension. dat.
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:
Code:
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:

Code:
$ 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
# 9  
Old 12-20-2015
Quote:
Originally Posted by Andrshdz
Code:
rm -rf *20151120*.dat

correct this? I worked and deleted all the files that begin with the name "20151120" and have extension. dat.
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.
# 10  
Old 12-21-2015
Why not delete all filses older than .... ?

Quote:
Originally Posted by Andrshdz
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.

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

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

For example :

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

(Delete all tmp files older than 14 days )

Regards

Moderator's Comments:
Mod Comment edit by bakunin: Please use CODE-tags like the ones i edited in for you. Thank you.

Last edited by bakunin; 12-21-2015 at 11:12 AM..
# 11  
Old 12-21-2015
Quote:
Originally Posted by bdittmar
Code:
find /Path -name "tmp*" -mtime +14 -exec rm -f {} \;

(Delete all tmp files older than 14 days )
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
# 12  
Old 12-21-2015
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.
# 13  
Old 12-21-2015
Quote:
Originally Posted by Aia
The OP clearly was referring to the name of the files when mentioning any indication of day.
Actually he wasn't:

Quote:
Originally Posted by Andrshdz
These files are temporary and I have to debug the filesystem leaving some days.
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).

Quote:
Originally Posted by Aia
The introduction of the find command in this thread has become an artifact of ignoring this indication.
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
# 14  
Old 12-21-2015
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Another one line command where I'd like to determine if Ubuntu or Red Hat when running command

Hello Forum, I'm making very good progress on my report thanks to the very helpful people on this forum. I've been able to successfully create my report for my Red Hat servers. But I do have a few ubuntu servers in the mix and I'd like to capture some data from them when an ssh connection is... (8 Replies)
Discussion started by: greavette
8 Replies

2. AIX

I'm facing problem with rpm command, when running the command and appears this error:

exec(): 0509-036 Cannot load program /usr/opt/freeware/bin/rpm because of the following errors: 0509-022 Cannot load module /opt/freeware/lib/libintl.a(libintl.so.1). 0509-150 Dependent module /opt/freeware/lib/libiconv.a(shr4.o) could not be loaded. 0509-152 Member... (4 Replies)
Discussion started by: Ohmkar
4 Replies

3. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

5. UNIX for Dummies Questions & Answers

passing command output from one command to the next command in cshell

HI Guys, I hope you are well. I am trying to write a script that gets executed every time i open a shell (cshell). I have two questions about that 1) I need to enter these commands $ echo $DISPLAY $ setenv $DISPLAY output_of_echo_$display_command How can i write a... (2 Replies)
Discussion started by: kaaliakahn
2 Replies

6. UNIX for Advanced & Expert Users

unix command : how to insert text at the cursor location via command line?

Hi, Well my title isn't very clear I think. So to understand my goal: I have a script "test1" #!/bin/bash xvkbd -text blabla with xbindkeys, I bind F5 key in order it runs my test1 script So when I press F5, test1 runs. I'm under Emacs/Vi and I press F5 in order to have "blabla" be... (0 Replies)
Discussion started by: xib.be
0 Replies

7. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

8. AIX

AIX:Command to get netaddress/subnet address command in IPv4/IP6

AIX:Command to get netaddress/subnet address command in IPv4/IP6 Can anybody help us with a command to retrieve netaddress/subnet address command in IPv4/IP6 on aix machine. net/subnet address is in the format 172.16.212.0(signifies all 255 machines in an IPv4 network) (2 Replies)
Discussion started by: rookie8278
2 Replies

9. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

10. SuSE

inconsistent ls command display at the command prompt & running as a cron job

Sir, I using the following commands in a file (part of a bigger script): #!/bin/bash cd /opt/oracle/bin ls -lt | tail -1 | awk '{print $6}' >> /tmp/ramb.out If I run this from the command prompt the result is: 2007-05-16 if I run it as a cron job then... (5 Replies)
Discussion started by: rajranibl
5 Replies
Login or Register to Ask a Question