Delete any file in linux

Содержание
  1. How To Delete File In Linux?
  2. rm Command Syntax
  3. rm Command Help
  4. Delete Single File with rm Command
  5. Delete Multiple Files with rm Command
  6. Delete Files According To Their Extensions/Types with rm Command
  7. Delete Files Recursively
  8. Delete File with Prompt Before Every Removal
  9. Print Verbose Output About Delete Operation
  10. Delete empty Directories or Folders with rmdir Command
  11. Read File Names From Text File For Delete or Removal
  12. Delete File Names Starts with Dash —
  13. Delete Files By Reading Their Names From A File/List
  14. Delete Files By Finding them with find Command
  15. How to delete and remove files on Debian Linux
  16. Command to delete and remove files on Debian Linux
  17. Delete multiple files
  18. Debian Linux delete a file and prompt before every removal
  19. Force rm command on Debian Linux to explain what is being done with file
  20. Debian Linux delete all files in folder or directory
  21. Debian Linux delete file begins with a dash or hyphen
  22. Do not run ‘ rm -rf / ‘ command as an administrator/root or normal Debian Linux user
  23. Conclusion
  24. How to delete and remove files on Ubuntu Linux
  25. Command to delete and remove files on Ubuntu Linux
  26. Delete multiple files on Ubuntu Linux
  27. Ubuntu Linux delete a file and prompt before every removal
  28. Force rm command on Ubuntu Linux to explain what is being done with file
  29. Ubuntu Linux delete all files in folder or directory
  30. Ubuntu Linux delete file begins with a dash or hyphen
  31. Do not run ‘ rm -rf / ‘ command as an administrator/root or normal Ubuntu Linux user
  32. Conclusion
  33. 3 Ways to Permanently and Securely Delete ‘Files and Directories’ in Linux
  34. 1. Shred – Overwrite a File to Hide Content
  35. 2. Wipe – Securely Erase Files in Linux
  36. 3. Secure-deletetion Toolkit for Linux
  37. 4. sfill -Secure Free Disk/Inode Space Wiper
  38. 5. sswap – Secure Swap Wiper
  39. 6. sdmem – Secure Memory Wiper
  40. If You Appreciate What We Do Here On TecMint, You Should Consider:

How To Delete File In Linux?

Deleting files in Linux can be sometimes tricky. We have a tool named rm which is the shortcut for the word remove. In this tutorial, we will look at how to remove or delete a file in Linux with different examples and ways.

rm Command Syntax

rm command syntax is the same as the most the Linux command. We can provide options before specifying the file and directories we cant to operate or delete.

  • OPTINS states the behavior of the rm command as we will see below in detail.
  • FILENAME is the file or directory name we want to delete or operate.

rm Command Help

rm command help information can be printed to the console with the —help command which is like below. Help information will provide some popular options and usages.

Delete Single File with rm Command

We will start with simple steps just deleting a single file. We will just specify the file name we want to delete. In order to remove the file successfully, we should have privileges to modify a file. For example, if we try to remove the file owned by root with a regular user we will get an error and would not delete the file. In this example, we will delete the file named foo.txt

Delete Multiple Files with rm Command

We have the ability to delete multiple files in a single rm command. We will just put file names we want to delete by separating them with space. In this example, we will delete file names foo.txt and bar.txt but we can add more if we need it.

Delete Files According To Their Extensions/Types with rm Command

Linux bahs provides the glob or asterisk in order to specify the files with a specific name or extension. We can use glob * in order to specify a specific extension like *.txt , *.pdf , *.tmp etc. We can use this extension or name specification in order to delete specific files. In this example, we will delete all *.deb extensions.

We can also specify names like deleting all files which name starts with pof like below.

Delete Files Recursively

rm command provides the ability to delete or remove files recursively. Recursive removal will check subdirectories for files to remove with the directories. We will remove the directory name ndiff with all sub-directories and files in this example. We will use -R option for the recursive operation.

Delete File with Prompt Before Every Removal

