Linux remove all data

Linux Delete All Files In Directory Using Command Line

Linux Delete All Files In Directory

The procedure to remove all files from a directory:

  1. Open the terminal application
  2. To delete everything in a directory run: rm /path/to/dir/*
  3. To remove all sub-directories and files: rm -r /path/to/dir/*

Let us see some examples of rm command to delete all files in a directory when using Linux operating systems.

How to remove all the files in a directory?

Suppose you have a directory called /home/vivek/data/. To list files type the ls command:
$ ls

Understanding rm command option that deleted all files in a directory

  • -r : Remove directories and their contents recursively.
  • -f : Force option. In other words, ignore nonexistent files and arguments, never prompt. Dangerous option. Be careful.
  • -v : Verbose option. Show what rm is doing on screen.

Deleting hidden vs non-hidden files

In Linux, any file or directory that starts with a dot character called a dot file. It is to be treated as hidden file. To see hidden files pass the -a to the ls command:
ls
ls -a
ls -la
To remove all files except hidden files in a directory use:
rm /path/to/dir/*
rm -rf /path/to/dir/*
rm *
In this example, delete all files including hidden files, run:
rm -rf /path/to/dir1/<*,.*>
rm -rfv /path/to/dir1/

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Bash remove all files from a directory including hidden files using the dotglob option

If the dotglob option set, bash includes filenames beginning with a ‘.’ in the results of pathname expansion. In other words, turn on this option to delete hidden files:

See GNU/bash man page for the shopt command online here:
man bash
help shopt

Linux Remove All Files In Directory

As I said earlier one can use the unlink command too. The syntax is:
unlink filename
For example, delete file named foo.txt in the current working directory, enter:
unlink foo.txt
It can only delete a single file at a time. You can not pass multiple files or use wildcards such as *. Therefore, I strongly recommend you use the rm command as discussed above.

Conclusion

In this quick tutorial, you learned how to remove or delete all the files in a directory using the rm command. Linux offers a few more options to find and delete files. Please see the following tutorials:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How To Securely And Permanently Delete Your Data In Linux

There are plenty of free and paid software available to recover the deleted data from a hard drive. You may not want your personal data to be recovered or you simply wants to make it harder to recover the data. Because, someone might recover and misuse the data for their own benefit. So, it is very important that you must remove all personal, official, or any kind of important data before selling or exchanging your old hard drive. This brief tutorial describes how to securely and permanently delete your data in Linux.

Читайте также:  Linux mint различия версий

Secure-delete

While there are many tools and methods are available to delete your important files or folders in Unix-like operating systems, we are going to see only one tool named «secure-delete». It provides four useful utilities that helps you to securely and permanently erase the data from your system, so that it would be very hard to recover those data by using any data recovery software.

A word of caution: Please remember that it is very hard to recover after deleting your files or folders using secure-delete. So, double check before using secure-delete utilities. Also, all of these are unnecessary and dangerous on SSD drives or other flash based media. SSDs store data differently from hard disk drives. I recommend you to use the manufacturer utilities to erase SSDs. For HDDs, this method will just work fine as described below.

Install secure-delete

secure-delete is available in the default repositories of DEB and RPM based systems.

In Debian, Ubuntu, Linux Mint, you can install secure-delete using command:

In Arch Linux, you can install it from AUR using any AUR helper programs, for example Yay:

Securely And Permanently Delete Your Data In Linux

Like I already mentioned, secure-delete package provides the following four utilities to securely erase file/folder, disk, swap, and memory of your Linux system.

  1. srm — secure remove
  2. sfill — Secure free disk and inode space wiper
  3. sswap — Secure swap wiper
  4. smem — secure erase memory

Let us discuss the usage of each command with examples.

srm is used to erase your files and folders securely and permanently, so the data can’t be recovered by data recovery software, law enforcement or any other threats.

The typical syntax of this command is:

  • -d — Ignore the two special dot files . and .. on the command line.
  • -f — fast and insecure mode. no /dev/urandom, no synchronize mode.
  • -l — lessens the security. If you use this option for second time, it lessens the security even more.
  • -r — Recursive mode (Deletes all sub-directories and its contents).
  • -v — Verbose mode.
  • -z — Wipes the last write with zeros instead of random data.

srm usage is pretty same as rm command.

The following command will recursively delete a folder called ostechnix.

And this one will delete a file called sk.txt.

Please note that srm will not completely delete NFS shares (remote file systems), RAID systems, and swap file system.

Suggested read:

sfill

sfill will scan the specified partition or directory and look for space marked as free or available. If there is any free space, it will fill it up with some random data ensuring that there are no more recoverable data on the specified partition.

The syntax for this command is:

The following command will fill some random data on the empty space in the /home partition.

sswap

Swap partition can be used when the RAM is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. To securely erase this data (inactive pages), you need sswap utility. As the name implies, sswap command will securely erase the data from swap partition.

Читайте также:  Windows интерфейс для delphi

The syntax of sswap command is:

First, find the swap partition using command:

Sample output of the above command in my Arch Linux is:

As you see above, my swap partition is /dev/sda3.

Disable swap partition using command:

Now, securely wipe the swap partition’s data with command:

After securely erasing the Swap partition’s data, enable it back:

semem command is used to securely erase the contents of the RAM which contains the state of running programs.

The Syntax for the above command is:

To securely clear the RAM which could contain any sensitive data of running programs, enter the following command:

You might want to protect some important files and directories from accidental deletion or modification. Here are the two simple ways to protect your important files from accidental deletion.

Источник

Remove all data and settings of an uninstalled application [duplicate]

Given that I removed an application using apt remove , how do I remove the configuration files later, so that I get the result of apt purge ?

4 Answers 4

You can use apt-get purge for the same exact purpose, here is an example:

First remove your desired program using apt remove , then run:

  • r package has been removed.
  • c configuration files still lives on your system

Now use apt-get purge for exact same program, this time dpkg -l package-name output should be like:

The location of the configuration files of an application varies from application to application, and sometimes it is time-consuming to find them. Even if an application has been removed, you can run sudo apt purge

to purge its configuration files.

This is really simple and straight forward. I do it all the time. Just make sure to change the program name to the one you want to remove.

Then you want to remove all dependencies that were installed with your program; you no longer need them.

When removing has finished, continue:

Now everything to do with that program, is completely removed and uninstalled. No traces. If you installed a repository/ies, you can remove it/them by going to:

System Settings >> Software & Updates >> Other Software

On this page, you can click one-by-one, all the repositories you aren’t using and click the «Remove» button beneath the box.

I hope this helps. This is what I do and it works as it should.

EDIT: if you want to do the first two steps in one line:

The commands to remove/purge the packages have already been provided in other answers.

There are some discrepancies that can call some confusion, and leave some of the application’s folders. If there are foreign files or directories in the applications configuration folders, it may not delete the folder, just the configuration files that it uses.

So the actual configuration files/settings will be removed.

The process should be followed the autoremove command.

This example will perform the task:

In the steps, I install a sample package that will include lots of configuration files. Then the commands that follow will remove the application and all its configuration files including the /etc/apache2 directory.

If you install libapache2-mod-php after installing apache2 , the folders with the Php files will remain with the PHP configuration. If you don’t install something that uses the shared folder, the folder will also be removed.

Note:
You can pick a different package to test the understanding of what happens with the install folders. The behavior will be the same for the actual application that you want to remove along with its configuration files and settings.

Читайте также:  Где хранится файл hosts windows

Источник

How to remove all files from a directory?

The closest I’ve gotten is

but that doesn’t work for files that don’t have an extension.

9 Answers 9

Linux does not use extensions. It is up to the creator of the file to decide whether the name should have an extension. Linux looks at the first few bytes to figure out what kind of file it is dealing with.

To remove all non-hidden files* in a directory use:

However, this will show an error for each sub-directory, because in this mode it is only allowed to delete files.

To remove all non-hidden files and sub-directories (along with all of their contents) in a directory use:

* Hidden files and directories are those whose names start with . (dot) character, e.g.: .hidden-file or .hidden-directory/ . Note that, in Bash, if the dotglob option (which is off by default) is set, rm will act on hidden files too, because they will be included when * is expanded by the shell to provide the list of filename arguments.

To remove a folder with all its contents (including all interior folders):

To remove all the contents of the folder (including all interior folders) but not the folder itself:

or, if you want to make sure that hidden files/directories are also removed:

To remove all the «files» from inside a folder(not removing interior folders):

Warning: if you have spaces in your path, make sure to always use quotes.

is equivalent to 2 separate rm -rf calls:

To avoid this issue, you can use ‘ single-quotes ‘ (prevents all expansions, even of shell variables) or » double-quotes » (allows expansion of shell variables, but prevents other expansions):

  • rm — stands for remove
  • -f — stands for force which is helpful when you don’t want to be asked/prompted if you want to remove an archive, for example.
  • -r — stands for recursive which means that you want to go recursively down every folder and remove everything.

Источник

Remove all files except some from a directory

When using sudo rm -r , how can I delete all files, with the exception of the following:

19 Answers 19

If you don’t specify -type f find will also list directories, which you may not want.

Or a more general solution using the very useful combination find | xargs :

for example, delete all non txt-files in the current directory:

The print0 and -0 combination is needed if there are spaces in any of the filenames that should be deleted.

The extglob (Extended Pattern Matching) needs to be enabled in BASH (if it’s not enabled):

find . | grep -v «excluded files criteria» | xargs rm

This will list all files in current directory, then list all those that don’t match your criteria (beware of it matching directory names) and then remove them.

Update: based on your edit, if you really want to delete everything from current directory except files you listed, this can be used:

It will create a backup directory /tmp_backup (you’ve got root privileges, right?), move files you listed to that directory, delete recursively everything in current directory (you know that you’re in the right directory, do you?), move back to current directory everything from /tmp_backup and finally, delete /tmp_backup .

I choose the backup directory to be in root, because if you’re trying to delete everything recursively from root, your system will have big problems.

Surely there are more elegant ways to do this, but this one is pretty straightforward.

Источник

Оцените статью