Linux delete all files in current directory

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.

Читайте также:  Linux посмотреть от кого запущен процесс

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.

Источник

Delete All Files of a Directory in Linux

Here is a quick Linux command tips on deleting the contents of a directory, not the directory itself.

At times you’ll need to delete all the files of a directory. Not the directory itself but the contents of the directory. You could do it to clean up a project, free up space or for any other purpose.

To empty a directory, you use the following command:

The wild card means everything here. So, you are instructing the rm command to remove everything in the given directory.

Читайте также:  Невозможно получить все индексы репозитория linux mint 20

Please note that this is different than using rm on the directory name directly without /* . If you use rm -r dir , it will delete the directory along with its contents. That’s not always desirable. Is it?

Let’s see about deleting all the contents of a directory in detail.

Properly removing all files in a directory

Linux command line does not have a recycle bin. You have to be careful while deleting files. And if you have to remove multiple files using wildcard, you must be extra cautious.

This is why I advise switching to the directory you want to empty and then using the rm command. This reduces the risk of accidentally deleting the contents of a wrong directory.

Step 1: Go to the desired directory

Use the cd command to switch to the directory you want to empty.

For example, I am going to delete all the contents of the /home/abhishek/sample directory. So, I switch to it first.

It’s good to ensure that you are in the current directory:

Switch to the directory to delete its contents

Step 2: List the directory contents

You should check the directory contents to ensure that you are not deleting any important files. I advise showing hidden files as well.

If there are sub-directories, please ensure nothing important is in there.

List directory contents

Step 3: Delete all files and folders in the directory

Once you are sure that nothing important is in the directory, it is time to delete the contents.

You can use the rm command like this:

Which is a way of instructing the rm command to delete everything it sees in the current directory recursively. The recursive option -r is essential for removing sub-directories.

Sometimes, there are write protected files and then you’ll be asked to confirm the removal. To avoid that, you can include the forced delete option -f .

Delete all files of a directory in Linux

To delete only the hidden files, you can additionally run this command:

Deleting hidden files

Conclusion

While deleting all the contents of a directory without deleting the directory itself seems like an easy job, things get a bit complicated if there are hidden files and folders.

In those cases, you have to run rm -rf .* after rm -rf * .

I hope you liked this quick little Linux command line tip. Let me know if you have questions or suggestions.

Источник

Linux Delete All Files in Current Directory

You may need to delete files from your directory to free up space, clean up a project, or remove malware files. In this tutorial, we will learn how to delete all files in the current directory in Linux.

Delete all files in the directory

To delete all files in the directory use rm command followed by the path to the directory and a wildcard character «*».

This command will delete all files in the directory, but it will not delete any subdirectories or their contents. to delete subdirectories as well, you can add the «-r» flag to the command to make it recursive.

Читайте также:  Команды линукс терминал сеть

With that note: In Linux, it is best practice to navigate to a desired directory and perform the action from the current directory. Using an absolute path is more prone to accidentally choosing the incorrect path and may end up deleting the wrong files.

To delete all files in the directory /home/ubuntu/mydata/, type:

rm delete all files in a directory

Here all the non-hidden files in the directory are deleted. However, it returns an error for sub-directories. On a side note, you can exclude a specific file from deletion by rm -v !(filename.txt).

To remove all files and sub-directories from the directory /home/ubuntu/mydata, type:

rm delete all files and subdirectories in a directory

You can verify using ls -al /home/ubuntu/mydata to confirm all files and subdirectories in it are deleted.

Delete all files in the current directory

As mentioned before safest way would be to navigate to the directory and delete files from the current directory.

1. Navigate to the Directory

Use cd command to change the directory. For example, we are using /home/ubuntu/mydata.

Change to /home/ubuntu/mydata:

change to directory where the content needs to be deleted

Use pwd command to confirm the current path:

confirm current path

This will print out the absolute path of the directory.

2. List directory contents

List the directory content to make sure you going to delete the right files. For listing all files in the current directory use ls -al command.

list the directory contents

This command will list all the contents of the directory /home/ubuntu/mydata such as sub-directories and files ( including hidden files).

3. Delete all files in the Directory

Once we confirm we are in the right directory, we can delete all files in the current directory using rm -rv *.

from current directory remove all files and sub directories

This command will delete all the files and subdirectories in the current directory. The -v option gives a verbose output to show removed files and the directory.

Note: If you have any write-protected files and directories rm will prompt.

You can verify by typing ls -al

If you have any manually created hidden files, to remove them all use rm -rv .* command. This is because the asterisk (*) does not match files that begin with a dot, which are hidden files.

remove all files and subdirectories including manually created hidden files from a directory.

The individual directories . (indicates current directory) and .. (indicates parent directory ) will be still there — which cannot be deleted. No harm in keeping it.

Delete all files in directory without prompt

Use rm -rf * to delete all files in the directory without any prompt. Prompt occurs when rm encounters any errors such as write access protection. This option helps to avoid it.

For example, to delete all files in the directory /home/ubuntu/mydata without any prompt, type:

delete all files in a directory without prompt

We verify that the files have been deleted by running:

Conclusion

In this tutorial, we learned how to delete all files in a directory in Linux. Be cautious when using rm with asterisk command, as it can potentially delete important files. Always double-check the files you want to delete before executing the command.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

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