Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a file forcefuly as in "rm -f" or unlink a filepath from directory forcefully

I have my code as follows -

#!/usr/bin/env python
import time, glob, os, sys
from datetime import date, timedelta

try:
    dpath = sys.argv[1]+"/"
except:
    print "usage: " + sys.argv[0] +" <dir_path_to_purge_files>"
    sys.exit(1)
print dpath
day_minus_mtime = time.mktime(date.today().timetuple())
g = glob.glob(dpath+"*")
for f in g:
        try:
                if day_minus_mtime > os.path.getmtime(f):
                        os.remove(f)
                        print "Removed: "+f
        except OSError, e:
                print "Not able to Remove: "+f , e

I believe that os.remove(file) is equivalent to "rm file" in linux.

I would like to know the equivalent function for "rm -f file". Forcefully remove a file or Forcefully unlink the file path from directory.

Also the above code is trying to purge files older than today. I have a situation where the files are not deleted as it is "write-protected" due to the ownership. But when I use "rm -f" to the same file; it is getting deleted.

I think it is better to ask a question, even though it sounds stupid to yourselves

like image 471
aslamplr Avatar asked Mar 04 '12 15:03

aslamplr


People also ask

What is the command to forcefully remove a file in Linux?

The rm command in Linux removes files and directories. Note: To remove multiple files or directories using the rm command, add multiple file or directory names, separated by blank spaces. The different rm command options include: - f : Forces the removal of all files or directories.

Can you delete a file removed by the rm command?

Syntax: rm command to remove a fileWhen rm command used just with the file names, rm deletes all given files without confirmation by the user. Warning: Be careful with filenames as Unix and Linux, by default, won't prompt for confirmation before deleting files.

What happens when you remove a directory using the command rm?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

What is rm -- force?

rm command with -f , force option combined with -r as rm -rf option is used to force remove Linux directories. -r stands for recursive so that rm can remove all the sub-directories also. -r option is needed to remove a directory even if the directory is empty with no subdirectory or file in it.


1 Answers

The --force option to rm means, to ignore non existing files and never prompt, according to my man page.

The never prompt part is easy, your python remove does not prompt, right?

The ignore non existing files is also easy: you could either check, if the file exists, right before you remove it. You have a small race condition, because the file might disappear between the existence check and the remove. Or you could catch the OSError, and verify that it is thrown because the file does not exist (OSError: [Errno 2] No such file or directory...). One other reason for the OSError is, that the file you want to remove is not a file but a directory.

The force option does mo permission magic (at least on my linux), just keep in mind, that removing a file is a write operation on the directory.

like image 139
Jörg Beyer Avatar answered Oct 04 '22 00:10

Jörg Beyer