How to safely rm/mv files/directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to safely rm/mv files/directory
# 1  
Old 09-17-2011
Data How to safely rm/mv files/directory

Hi all,

Am writing a script that does a rm/mv if a file exist, however, in one scenario, one of the variables which is supposed to a variable for a directory is undefined/blank so instead of the variable resolving to /tmp/logfile.dmp, it resolves instead to / so the rm translates to a rm / instead of rm /tmp/logfile.dmp.

Luckily I guess, the rm fails because / is a directory. In the same instance, a similar thing happen to one of the mv command, i.e. it translates to mv /, and luckily the mv gives Device or resource busy.

To avoid really causing a problem, I temporarily disabled all rm and mv in my script.

Can anyone please advise if there is a safe way to run a rm or mv to avoid removing /mv'ing the wrong file or there is no such way or doing so and I will just have to make sure that whatever I am trying to remove or mv resolves to the right file that I want to remove?

Any advise much appreciated. Thanks in advance.
# 2  
Old 09-17-2011
If you know you are deleting files, you can check if it a file and then delete. Something like this

Code:
test -f "$file_name" && rm -f "$file_name"

May be you should hard code a check for "/".

Another option is use to check the emptiness of the variable before using it
Say, $var is supposed to have the file name, tmp.log but it was not there, so you can check the variable using

Code:
test ! -z "$var" && echo "Okay I am not empty : delete $var"

--ahamed
# 3  
Old 09-17-2011
This 1st test is not so useful ! as rm cannot remove directory without options, and with -f option even if the directory is empty.

But the idea of testing a variable for non empty is good, that can be followed.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Safely Remove Files with Special Chars

Hey Guys, I'm swamped writing code for the forums: Could someone write a script or command line to safely delete files with special chars in filenames from a directory: Example: -rw-r--r-- 1 root root 148 Apr 30 23:00 ?xA?? -rw-r--r-- 1 root root 148... (8 Replies)
Discussion started by: Neo
8 Replies

2. Shell Programming and Scripting

Directory containing files,Print names of the files in the directory that are exactly same content.

Given a directory containing say a few thousand files, please output a list of all the names of the files in the directory that are exactly the same, i.e. have the same contents. func(a_directory_name) output -> {“matches”: , ... ]} e.g. func(“/home/my/files”) where the directory... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

3. Red Hat

Can all files under /tmp be safely removed

I wanted to know whether all files under /tmp can be safely removed. I guess that /tmp may also have temporary files for applications currently being worked on, so at the most those applications may just shut down. I hope that my question is clear whether all files under /tmp can be safely... (5 Replies)
Discussion started by: RHCE
5 Replies

4. Solaris

need to safely reboot to cdrom

I am using: reboot -- cdrom However I'm afraid of causing file system errors/corruption. I've seen many threads say that init 6 is safer, but I need to get to CDROM. Is there a command that is as safe as init, but can boot to cdrom, or should I not worry so much about the reboot... (5 Replies)
Discussion started by: lcoreyl
5 Replies

5. Shell Programming and Scripting

Safely parsing parameters

I have a string like root=/dev/sda3 noacpi foo "Baz mumble" which I would like to separate into tokens like a shell does. This would be easily done with eval but that would open a security hole big enough to drop a cow through, injecting arbitrary code would be easy as pie. How can I parse this... (15 Replies)
Discussion started by: Corona688
15 Replies

6. Solaris

How to remove soft link safely

Greetings, I need some help performing a system admin function that I have been tasked with. The request seems simple enough, but my feeling is that it might be more complicated than it seems. Here is what i've been tasked with: SunOS 5.10 Generic_142900-15 sun4u sparc SUNW,SPARC-Enterprise... (3 Replies)
Discussion started by: Harleyrci
3 Replies

7. Solaris

How to safely copy full filesystems with large files (10Gb files)

Hello everyone. Need some help copying a filesystem. The situation is this: I have an oracle DB mounted on /u01 and need to copy it to /u02. /u01 is 500 Gb and /u02 is 300 Gb. The size used on /u01 is 187 Gb. This is running on solaris 9 and both filesystems are UFS. I have tried to do it using:... (14 Replies)
Discussion started by: dragonov7
14 Replies

8. AIX

How to Safely delete oracle db datafiles in AIX?

We have an oracle Database on AIX box. We want to delete the database safely. How can scrubb the datafile prior to delete on AIX? (1 Reply)
Discussion started by: johnveslin
1 Replies

9. UNIX for Advanced & Expert Users

Can I safely kill vdump?

Sceduled backups with vdump have been delayed as a mounted system had crashed while I was away for 2 weeks. Now there are 5 simultaneous vdumps running very slowly. The full system backup usually takes a whole weekend. Can I safely kill these? (I will have to live without a backup untill next... (4 Replies)
Discussion started by: nickt
4 Replies
Login or Register to Ask a Question