While removing files and directories we may need approval for each file to delete. In this case, we can use -i option which will prompt to accept or deny deletion of the given file.

While deleting files and directories we may want to see details of the removal operation. rm command provides a verbose option which will list information about each deletion of file or directory. We will use -v option for this.

Читайте также:  Hp protecttools security manager для windows 10

Delete empty Directories or Folders with rmdir Command

In some cases, we need to delete empty folders. rm without options will not work in this case as we can see this in the following screenshot. We case use rmdir command to remove an empty directory or folder.

Read File Names From Text File For Delete or Removal

Another interesting use case for rm command is providing file or directory names from a list like a text file. We will use xargs command to-read the list and redirect to the rm command.

Delete File Names Starts with Dash —

Another interesting case is dash or — problem where file or directory names starting with dash . As we know Linux commands options are specified with dash . So how can rm recognize file name from option? We will use — or double dash were to specify the file or directory name start. For example we have a file named -file.txt and we want to remove. We will use the following command. As we can see the file name is specified after double dash. Options are specified before the double dash.

Delete Files By Reading Their Names From A File/List

In some cases, we may need to read a list that contains the file names we want to delete. This is generally a simple text file where each file name with their path is specified line by line. We can use xargs command to redirect the list contents to the rm command which will delete them one by one. In this example, we will read the list file names file-list.txt .

Delete Files By Finding them with find Command

find is a very useful command which is used to find files and folders. find command also provides some options like running commands on the search results. We can also remove or delete files found by the find command. In this example, we will delete files that are older than 3 days.

Источник

How to delete and remove files on Debian Linux

Command to delete and remove files on Debian Linux

The syntax is as follows for the rm and unlink command to remove files on Debian Linux:

  1. Open the Debian terminal application (bash shell)
  2. Type any one of the following command to delete a file named joey.db in the current directory
  3. rm joey.db
    OR
    unlink joey.db

Let use see all the rm command options to delete and remove files on Debian Linux.

Delete multiple files

Type the following command to delete the file named hannah.doc, vince.jpg, and dance.jpg located in the current directory:
vivek@debian-9-stretch:

$ rm hannah.doc vince.jpg dance.jpg
You can specify path too. If a file named hannah.doc located in /tmp/ directory, you can run:
vivek@debian-9-stretch:

$ rm /tmp/hannah.doc
vivek@debian-9-stretch:

$ rm /tmp/hannah.doc /home/vivek/dance.jpg /home/vivek/data/vince.jpg

  • 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

Debian Linux delete a file and prompt before every removal

To get confirmation before attempting to remove each file pass the -i option to the rm command on Debian Linux:
vivek@debian-9-stretch:

$ rm -i filename1
vivek@debian-9-stretch:

Force rm command on Debian Linux to explain what is being done with file

Pass the -v option as follows to get verbose output on Debian Linux box:
vivek@debian-9-stretch:

$ rm -v filename1
vivek@debian-9-stretch:

$ rm -v cake-day.jpg

Debian Linux delete all files in folder or directory

You need to pass the following options:
vivek@debian-9-stretch:

$ rm -rf dir1
vivek@debian-9-stretch:

$ rm -rf /path/to/dir/
vivek@debian-9-stretch:

$ rm -rf /home/vivek/oldschoolpics/
It will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data on Debian Linux.

Debian Linux delete file begins with a dash or hyphen

If the name of a file or directory or folder starts with a dash ( — or hyphen — ), use the following syntax:
vivek@debian-9-stretch:

$ rm — -filename1
vivek@debian-9-stretch:

$ rm — —filename1
vivek@debian-9-stretch:

$ rm -rf —directory1
vivek@debian-9-stretch:

$ rm ./-file
vivek@debian-9-stretch:

$ rm -rf ./—directory1

Do not run ‘ rm -rf / ‘ command as an administrator/root or normal Debian Linux user

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Debian Linux disasters. The rm -rf / variant of the command, if run by an administrator, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands on Debian Linux :
vivek@debian-9-stretch:

