Меню Рубрики

Remove linux directory with files

How do I remove a full directory in Linux?

When attempting to remove a directory using a command, such as rmdir, you may receive a prompt similar to «rmdir: ‘dir’: Directory not empty» and be unable to delete the directory.

To remove a directory that contains other files or directories, use the following command.

In the example above, you would replace «mydir» with the name of the directory you want to delete. Executing the command would recursively delete all files and subdirectories in that directory.

By default, rm will not prompt you to confirm deletions. If rm deletes the files successfully, it displays no output, and silently return exit status 0 (success).

If you want to be prompted before deletions, use the -i option. To see a line of output for each deletion, use the -v (verbose) option. For complete options and examples, see our Linux rm command reference.

Источник

How to Remove (Delete) Directory in Linux

Updated Feb 26, 2020

There are several different ways to remove directories in Linux systems. If you use a Desktop file manager such as Gnome’s Files or KDE’s Dolphin, then you can delete files and directories using the manager’s graphical user interface. But, if you are working on a headless server or want to remove multiple directories at once, your best option is to delete the directories (folders) from the command line.

In this article, we will explain how to delete directories in Linux using the rmdir , rm , and find commands.

Before You Begin #

When removing a directory using a desktop file manager, the directory is actually moved to the Trash and can be easily recovered.

Be extra careful when removing files or directories from the command line because once the directory is deleted using the commands explained in this article, it cannot be fully recovered.

On most Linux filesystems, deleting a directory requires write permission on the directory and its content. Otherwise, you will get “Operation not permitted” error.

Directory names with a space in them must be escaped with a backslash ( / ).

Removing Directories with rmdir #

rmdir is a command-line utility for deleting empty directories. It is useful when you want to delete a directory only if it is empty, without needing to check whether the directory is empty or not.

To delete a directory with rmdir , type the command followed by the name of the directory you want to remove. For example, to delete a directory named dir1 you would type:

If the directory is not empty, you will get the following error:

In this case, you will need to use the rm command or manually remove the directory contents before you can delete it.

Removing Directories with rm #

rm is a command-line utility for deleting files and directories. Unlike rmdir the rm command can delete both empty and non-empty directories.

By default, when used without any option rm does not remove directories. To delete an empty directory, use the -d ( —dir ) option and to delete a non-empty directory, and all of its contents use the -r ( —recursive or -R ) option.

For example to delete a directory named dir1 along with all of its contents you would type:

If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:

To remove multiple directories at once, invoke the rm command, followed by the names of the directories separated by space. The command below will remove each listed directory and their contents:

The -i option tells rm to prompt you to confirm the deletion of each subdirectory and file. If the directory contains a lot of files, this can be a little annoying, so you may consider using the -I option what will prompt you only once before proceeding with the deletion.

To remove the directory type y and hit Enter .

You can also use regular expansions to match and delete multiple directories. For example, to remove all first-level directories in the current directory that ends with _bak , you would use the following command:

Using regular expansions when removing directories may be risky. It is recommended first to list the directories with the ls command so that you can see what directories will be deleted before running the rm command.

Removing Directories with find #

find is a command-line utility that allows you to search for files and directories based on a given expression and perform an action on each matched file or directory.

The most common scenario is to use the find command to delete directories based on a pattern. For example, to delete all directories that end with _cache in the current working directory, you would run:

Let’s analyze the command above:

  • /dir — recursively search in the current working directory ( . ).
  • -type d — restricts the search to directories.
  • -name ‘*_cache’ — search only directories that end with _cache
  • -exec — executes an external command with optional arguments, in this case, that is rm -r .
  • <> + — appends the found files to the end of the rm command.

Removing all empty directories #

To remove all empty directories in a directory tree you would run:

Here is an explanation for the options used:

  • /dir — recursively search in the /dir directory.
  • -type d — restricts the search to directories.
  • -empty — restricts the search only to empty directories.
  • -delete — deletes all found empty directories in the subtree. -delete can delete only empty directories.

Use the -delete option with extreme caution. The find command line is evaluated as an expression, and if you add the -delete option first, the command will delete everything below the starting points you specified.

Always test the command first without the -delete option and use -delete as the last option.

/bin/rm: Argument list too long #

This error message appears when you use the rm command to remove a directory that contains a huge number of files. This happens because the number of files is larger than the system limit on the size of the command line argument.

