Delete folder in linux command

How to delete a non-empty directory in Terminal?

I’m unable to remove a directory like «New Folder» using all the above detailed commands. It’s double worded. But I want to remove that directory. Any suggestions will be welcomed. T.Divakara, Bengaluru, India

Its the blank space in the file name, try using ‘quotes’ > rmdir ‘New Folder’ < then the folder disapers, or use escape characters for non-vissible characters.

4 Answers 4

Use the below command :

It deletes all files and folders contained in the lampp directory.

In case user doesn’t have the permission to delete the folder:

Add sudo at the beginning of the command :

Otherwise, without sudo you will be returned permission denied. And it’s a good practice to try not to use -f while deleting a directory:

Note: this is assuming you are already on the same level of the folder you want to delete in terminal, if not:

sudo rm -r /path/to/folderName 

FYI: you can use letters -f , -r , -v :

  • -f = to ignore non-existent files, never prompt
  • -r = to remove directories and their contents recursively
  • -v = to explain what is being done

In my humble opinion, it’s a good practice never to add the «f» on first attempt. Its purpose is to ignore certain warning prompts that may be important, especially if you’ve accidentally done it on/from the wrong directory. In my opinion it’s good to try without the «f» first, then only if you are encountering a lot of warning prompts and you’re sure it’s OK to ignore them all, Ctrl+C out of it and repeat the command with the «f».

@thomasrutter . Agree. A file «xxx» owner: root and group: root can BE deleted with the -f switch; and without sudo. This is the message without -f: «rm: remove write-protected regular file ‘/home/william/.cache/netbeans/v08.01/tmp/xxx’? _». _Tread gently.

However, you need to be careful with a recursive command like this, as it’s easy to accidentally delete a lot more than you intended.

It is a good idea to always double-check which directory you’re in, and whether you typed the command correctly, before pressing Enter.

Safer version

Adding -i makes it a little safer, because it will prompt you on every deletion. However, if you are deleting many files this is not going to be very practical. Still, you can try this first.

Читайте также:  Acer aspire one aoa110 linux

Many people suggest using -f (combining it into -Rf or -rf ), claiming that it gets rid of annoying prompts. However, in normal cases you don’t need it, and using it suppresses some problems that you probably do want to know about. When you use it, you won’t be warned if your arguments supply a non-existing directory or file(s): rm will just silently fail to delete anything. As a general rule, try first without the -f : if there are problems with your arguments, then you’ll notice. If you start getting too many prompts about files without write access, then you can try it with -f . Alternatively, run the command from a user (or the superuser using sudo) that has full permissions to the files and directories you’re deleting to prevent these prompts in the first place.

Источник

Remove Directory in Linux – How to Delete a Folder from the Command Line

Zaira Hira

Zaira Hira

Remove Directory in Linux – How to Delete a Folder from the Command Line

Linux CLI is a powerful tool that can help you accomplish complex tasks.

One of the common operations you’ll need to perform is to delete things. Just as like creating files and folders, deleting them from the Linux command line is something you’ll do often.

In this post, we will discuss how to delete directories from the command line. We will discuss the syntax along with some examples. I am using Ubuntu in these examples.

Syntax of the Linux rm Command

You use the rm command to delete something from the command line in Linux. The syntax of the rm command looks like this:

Some important flags you’ll need to use when deleting a directory are as follows:

  • -r , -R , —recursive [«Recursion»] – Removes directories and their contents recursively.
  • -v , —verbose [«Verbose»] – This option outputs the details of what is being done on the CLI.
  • -f , —force [«Force»] – This option ignores nonexistent files and never prompts you.
  • -i [«Interactive»] – Use this flag when you want to be prompted before every removal.
  • -d [«Directory] – This works only when the directory is empty.

⚠ Be careful when using the rm command️ and always make sure that any important data is backed up.

How to identify a folder to remove

As we are discussing how to delete folders, we need to be pretty sure that we are actually deleting a folder. We can identify a folder/directory with the d flag in the first column. Notice that files have the first flag as — .

image-55

Examples of Linux rm command

In our current folder, we have 2 folders CSharpLab and PythonLab . Their content is shown below.

Читайте также:  Linux проверить версию opengl

image-48

Note that CSharpLab is an empty directory.

How to delete a folder that’s not empty

Let’s delete the PythonLab folder first.

