- How do I force delete a directory in Linux?
- How to force delete a directory in Linux
- Removing a directory that contains other files or directories
- How do I remove a full directory in Linux with verbose output?
- Delete a non-empty directory in Linux terminal
- 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
- Linux Delete All Files In Directory Using Command Line
- Linux Delete All Files In Directory
- How to remove all the files in a directory?
- Understanding rm command option that deleted all files in a directory
- Deleting hidden vs non-hidden files
- Bash remove all files from a directory including hidden files using the dotglob option
- Linux Remove All Files In Directory
- Conclusion
- Linux Delete Folder Recursively Command
- rm command syntax to delete directories recursively
- Examples that examples how to delete folder recursively
- Removing folders with names containing strange characters
- Deleting folder recursively command summary
How do I force delete a directory in Linux?
How to force delete a directory in Linux
Here is how to forcefully delete a folder in Linux:
- Open the terminal application on Linux.
- The rmdir command removes empty directories only. Hence you need to use the rm command to remove files on Linux.
- Type the command rm -rf dirname to delete a directory forcefully.
- Verify it with the help of ls command on Linux.
Removing a directory that contains other files or directories
The syntax is as following for deleting a directory:
rm -rf dirName
Say you have a directory named /tmp/data/ that contains two files and one directory as follows:
ls -l /tmp/data/
If you run the rmdir command, you will get an error as follows:
rmdir /tmp/data/
As explained earlier rmdir only delete the DIRECTORIES, if they are empty. Therefore you must use the rm command to remove a full directory in Linux:
rm -rf /tmp/data/
Verify it:
ls -l /tmp/data/
Use rm command to delete a non-empty directory in Linux terminal
How do I remove a full directory in Linux with verbose output?
Pass the -v option to the rm command as follows:
rm -rfv dirname
For example, delete a full directory named /tmp/bar in Linux and note down the output on the screen:
rm -rfv /tmp/bar/
Where,
- 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 ➔
- -r : Recursive delete
- -f : Force delete a directory
- -v : Verbose output
Delete a non-empty directory in Linux terminal
In case if you don’t have the permission to delete the folder run rm command as a root user. Otherwise, you will see permission denied message on the screen. Therefore to avoid that prefix sudo at the beginning of the rm command :
sudo rm -rf dirName
OR
sudo rm -rf /somedir/
To remove a folder whose name starts with a ‘ — ‘, for example ‘-backups’, use one of these commands:
rm -rfv — -backups/
OR
rm -rfv ./-bacups/
Источник
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.
Источник
Linux Delete All Files In Directory Using Command Line
Linux Delete All Files In Directory
The procedure to remove all files from a directory:
- Open the terminal application
- To delete everything in a directory run: rm /path/to/dir/*
- 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
Источник
Linux Delete Folder Recursively Command
rm command syntax to delete directories recursively
The syntax is as follows:
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | 2m |
rm -r dirName
## OR ##
rm -r folderName
## OR ##
rm -rf folderName
Did you know?
Everything is a file in Linux and Unix-like systems. In other words, your pictures, documents, directories/folders, SSD/hard-drives, NIC, USB devices, keyboards, printers, and some network communications all are files.
Examples that examples how to delete folder recursively
In this example, recursively delete data folder in the current home directory:
The specified /home/vivek/data/ will first be emptied of any subdirectories including their subdirectories and files and then data directory removed. The user is prompted for removal of any write-protected files in the directories unless the -f (force) option is given on command line:
To remove a folder whose name starts with a — , for example ‘ —dsaatia ‘, use one of these commands:
We can add the -v option to see verbose outputs. In other words, the rm command will explain what is being done to our files and folders on Linux. For instance:
rm -rfv /path/to/dir1
rm -r -f -v /home/vivek/oldpartpics
Removing folders with names containing strange characters
Your folders and files may have while spaces, semicolons, backslashes and other chracters in Linux. For example:
ls -l
Let us say we have a folder named “ Our Sales Data ” and “ baddir# ” or “ dir2 ;# “. So how do we delete those directories with special names containing strange characters? The answer is simple. We try to enclose our troublesome filename or folder name in quotes. For example:
rm ‘Our Sales Data’
rm -rfv ‘/path/to/Dir 1 ;’
rm -r -f -v «baddir#»
rm a\ long \dir1 \name
Sometimes, we need insert a backslash ( \ ) before the meta-character in your filename or folder name:
rm \$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 ➔
Deleting folder recursively command summary
Command and options | Description |
---|---|
-f | Forceful option. Ignore nonexistent files and arguments, never prompt |
-r | remove directories and their contents recursively |
-v | Verbose output |
rm — ‘-dir1’ | Remove a dir/file whoes name start with a ‘ — ‘ |
rm ./-dir1 | Same as above |
rm -rfv ‘dir name here’ | Enclose your troublesome filename/folder in quotes |
rm -rfv \$dirname1 | Same as above |
See Linux rm(1) command man page or rm command example page for more information:
man rm
rm —help
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник