- How To Delete File In Linux?
- rm Command Syntax
- rm Command Help
- Delete Single File with rm Command
- Delete Multiple Files with rm Command
- Delete Files According To Their Extensions/Types with rm Command
- Delete Files Recursively
- Delete File with Prompt Before Every Removal
- Print Verbose Output About Delete Operation
- Delete empty Directories or Folders with rmdir Command
- Read File Names From Text File For Delete or Removal
- Delete File Names Starts with Dash —
- Delete Files By Reading Their Names From A File/List
- Delete Files By Finding them with find Command
- How to delete and remove files on Ubuntu Linux
- Command to delete and remove files on Ubuntu Linux
- Delete multiple files on Ubuntu Linux
- Ubuntu Linux delete a file and prompt before every removal
- Force rm command on Ubuntu Linux to explain what is being done with file
- Ubuntu Linux delete all files in folder or directory
- Ubuntu Linux delete file begins with a dash or hyphen
- Do not run ‘ rm -rf / ‘ command as an administrator/root or normal Ubuntu Linux user
- Conclusion
- How to Delete Files and Folders in Linux
- The rmdir Command
- Remove Folders in Linux Using the rmdir Command
- The rm Command
- Remove Files in Linux Using the rm Command
- Wrap Up
- 3 Ways to Permanently and Securely Delete ‘Files and Directories’ in Linux
- 1. Shred – Overwrite a File to Hide Content
- 2. Wipe – Securely Erase Files in Linux
- 3. Secure-deletetion Toolkit for Linux
- 4. sfill -Secure Free Disk/Inode Space Wiper
- 5. sswap – Secure Swap Wiper
- 6. sdmem – Secure Memory Wiper
- 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.
Print Verbose Output About Delete Operation
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.
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 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:
- Open the Ubuntu terminal application (bash shell)
- Type any one of the following command to delete a file named ubuntu.nixcraft.txt in the current directory
- 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
Источник
How to Delete Files and Folders in Linux
Linux command line fundamentals are absolutely essential for every future system administrator and advanced Linux user. Today we’ll cover another basic function – deleting files and directories in Linux using the command line.
The rmdir Command
The command used to delete empty directories in Linux is rmdir.
The basic syntax of this command is easy to grasp. here’s an example:
- rmdir is the command
- [option] is an optional modifier that changes the way the command behaves
- DirectoryName is the directory you want removed
If no option is provided, rmdir simply deletes the directory whose name is provided as the destination. Before using this command, you’ll have to log into your VPS server using SSH. Here’s an article to help you out.
Remove Folders in Linux Using the rmdir Command
Before using the rmdir command we suggest you check the files present in a directory with the ls command. In our case, we have a directory named Dir1.
This command will delete the empty directory named Dir1. Simple enough, right?
You can also delete multiple directories by separating their names by spaces. For instance:
After executing this command, the directories named Dir1, Dir2 and Dir3 will be deleted.
Let say we have a directory named Dir3. Dir3 has subdirectories and files inside of it. Now if we use following command:
We will receive an error like this:
As you may have guessed from the output, rmdir only works with empty directories.
The rmdir is a smart utility, and only allows you to delete empty directories as a built-in safeguard to prevent accidental loss of data. Remember it’s almost impossible to recover deleted data on any Linux distribution.
The -p option lets you delete the directory as well as its parent directories.
This above command will delete Dir3 and its parent directories Dir2 and Dir1.
The -v option outputs a diagnostic text for every directory processed. Using this option will output a confirmation listing all the directories that were deleted.
The rm Command
The rmdir command is great for safely removing unused and empty directories. If you want to remove files or directories containing files, you would have to use the rm command.
The basic syntax of this command is similar to rmdir:
Remove Files in Linux Using the rm Command
Use the rm command to remove the file named article.txt:
If we have a directory with the name Dir1 containing subdirectories and files we will have to attach the -r modifier. The command would look like this:
The -r option recursively removes directories and their content.
Another helpful option is -i. This will ask you to confirm the files that will be deleted individually. That way you can avoid any unpleasant mistakes.
You can also delete empty directories using -d option. The following command will delete an empty directory with name Dir1:
You can use a wildcard (*) and a regular expansions to match multiple files. For instance, the following command will delete all pdf files placed in the current directory.
You can use variation of all the commands listed above to delete files with other extensions like .txt, .doc, .odt, etc.
The -f option lets you forcefully delete everything placed in a directory. The command would look like this:
The above command will delete everything recursively and forcefully, residing under the Dir1 directory without prompting anything on the terminal.
You can also delete more than one directory at a time. The following command will delete three directories Dir1, Dir2 and Dir3 in a single command.
Congratulations, you successfully mastered all the basic functions of the rm and rmdir commands!
Wrap Up
In Linux deleting a single file by accident can lead to major issues. That’s why it’s essential to master the two main commands for file and directory removal – rm and rmdir. In this article we cover these two commands and the various options that can be used with them.
We hope you found this article useful! Remember, once you delete a file or directory from Linux, you can’t recover it, so be extra careful! Good luck.
Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward’s goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.
Источник
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).
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:
- -z – adds a final overwrite with zeros to hide shredding
- -v – enables display of operation progress
- -u – truncates and removes file after overwriting
- -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:
- -r – tells wipe to recurse into subdirectories
- -f – enables forced deletion and disable confirmation query
- -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:
- -v – enables verbose mode
- -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.
Источник