image-53

  • -r recursively deletes all files and folders. Note in the output below, all files ( man.py, calculator.py, palindrome.py ) and folders ( /lib ) were removed.
  • -v shares details.
  • -i makes deletion interactive, which means it will ask before removing anything.

How to delete an empty folder

Let’s try to delete the CSharpLab folder. As this folder is empty, we can use the -d flag.

image-50

How to use the -f «force» flag

Let’s now see how the -f flag works. This forces the deletion of folders without any prompts or warnings. In case of an error, -v still ignores, and deletes the files that are valid.

In the example below, there is a typo in the folder name. Note that the typo is ignored. The original file is intact.

image-51

Wrapping up

Removing directories is useful when you need to remove folders after archiving them, when deleting duplicates, when deleting unused folders, and much more.

All these tasks are targeted at creating more disk space. I hope you found this blog helpful.

Источник

How to Delete Folders in Ubuntu Command Line

Deleting folders in Ubuntu command line

You probably already know how to delete files in the command line with the rm command.

The same rm command can also be used to delete folders (called directories in Linux terminology).

rm -r directory_name_or_path

The -r option is important and I’ll explain it in the later section of this article.

Creating a new folder in Ubuntu is simple. Deleting them might not be that straightforward, especially if the folders are not empty.

There is a dedicated rmdir command for removing directories. However, it can only delete empty directories. This is why you have to use the rm command for deleting folders.

Let’s see more about removing directories in the command line in Ubuntu Linux.

Delete folders using rm command

On a fundamental level, directories are just special files that have information about the location of files stored in the memory.

This means you can almost use the rm command to remove them, just like regular files. Almost because there is a slight twist here.

If you try to delete directories the same way as files, you may encounter the «rm: cannot remove XYZ is a directory»:

$ rm no_strings_on_me rm: cannot remove 'no_strings_on_me': Is a directory

This is just a failsafe so that you don’t accidentally delete a folder with multiple files inside it.

To purposely delete a folder with the rm command, you have to use the recursive option -r :

This way, you tell the Linux system that you know that you are deleting a directory recursively with all its content. The contents are deleted first and the directory is removed.

Читайте также:  Astra linux wireguard client

Force remove a directory and its contents

A directory may have numerous files and if some of those files are write-protected, you’ll be prompted to confirm the deletion.

$ rm -r new_dir rm: remove write-protected regular empty file 'new_dir/b.txt'?

It’s okay for a couple of files but if it keeps on asking this question for too many files, it will be tiring.

This is when the -f option comes to the rescue so that you can force delete the files. You won’t get promoted anymore.

rm -rf directory_name_or_path

If you use rm -rf dir_name/* it will delete the contents of the dir_name folder but the dir_name will still exist. Why? Because you are explicitely telling the system to delete the contents of directory, not the directory.

Removing empty directories with rmdir command

If you accidentally delete the wrong file, only one file is deleted. If you accidentally delete one directory, multiple files, and even nested directories will be deleted. Which would be a terrible loss and an irreversible one.

The rmdir command exists for this sole purpose. When you use this command, the directory will be removed only if it is found to be empty. If the directory contains any files or sub-directories, the command will stop with an error that the directory is not empty.

Let us first take a look at its syntax:

I have a non-empty directory and an empty directory, as shown below:

$ ls * my_empty_dir: non_empty_dir: string1 string2 string3 string4

Let us see how it looks when I try to remove both of these directories using the rmdir command.

$ rmdir -v my_empty_dir rmdir: removing directory, 'my_empty_dir' $ rmdir -v non_empty_dir rmdir: removing directory, 'non_empty_dir' rmdir: failed to remove 'non_empty_dir': Directory not empty

The rmdir command prevented me from removing ‘non_empty_dir’ because it is not empty.

If you want to stick to using the rm command, it has a «safe» delete option as well. The rm command, when paired with -d option will exit when the directory is not empty.

Since rmdir did not delete ‘non_empty_dir’, let’s see if rm -d deletes it.

$ rm -d non_empty_dir rm: cannot remove 'non_empty_dir': Directory not empty

Conclusion

To summarize what you just learned about deleting folders in Ubuntu command line:

Command Description
rm -r dir Delete the dir folder
rm -rf dir Force delete the dir folder
rm -rf dir/* Force delete the contents but not dir itself
rmdir dir Delete dir if it is empty

The rm command is what I personally prefer, as it is used for the removal of files as well as directories, but rmdir command can be useful too, depending on the scenario.

Do let me know if you have any queries or confusion 🙂

Источник

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