Linux how to delete directory with files

How do I remove a directory and all its contents?

The following command will do it for you. Use caution though if this isn’t your intention as this also removes files in the directory and subdirectories.

«-f» is «—force» which overrides some sanity checks and prompting. A safer command to start with would be rm -r directoryname .

@JimParis I think the word «safer» is relative. Suppose you are writing a script to run on a remote computer. That script has a command which is supposed to remove a directory. Here, it would be «safer» to use rm -rf directoryname coz you wouldn’t want your script to pause execution, because it’s waiting for user input. Of course, you have to be sure that deleting the directory would do no harm.

if rm -rf directoryname fails you, try using rm -R -f directoryname , or rm —recursive -f directoryname .

If you are not having any luck with these, you should consider reinstalling rm or switching shells.

These were the options available on my rm man page, I looked it up by typing man rm to view my options on recursive deletion and the force options.

Does your rm man page list -r ? What does it do? (Try it in a directory that you create just for testing purposes, with only dummy files (and maybe subdirectories) in it.) What operating system are you using?

P.S. If rm -r doesn’t work, that would be an OS issue, not a shell issue. (Strictly speaking, it would be an issue with the version of rm that you’re using, so you could address it by installing a different version of rm , or searching your system to see whether you already have a different version of rm in some directory other than /bin .)

Ah, right. I forgot to mention I’m on Ubuntu 14.04 When I ran man rm in my terminal, it gave me a text file with the less text viewer. I scrolled found an indented entry with a whole that had the -R and —recursive options cozied up with the -r option, signifying that all of those arguments are identical.

edit: have you tried sudo rm -r directoryName ? The unwritten rules of the basic commands is that -r will allow a program to run recursively on every file your filesystem (starting where ever you choose!) and that -f will forcefully do things, even if it’s dangerous. ‘cd’, ‘mv’, ‘ls’ mostly holds this principle true. ls -r / is gonna be a duzie, and cp -rf / /dev/null will destroy everything on your filesystem.

Читайте также:  Linux find directory and file

Other answers show how to completely remove a directory’s content, but IMO they don’t address the literal question of the original post — that is, how can one delete subdirectories (as opposed to usual files). In other words, how can one delete empty directory structures while keeping subdirectories containing files ?

This can be achieved with find :

find directoryname -type d -delete 

This command will recursively search for directories ( -type d ) through directoryname and -delete them only if their subdirectories or themselves don’t contain any files.

Источник

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.

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.

Читайте также:  Linux ufrii drv v340 uken

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 files and directories quickly via terminal (bash shell) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Читайте также:  Yozo office для linux

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

From a terminal window: When I use the rm command it can only remove files.
When I use the rmdir command it only removes empty folders. If I have a directory nested with files and folders within folders with files and so on, is there a way to delete all the files and folders without all the strenuous command typing? If it makes a difference, I am using the Mac Bash shell from a terminal, not Microsoft DOS or Linux.

Just in case you wish to restore the files in future , don’t use «rm» for such cases . Use «rm-trash» : github.com/nateshmbhat/rm-trash

4 Answers 4

-r «recursive» -f «force» (suppress confirmation messages)

+1 and glad you added the «Be careful!» part. definitely a «Sawzall» command that can quickly turn a good day into a bad one.. if wielded carelessly.

@itsmatt: You know what they say. give someone a Sawzall, and suddenly every problem looks like hours of fun!

On a Mac? Do this instead: brew install trash then trash -rf some_dir This will move the unwanted directory into your trashbin instead of just vanishing Prestige-style into the ether. (source)

Would remove everything (folders & files) in the current directory.

But be careful! Only execute this command if you are absolutely sure, that you are in the right directory.

Yes, there is. The -r option tells rm to be recursive, and remove the entire file hierarchy rooted at its arguments; in other words, if given a directory, it will remove all of its contents and then perform what is effectively an rmdir .

The other two options you should know are -i and -f . -i stands for interactive; it makes rm prompt you before deleting each and every file. -f stands for force; it goes ahead and deletes everything without asking. -i is safer, but -f is faster; only use it if you’re absolutely sure you’re deleting the right thing. You can specify these with -r or not; it’s an independent setting.

And as usual, you can combine switches: rm -r -i is just rm -ri , and rm -r -f is rm -rf .

Also note that what you’re learning applies to bash on every Unix OS: OS X, Linux, FreeBSD, etc. In fact, rm ‘s syntax is the same in pretty much every shell on every Unix OS. OS X, under the hood, is really a BSD Unix system.

Источник

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