Deleting directory with files in linux

In Unix, how do you remove everything in the current directory and below it?

But how do you delete everything in the current directory as well as every subdirectory below it and the contents of all of those subdirectories?

10 Answers 10

Practice safe computing. Simply go up one level in the hierarchy and don’t use a wildcard expression:

The two dashes — tell rm that is not a command-line option, even when it begins with a dash.

Because you are specifically matching a named directory and are thus less likely to delete something that you don’t intend to delete.

doesn’t it delete the directory itself too? You have to do mkdir afterwards. But then any hardlink referring to that directory will be a distinct directory afterwards.

hmm. i thought he wanted to delete everything in the current directory, but not the directory itself. how do we do that?

Will delete all files/directories below the current one.

If you want to do the same with another directory whose name you have, you can just name that

If you want to remove not only the sub-directories and files of it, but also the directory itself, omit -mindepth 1 . Do it without the -delete to get a list of the things that will be removed.

I needed to delete all the files in sub-directories, but did not want to delete the sub-directories themselves. find -mindepth 2 -delete worked great!

You need -mindepth 1 if you are specifying a directory ( find -mindepth 1 -delete ). Otherwise Johannes is right it will not delete the current working directory (when using find -delete ).

I tried: find -mindepth 1 -delete but i got illegal option — m but it worked great when i removed the mindepth option find . -delete

and then hit ESC-*, and bash will expand the * to an explicit list of files and directories in the current working directory.

  • I can review the list of files to delete before hitting ENTER.
  • The command history will not contain «rm -rf *» with the wildcard intact, which might then be accidentally reused in the wrong place at the wrong time. Instead, the command history will have the actual file names in there.
  • It has also become handy once or twice to answer «wait a second. which files did I just delete?». The file names are visible in the terminal scrollback buffer or the command history.

In fact, I like this so much that I’ve made it the default behavior for TAB with this line in .bashrc:

bind TAB:insert-completions 

Update: The . stands for current directory, but we cannot use this. The command seems to have explicit checks for . and .. . Use the wildcard globbing instead. But this can be risky.

Читайте также:  Windows operating system and linux operating system

A safer version IMO is to use:

(this prompts you for confirmation before deleting every file/directory.)

When doing things like this, I’ve found a quick ls -r . first lets you see what you are going to delete. Useful to give a quick idea that you aren’t going to delete the whole disk.

@Yen — because if you do it in the wrong place you can get disastrous results. Using a specific name in the wrong place can only go wrong if the same subdirectory happens to exist there.

It is correct that rm –rf . will remove everything in the current directly including any subdirectories and their content. The single dot ( . ) means the current directory. be carefull not to do rm -rf .. since the double dot ( .. ) means the previous directory.

This being said, if you are like me and have multiple terminal windows open at the same time, you’d better be safe and use rm -ir . Lets look at the command arguments to understand why.

First, if you look at the rm command man page ( man rm under most Unix) you notice that –r means «remove the contents of directories recursively». So, doing rm -r . alone would delete everything in the current directory and everything bellow it.

In rm –rf . the added -f means «ignore nonexistent files, never prompt». That command deletes all the files and directories in the current directory and never prompts you to confirm you really want to do that. -f is particularly dangerous if you run the command under a privilege user since you could delete the content of any directory without getting a chance to make sure that’s really what you want.

On the otherhand, in rm -ri . the -i that replaces the -f means «prompt before any removal». This means you’ll get a chance to say «oups! that’s not what I want» before rm goes happily delete all your files.

In my early sysadmin days I did an rm -rf / on a system while logged with full privileges (root). The result was two days passed a restoring the system from backups. That’s why I now employ rm -ri now.

Источник

How to Delete Large Directory with Thousands of Files in Linux

File management is one of the common tasks that a user undertakes on a Linux system, which includes creating, copying, moving, modifying, and deleting files and directories.

This article provides a few command-line tips on how you can delete a large directory that contains thousands of files in a Linux system.

Delete Files in Linux

The most common way of deleting files on a Linux system is using the rm command, which takes the following syntax format:

$ rm [ options ] sample_file.txt

For example, to delete a text file called file1.txt, run the command:

To forcefully remove a file without being asked for permission, pass the -f flag as follows.

Delete Directory in Linux

To remove or delete a directory called sample_directory, run the following command:

The -r option recursively deletes the directory alongside all the subdirectories and files contained therein.

To delete or remove an empty directory use the rmdir command, which comes in handy when you want to remove an empty directory called test_directory as shown:

Delete a Large Directory with Tons of Files

When the rm command is executed, the filesystem only removes the link to the file, which makes the file unavailable to the user, but in the real sense, the file’s data itself remains intact on the disk.

Читайте также:  What is raid partition in linux

Therefore, when the rm command is issued, only the reference to the files is removed, which frees up the storage blocks in the filesystem.

As such, there exist several avenues to delete files in Linux.

Delete Files With Inode Number in Linux

For example, you can delete a file using its inode number. You can find out a file’s inode number using the stat command as shown.

$ stat file1.txt File: file.txt Size: 4076 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 1573697 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ tecmint) Gid: ( 1000/ tecmint) Access: 2023-05-08 12:10:55.656070248 +0530 Modify: 2023-05-08 12:10:55.656070248 +0530 Change: 2023-05-08 12:10:55.656070248 +0530

In addition, you can pass the -i flag in the ls command when listing files inside a directory.

$ ls -li 1573697 .rw-rw-r-- tecmint tecmint 4.0 KB Mon May 8 12:10:55 2023  file1.txt