Читайте также:  Qemu bridge network linux

$ rm -rf /
vivek@debian-9-stretch:

Conclusion

And there you have it, the rm command which is used to removes each given FILE Debian Linux operating systems. Please note that rm does not remove directories by default. You need to pass the -r option as described above. To see all available options for the rm command type the following man command:
rm —help
OR
man rm

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

Источник

How to delete and remove files on Ubuntu Linux

Command to delete and remove files on Ubuntu Linux

The syntax is as follows for the rm and unlink command to remove files on Ubuntu Linux:

  1. Open the Ubuntu terminal application (bash shell)
  2. Type any one of the following command to delete a file named ubuntu.nixcraft.txt in the current directory
  3. rm ubuntu.nixcraft.txt
    OR
    unlink ubuntu.nixcraft.txt

Let use see all rm command options to delete and remove files on Ubuntu Linux.

WARNING : Do not type sudo rm -R / or sudo rm -r / or sudo rm -f /* or sudo rm —no-preserve-root -rf / as it removes all the data in the root directory. Avoid data loss and you should not execute them!

Delete multiple files on Ubuntu Linux

Type the following command to delete the file named dellLaptopSerials.txt, tom.txt, and dance.jpg located in the current directory:
vivek@nixcraft:

$ rm dellLaptopSerials.txt tom.txt dance.jpg
You can specify path too. If a file named dellLaptopSerials.txt located in /tmp/ directory, you can run:
vivek@nixcraft:

$ rm /tmp/dellLaptopSerials.txt
vivek@nixcraft:

$ rm /tmp/dellLaptopSerials.txt /home/vivek/dance.jpg /home/vivek/data/tom.txt

Ubuntu Linux delete a file and prompt before every removal

To get confirmation before attempting to remove each file pass the -i option to the rm command on Ubuntu Linux:
vivek@nixcraft:

$ rm -i fileNameHere
vivek@nixcraft:

$ rm -i dellLaptopSerials.txt

Force rm command on Ubuntu Linux to explain what is being done with file

Pass the -v option as follows to get verbose output on Ubuntu Linux box:
vivek@nixcraft:

$ rm -v fileNameHere
vivek@nixcraft:

$ rm -v cake-day.jpg

  • 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

Ubuntu Linux delete all files in folder or directory

You need to pass the following options:
vivek@nixcraft:

$ rm -rf dir1
vivek@nixcraft:

$ rm -rf /path/to/dir/
vivek@nixcraft:

$ rm -rf /home/vivek/oldschoolpics/
It will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data on Ubuntu Linux.

Ubuntu Linux delete file begins with a dash or hyphen

If the name of a file or directory or folder starts with a dash ( — or hyphen — ), use the following syntax:
vivek@nixcraft:

$ rm — -fileNameHere
vivek@nixcraft:

$ rm — —fileNameHere
vivek@nixcraft:

$ rm -rf —DirectoryNameHere
vivek@nixcraft:

$ rm ./-file
vivek@nixcraft:

$ rm -rf ./—DirectoryNameHere

Do not run ‘ rm -rf / ‘ command as an administrator/root or normal Ubuntu Linux user

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Ubuntu Linux disasters. The rm -rf / variant of the command, if run by an administrator, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands on Ubuntu Linux :
vivek@nixcraft:

$ rm -rf /
vivek@nixcraft:

Conclusion

The rm command removes files on Ubuntu Linux. The rm command options are as follows:

  • -f : Remove read-only files immediately without any confirmation.
  • -i : Prompts end-users for confirmation before deleting every file.
  • -v : Shows the file names on the screen as they are being processed/removed from the filesystem.
  • -R OR -r : Removes the given directory along with its sub-directory/folders and all files.
  • -I : Prompts users everytime when an attempt is made to delete for than three files at a time. Also works when deleting files recursively.

See rm command man page by typing the following man command:
% man rm
% rm —help

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

Источник

3 Ways to Permanently and Securely Delete ‘Files and Directories’ in Linux

In most cases the means we use to delete a file from our computers such as using Delete key, Trash files or rm command, which do not permanently and securely remove the file from the hard disk (or any storage media).

The file is simply hidden from users and it resides somewhere on the hard disk. It can be recovered by data thieves, law enforcement or other threats.

Assuming a file contains classified or secret content such as usernames and passwords of a security system, an attacker with the necessary knowledge and skills can easily recover a deleted copy of the file and access these user credentials (and you can probably guess the aftermath of such as scenario).

Читайте также:  Системное администрирование astra linux special edition

In this article, we will explain a number of command line tools for permanently and securely deleting files in Linux.

1. Shred – Overwrite a File to Hide Content

shred overwrites a file to hide its contents, and can optionally delete it as well.

In the command below, the options:

  1. -z – adds a final overwrite with zeros to hide shredding
  2. -v – enables display of operation progress
  3. -u – truncates and removes file after overwriting
  4. -n – specifies number of times to overwrite file content (the default is 3)

shred – overwrite a file to hide its contents

You can find more usage options and information in the shred man page:

2. Wipe – Securely Erase Files in Linux

A Linux wipe command securely erases files from magnetic memory and thereby making it impossible to recover deleted files or directory content.

First, you need to install wipe tool in order to it, run the appropriate command below:

The following command will destroy everything under the directory private.

where the flags used:

  1. -r – tells wipe to recurse into subdirectories
  2. -f – enables forced deletion and disable confirmation query
  3. -i – shows progress of deletion process

Wipe – Securely Erase Files in Linux

Note: Wipe only works reliably on magnetic memory, therefore use the other methods for solid state disks (memory).

Read through the wipe man page for additional usage options and instructions:

3. Secure-deletetion Toolkit for Linux

Secure-delete is a collection of secure file deletion tools, that contains srm (secure_deletion) tool, which is used to remove files securely.

First you need to install it using the relevant command below:

Once installed, you can use srm tool to remove files or directories securely on a Linux system as follows.

where the options used:

  1. -v – enables verbose mode
  2. -z – wipes the last write with zeros instead of random data

srm – Securely Delete Files in Linux

Read through the srm man page for more usage options and information:

4. sfill -Secure Free Disk/Inode Space Wiper

sfill is a part of secure-deletetion toolkit, is a secure free disk and inode space wiper, it deletes files on free disk space in a secure method. sfill checks the the free space on the specified partition and fills it with random data from /dev/urandom.

The command below will execute sfill on my root partition, with the -v switch enabling verbose mode:

Assuming you created a separate partition, /home to store normal system users home directories, you can specify a directory on that partition to apply sfill on it:

The are a few limitations of sfill that you can read about in the man page, where you can also find additional usage flags and instructions:

Note: These following two tools (sswap and sdmem) in the secure-deletetion toolkit are not directly relevant for the scope of this guide, however, we will explain them for knowledge purpose and future use.

5. sswap – Secure Swap Wiper

It is a secure partition wiper, sswap deletes data present on your swap partition in a secure manner.

Caution: remember to unmount your swap partition before using sswap! Otherwise your system might crash!

Simply determine you swap partition (and check if paging and swapping devices/files are turned on using swapon command), next, disable paging and swapping devices/files with swapoff command (renders swap partition unusable).

Then run sswap command on the swap partition:

sswap – Secure Swap Wiper

Make an effort to read through the sswap man page for more usage options and information:

6. sdmem – Secure Memory Wiper

sdmem is a secure memory wiper, it is designed to remove data present in your memory (RAM) in a secure manner.

It was originally named smem, but because on Debain systems there exists another package called smem – report memory consumption on per-process and per-user basis, the developer decided to rename it sdmem.

For more usage information, read through the sdmem man page:

That’s it! In this article, we reviewed a number command line tools for permanently as well as securely deleting files in Linux. As usual, offer your thoughts or suggestions about the post via the comment form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

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