There are several different solutions to this problem. For example, you can cd to the directory and manually or using a loop to remove sub-directories one by one.

The easiest solution is first to delete all files within the directory with the find command and then delete the directory:

Conclusion #

With rm and find you can delete directories based on different criteria fast and efficient.

Deleting directories is a simple and easy process, but you must be cautious not to delete important data.

If you have any questions or feedback, feel free to leave a comment.

Источник

How to remove files and directories quickly via terminal (bash shell) [closed]

Want to improve this question? Update the question so it’s on-topic for Stack Overflow.

Closed 5 years ago .

From 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 any 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 terminal, not Microsoft DOS or linux.

4 Answers 4

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

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.

Источник

Delete / Remove a Directory Linux Command

Commands to remove a directory in Linux

There are two command to delete a folder in Linux:

  1. rmdir command – Deletes the specified empty directories and folders in Linux.
  2. rm command – Delete the file including sub-directories. You can delete non-empty directories with rmdir command in Linux.

Let us see some examples and usage in details delete the directories.

rmdir command syntax to delete directory in Linux

The rmdir command remove the DIRECTORY(ies), if they are empty. The syntax is:
rmdir directory-name
rmdir [option] directory-name
Open the terminal application and run command to delete given directory. For example, delete a folder named dir1:
rmdir dir1

Delete directory Linux Command

Open a command line terminal (select Applications > Accessories > Terminal), and then type the following command to remove a directory called /tmp/docs:
rmdir /tmp/docs
If a directory is not empty you will get an error message that read as follows:
rmdir letters
Sample outputs:

You can cd to the directory to find out and list all files:
$ cd letters
$ ls
Delete those files or directories. In this next example, remove data, foo and bar if bar were empty, foo only contained bar and data only contained foo directories:
cd /home/nixcraft
rmdir -p data/foo/bar

Where,

  • -p : Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component.

How to see a diagnostic message for every directory processed

Pass the -v option to the rmdir command:
$ rmdir -v dir1
Sample outputs:

Removing directories with rmdir and wildcards

We can use wildcards such as ‘*’ and ‘?’ to match and delete multiple directories. For example:
$ ls -l dir*
We have three dirs named dir1, dir2, and dir3. To delete all directories starting with ‘dir’ in the current, you would use the following command:
rmdir -v dir*

Linux remove entire directory including all files and sub-directories command

To remove all directories and subdirectories use the rm command. For example, remove *.doc files and all sub-directories and files inside letters directory, type the following command ( warning all files including subdirectories will be deleted permanently):
$ rm -rf letters/
Sample session:

Where,

  • -r : Attempt to remove the file hierarchy rooted in each file argument i.e. recursively remove subdirectories and files from the specified directory.
  • -f : Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions

Are you getting permission denied error message while removing directories?

Only owners can delete their directories. However, a sysadmin can delete any directories created by anyone on the system. The syntax is:
sudo rmdir /path/to/dir/
sudo rm -rf dir2
When prompted, you need to provide root user or sudo user password.

Use find command to delete unwanted directories

Say you want to find out all directories named ‘session’ and delete them in the current directory, run:
find . -type d -iname ‘session’ -delete

How to find and remove all empty directories

Run:
find . -type d -iname ‘session’ -empty -delete
Where,

  • -type d : Only search for directories and ignore all other files.
  • -iname ‘session’ : Search directory named ‘session’. You can use wildcards here too. For example, -iname ‘dir*’ .
  • -empty : Only match empty directories
  • -delete : Deletes all found empty directories only

To delete all ‘.DS_store’ directories stored in /var/www/html, run:
sudo find /var/www/html/ -type d -name .DS_Store -exec rm <> \;
OR
sudo find /var/www/html/ -type d -name .DS_Store -exec rm <> +
The -exec option to the find command run an external command named rm to delete all files. The “ rm <> +/ ” is a better option as it uses one rm command to delete all .DS_Store directories.

Conclusion

This page showed how to delete a directory when it is empty. Further, it showed, how to remove folders using the rm and rmdir commands. See rm help page here:

  • For more information read man pages: rm(1)

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

  • Очистка корзины mac os через терминал
  • Очистка mac os sierra
  • Очистить сообщения mac os
  • Очистить другое на mac os x как очистить
  • Очистить dns кэш mac os sierra