To remove the file using its inode, use the find command as shown in the syntax below.

$ find /path/to/file -inum INODE_NUM -exec rm -i <> +

In our example, to remove file file1.txt that sits in the current directory, the command will be:

$ find /path/to/file -inum 1573697 -exec rm -i <> +

Hit ‘y’ to confirm the removal and press ENTER.

Delete Files By Inode Number

Let us now see how to delete large directories with thousands of files.

Create a Directory with Thousands of Files

The good old rm command is the fastest way of deleting a large directory with thousands of files. To demonstrate this, we will, first, create a sample directory and navigate into it.

$ mkdir test_dir $ cd test_dir

Next, we will create an insanely huge number of files, in this case, 500,000 text files using the following bash for a loop.

$ time for item in ; do touch file_name$item.txt; done

NOTE: The above command is resource intensive and, hence, consumes substantial CPU and RAM. It also takes quite some time depending on your system specifications. In my case, I’m running a VM with 4GB RAM and 3 CPUs.

Create Thousands of Files in Linux

Fastest Way to Delete Directory in Linux

The fastest way to delete a large directory is using the good old rm directory as shown below. Here, the time option displays the time taken to successfully execute the command.

Fastest Way to Delete Large Directory

From the output, you can see that it has taken roughly 6 seconds to delete the entire directory.

Delete Large Directory with Find Command

Another way to delete large directories is using the find command as shown in the following syntax.

$ time find /path/to/directory -delete

Although not as fast as the rm command it still gets the job done.

$ time find test_dir -delete

Find Command - Delete Large Directory

Delete Large Directory with Perl Command

Another approach is to use the Perl scripting language inside the directory to remove tons of files.

$ cd test_dir $ time perl -e 'for()'

Perl Command - Delete Large Directory

From the output, you can this that it took much longer to delete all the files in the directory than the previous commands that we looked at earlier.

Conclusion

There you have it. In this guide, we have looked at how you can delete large directories that contain thousands of files on a Linux system.

Источник

How to Remove a Directory and File in Linux Using ‘rm’ Command

The rm command is a UNIX and Linux command line utility for removing files or directories on a Linux system. In this article, we will clearly explain what actually rm and “rm -rf” commands can do in Linux.

In addition, we will share a few useful examples of removing a file, removing a directory, removing multiple files or directories, prompting for confirmation, removing files recursively, and forcing the removal of files.

Читайте также:  Docker desktop linux mint

The rm command is also one of the frequently used commands on a Linux system, but also a dangerous command that you will discover later on in this article.

Remove File in Linux

By default, the rm command only removes file or files specified on the command line immediately and it doesn’t remove directories.

$ mkdir -p tecmint_files $ touch tecmint.txt $ rm tecmint.txt $ rm tecmint_files

rm Command Example

Remove Multiple Files in Linux

To remove multiple files at once, specify the file names one by one (for example file1 file2) or use a pattern to remove multiple files (for example, a pattern ending with .txt ) at one go.

$ rm tecmint.txt fossmint.txt [Using Filenames] $ rm *.txt [Using Pattern]

Remove Multiple Files in Linux

Remove Directory in Linux

To remove a directory, you can use the -r or -R switch, which tells rm to delete a directory recursively including its content (sub-directories and files).

$ rm tecmint_files/ $ rm -R tecmint_files/

Remove Directory in Linux

Remove Files with Confirmation in Linux

To prompt for confirmation while deleting a file, use the -i option as shown.

Remove Files with Confirmation

Remove Directory with Confirmation in Linux

To prompt for confirmation while deleting a directory and its sub-directories, use the -R and -i option as shown.

Remove Directory with Confirmation

Force Remove Directory in Linux

To remove a file or directory forcefully, you can use the option -f force a deletion operation without rm prompting you for confirmation. For example, if a file is unwritable, rm will prompt you whether to remove that file or not, to avoid this and simply execute the operation.

When you combine the -r and -f flags, it means that recursively and forcibly remove a directory (and its contents) without prompting for confirmation.

Force Deletion of File and Directory

Delete Directory with Verbose in Linux

To show more information when deleting a file or directory, use the -v option, this will enable the rm command to show what is being done on the standard output.

Show Information of Deletion

rm -rf / Command in Linux

You should always keep in mind that “rm -rf” is one of the most dangerous commands, that you can never run on a Linux system, especially as root. The following command will clear everything on your root(/) partition.

Create rm Command Alias in Linux

As a safety measure, you can make rm always prompt you to confirm a deletion operation, every time you want to delete a file or directory, using the -i option.

To create an alias for the rm command permanently, add an alias in your $HOME/.bashrc file.

Save the changes and exit the file. Then source your .bashrc file as shown or open a new terminal for the changes to take effect.

This simply implies that whenever you execute rm, it will be invoked with the -i option by default (but using the -f flag will override this setting).

$ rm fossmint.txt $ rm tecmint.txt

Alias rm Command Confirmation

Does rm Remove Files in Linux

Actually, the rm command never deletes a file, instead, it unlinks from the disk, but the data is still on the disk and can be recovered using tools such as PhotoRec, Scalpel, or Foremost.

If you really want to permanently delete a file or directory, you can use the shred command-line tool to overwrite a file to hide its contents.

That’s it! In this article, we have explained some really useful rm command examples and also elaborated on what the “rm -rf” command can do in Linux. If you have any questions, or additions to share, use the comment form below to reach us.

Источник

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