What Is The Safest Way to Empty a Directory in *Nix

What is the safest way to empty a directory in *nix?

The safest way is to sit on your hands before pressing Enter.

That aside, you could create an alias like this one (for Bash)


alias rm="pwd;read;rm"

That will show you your directory, wait for an enter press and then remove what you specified with the proper flags. You can cancel by pressing ^C instead of Enter.

How to delete the contents of a folder?

import os, shutil
folder = '/path/to/folder'
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))

Delete non git directory in git bash, windows

rmdir will not work if directory is empty

Try

  rm -rf yo-2

git-bash is a Linux like shell

How do I remove/delete a folder that is not empty?

import shutil

shutil.rmtree('/folder_name')

Standard Library Reference: shutil.rmtree.

By design, rmtree fails on folder trees containing read-only files. If you want the folder to be deleted regardless of whether it contains read-only files, then use

shutil.rmtree('/folder_name', ignore_errors=True)

How to delete all files in a folder, but not delete the folder using NIX standard libraries?

In C/C++, you could do:

system("exec rm -r /tmp/*")

In Bash, you could do:

rm -r /tmp/*

This will delete everything inside /tmp, but not /tmp itself.

rm -rf equivalent for Windows?

RMDIR or RD if you are using the classic Command Prompt (cmd.exe):

rd /s /q "path"

RMDIR [/S] [/Q] [drive:]path

RD [/S] [/Q] [drive:]path

/S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.

/Q Quiet mode, do not ask if ok to remove a directory tree with /S

If you are using PowerShell you can use Remove-Item (which is aliased to del, erase, rd, ri, rm and rmdir) and takes a -Recurse argument that can be shorted to -r

rd -r "path"

Tried to delete a package from /nix/store now system has errors, how to fix?

Try running

 nix-store --verify --check-contents --repair

From the manpages:

OPERATION --VERIFY
Synopsis
nix-store --verify [--check-contents] [--repair]

Description
The operation --verify verifies the internal consistency of the Nix database, and the
consistency between the Nix database and the Nix store. Any inconsistencies
encountered are automatically repaired. Inconsistencies are generally the result of
the Nix store or database being modified by non-Nix tools, or of bugs in Nix itself.

This operation has the following options:

--check-contents
Checks that the contents of every valid store path has not been altered by
computing a SHA-256 hash of the contents and comparing it with the hash stored in
the Nix database at build time. Paths that have been modified are printed out.
For large stores, --check-contents is obviously quite slow.

--repair
If any valid path is missing from the store, or (if --check-contents is given)
the contents of a valid path has been modified, then try to repair the path by
redownloading it. See nix-store --repair-path for details.

NB. I recommend reading the manpages yourself with man nix-store to ensure this is what you want before running this.

NB.2 Due to the nature of the operations, a lot has to be checked―this operation will take a while. For my 11 GiB /nix/store, this ran for 4m13s.


Addendum. In future, when you want to delete a package from the nix store manually, use

nix-store --delete /nix/store/[what you want to delete]

instead.

What characters are forbidden in Windows and Linux directory names?

A “comprehensive guide” of forbidden filename characters is not going to work on Windows because it reserves filenames as well as characters. Yes, characters like
* " ? and others are forbidden, but there are a infinite number of names composed only of valid characters that are forbidden. For example, spaces and dots are valid filename characters, but names composed only of those characters are forbidden.

Windows does not distinguish between upper-case and lower-case characters, so you cannot create a folder named A if one named a already exists. Worse, seemingly-allowed names like PRN and CON, and many others, are reserved and not allowed. Windows also has several length restrictions; a filename valid in one folder may become invalid if moved to another folder. The rules for
naming files and folders
are on the Microsoft docs.

You cannot, in general, use user-generated text to create Windows directory names. If you want to allow users to name anything they want, you have to create safe names like A, AB, A2 et al., store user-generated names and their path equivalents in an application data file, and perform path mapping in your application.

If you absolutely must allow user-generated folder names, the only way to tell if they are invalid is to catch exceptions and assume the name is invalid. Even that is fraught with peril, as the exceptions thrown for denied access, offline drives, and out of drive space overlap with those that can be thrown for invalid names. You are opening up one huge can of hurt.

How to delete all contents of a folder with Ruby-Rails?

Ruby has the *nix rm -rf equivalent in the FileUtils module that you can use to delete both files and non-empty folders/directories:

FileUtils.rm_rf('dir/to/remove')

To keep the directory itself and only remove its contents:

FileUtils.rm_rf(Dir.glob('dir/to/remove/*'))

FileUtils.rm_rf(Dir['dir/to/remove/*']) # shorter version of above


Related Topics



Leave a reply



Submit