How to delete a directory with a linux. How to Delete Files and Directories Using the Linux Command Line

The command line is a powerful tool for automating and simplifying many of the tasks that arise when administering an operating system computer. Windows system. In this article, we will look at the DEL, ERASE, RD, and RMDIR commands. With their help, you can delete files and folders directly from command line.

If you need to delete a file via , then you need to use the DEL or ERASE command to do this. These commands are synonymous and work in the same way. You can get detailed information about these commands if you enter them on the command line with the "/? ". For example, you can type "del /? ” and all the basic information about the del command will be displayed in the console.

The DEL (or ERASE ) command is designed to delete one or more files and can take the following parameters:

  • /P - deletion with a confirmation request for each file;
  • /F - delete files with the read-only attribute;
  • /S - delete the specified file from all subfolders;
  • /Q - deletion without a confirmation request;
  • /A - deleting files according to their attributes;

    • S - System;
    • H - Hidden;
    • R - Read only;
    • A - For archiving
    • You can also use a minus sign "-" before the attributes, which has the value "NOT". For example, "-S" means a non-system file.

Usually, in order to use the DEL command, you must first go to the folder in which the file to be deleted is located, and then execute the command. In order to change the drive, you just need to enter the drive letter and a colon. And to move through the folders you need to use the command " CD".

After you got into desired folder you can start deleting files. To do this, simply enter the DEL command and the file name.

Also, if necessary, you can delete files without moving through folders. In this case, you must specify the full path to the document.

If there is a need to perform a request to confirm the deletion of each of their files, then the “/p” parameter must be added to the DEL command. In this case, the command line will prompt you to delete the file and the user will need to type the letter "Y" to confirm.

It should be noted that when using the "/a" parameter, the letters responsible for the attributes must be entered through a colon. For example, in order to delete all files with the attribute "read-only" and with the extension "txt" you need to enter the command " del /F /A:R *.txt".

Other options can be added to the DEL command in the same way. By combining them you can create very powerful commands for deleting files through the command line. Windows string. Below we provide some more examples:

  • del D:\ - destruction of all files in the root of drive D;
  • del D:\*.txt - destruction of all files with the txt extension in the root of drive D;
  • del D:\doc - deletes all files in the d:\doc folder (documents with attributes will be skipped);
  • del /A:r d:\doc\*.txt - destroy all files with the "read-only" attribute and the extension "txt" in the folder d:\doc;

Deleting folders via command line

If you need to delete a folder through the Windows command line, then the above commands will not help you. To remove folders, there is a separate command RD or RMDIR (short for English Remove Directory).

The RD and RMDIR commands are synonymous and are designed to remove folders. They can take the following parameters:

  • /S - deleting the entire directory tree, when using this parameter, not only the folder itself will be deleted, but also all its contents;
  • /Q - deleting a folder tree without asking for confirmation;

For example, in order to delete a folder, it is enough to enter the command RD and the name of the folder.

If the folder contains subfolders or files, then an error will be displayed when it is deleted.

To solve this problem, add the "/s" parameter to the RD command. In this case, the deletion proceeds without problems, but a prompt appears to confirm the deletion.

In order for the folder tree to be deleted without a confirmation prompt, you need to add the “/ q” parameter to the command. In this case, the folder is deleted without further questions.

Also, the RD command can accept several folders at once, for this you just need to separate them with a space. For example, if you execute “ rd 111 222”, then at one time you can delete folders with the names “ 111" and " 222".

If you need to delete a folder through the command line, which itself contains a space, then in this case its name must be enclosed in double quotes.

By combining the DEL and RD commands, you can create powerful scripts for clearing and deleting folders in the Windows operating system.

Free distribution, speed and immunity to viruses are the reasons for the transition to operating systems running on the Linux kernel. The first issue that a person faces is working with directives. How to create and delete a folder in Linux will be discussed next.

How to create a directory in Linux

The issue is resolved in two ways:

Both methods have an individual approach. For those who are accustomed to performing most operations with the mouse, the first method will be convenient, but if the use of the keyboard predominates in the work, the second. Moreover, creating a directory in Linux through the terminal has additional features, which are described below.

How to create a folder in Ubuntu via terminal

The terminal makes it easy to work with file system due to the possibility of simultaneous work with several objects and specifying their properties at the stage of program definition.

To create a directory in Ubuntu, use the mkdir command (from English - make directory). In this case, you must have rights to write data in the parent directory. Specifying a command involves writing the name of the program itself and listing operands (directory names) after it. If necessary, specify additional options:

  • - m – sets the access rights to the folder;
  • -p- creates a folder tree.

The full entry template looks like this:

mkdir -p -m permissions foldername1 foldername2 foldername3

To create a folder in the Debian operating system, the same principle applies, since it is based on the Linux kernel.

How to create a folder in Linux using the file manager

This method looks more aesthetically pleasing, since it does not involve writing commands. The work is based on the usual visual display of operations on the screen, which are controlled by the mouse. All actions are performed in the built-in Nautilus file manager.

The program is launched by clicking on its shortcut on the toolbar, as well as using the Dash menu. The next step is to navigate to the directory in which you plan to create a child folder. Clicking on an empty space of the manager with the right mouse button will call context menu, where the desired item is located - "Create folder". It remains to give a name to the new directory. Unlike Windows, Linux allows you to use all the characters of the keyboard layout when choosing a name, with the exception of the slash. To save the result, you need to click on the free space of the utility.

Permissions on the Ubuntu folder

Once a directory is created on Linux, it is given default permissions to allow you to perform various operations on it.

Linux tools allow you to impose restrictions on the use of a folder. To view the set permissions, use the ls-lt command. The result of its execution is a similar record:

Here, d initializes namedir as a directory, and each subsequent character implies the corresponding action:

  • r - reading;
  • w – writing to a folder;
  • x - execution.

The record can be divided into three parts. The first 3 characters set the permissions for the directory creator, the next set group permissions for the file specified in the group name. The last set of characters are permissions for all other users. From the above example, it is clear that the owner can do anything with the folder, while groups and outsiders are not allowed to write.

To edit access permissions for a folder or file, use chmod command. It operates with three digital values:

  • 4 - opens access to reading;
  • 2 - allows recording;
  • 1 - allows execution.

To open several rights at the same time, the numbers are summed up. It is important to remember that rights are divided into three categories: owner, group, other users. To allow only the group to read the folder and write information to it, the following command is given in the terminal:

Changing the control folder of a group is implemented by the chgrp command. First, create as follows Account groups:

Now you can give the group control of the folder:

chgrp accgroup namedir

How to make files public Linux Ubuntu

If the computer is connected to local network, there may be a need to share a directory or file. Sharing in Ubuntu is possible using the file manager.

You need to right-click on the directory shortcut and then select "Local Network Public Folder". If it's not there, you're missing the Nautilus Share add-on, which is easily found in the Ubuntu Software Center. After installation, the manager must be restarted.

If you need to organize access by username and password, the System Config Samba extension is installed. Through the settings, it allows you to add or remove the user for which the directory is opened.

Detailed Instructions on samba setup described in the video:

How to delete a folder in Linux

Ubuntu provides the option to remove a directory using the rmdir command. To do this, its name is recorded, and directories are listed (provided it is in the desired directory):

rmdir namedir1 namedir2

Important! If the user has another directory open, you should specify the full path to avoid disappearance desired files:

rmdirhome/dir1/namedir1

To delete a folder with files in Ubuntu, you need to use the rm-rf command:

To remove multiple directories with rmdir, you need to list them all.

Conclusion

There are several ways to create and delete a folder in Linux, each with its own advantages. Nautilus file manager and terminal are provided to choose from. If the first is more familiar and visually convenient, then the second speeds up the process and provides more options.

Good day, dear readers. Today I would like to talk about deleting a directory in PHP. It would seem that the operation is not complicated, but there are some nuances. Let's look at ways to do this.

PHP has a function for this. rmdir(). It takes as an argument the path to the directory you want to delete. However, directory must be empty is the main condition (except that the user running the web server must have write access to the directory). If files are placed in the directory, we will get an error when calling the function. Accordingly, we come to the conclusion that the directory before using the function rmdir() must be cleaned first.

On the net, it is advised to write a function that will be passed the path to the directory that we need to delete as an argument. In the body of the function, it is necessary to organize a listing of the contents of the directory, within which it is checked whether the file is a directory. If the file is a directory, then by this condition the function calls itself. This is needed to clean up nested directories, and it will work at all levels of nesting. Otherwise, the function is applied to the file unlink()- it is intended for deleting a file. Outside of this check, the last step is to remove the directory.

Implementation example:

Function recursiveRemoveDir($dir) ( $includes = glob($dir."/*"); foreach ($includes as $include) ( if(is_dir($include)) ( recursiveRemoveDir($include); ) else ( unlink( $include); ) ) rmdir($dir); ) //Remove the tmp directory from the current directory recursiveRemoveDir("tmp");
What are the problems with this code?

AT simple cases directory with all attachments will be deleted. But suppose we have a hidden file in our directory that starts with a dot, for example, .htaccess. Function glob() is a UNIX glob substitution that does not involve hidden files (DOTFILES) by default. For example, if we go to a nix terminal and use bash, then we will encounter the same problem (we are talking about hidden files within the glob). To solve the problem in bash there is a command shopt -s dotglob- it allows glob substitution hidden files. In PHP, this can be solved by adding an additional parameter GLOB_BRACE for glob() and expanding the pattern from the first parameter of the function.

$includes = glob("tmp/(,.)*", GLOB_BRACE);
And there is also a problem here - we will encounter an infinite loop, since the array will include values. and.. - which is the current and parent directory at the system level. We will come to endless loop(rather than deleting all parent directories), because. within the framework of sorting by default in the generated array will be the first .. - that is, each time we will access the same directory. To solve the problem, let's form an array from these system directories and delete their indices from the main array in a loop. There is also the issue of symbolic links. Let's say that the directory to be deleted contains a link to another directory, in deleting the contents of which we are absolutely not interested. To solve the problem, before creating a recursion, it is necessary to organize a check that the entity is not a link.

As a result, the function takes the following form:

Function recursiveRemoveDir($dir) ( $includes = glob($dir."/(,.)*", GLOB_BRACE); $systemDots = preg_grep("/\.+$/", $includes); foreach ($systemDots as $index => $dot) ( unset($includes[$index]); ) foreach ($includes as $include) ( if(is_dir($include) && !is_link($include)) ( recursiveRemoveDir($include); ) else ( unlink($include); ) ) rmdir($dir); ) //Remove the tmp directory from the current directory recursiveRemoveDir("tmp");
The code is working, but in fact it could have been made simpler. In PHP, there is a FilesystemIterator class, which already has the settings we need by default. The path to the directory whose listing we need is passed to the constructor. We just need to create an object.

Function recursiveRemoveDir($dir) ( $includes = new FilesystemIterator($dir); foreach ($includes as $include) ( if(is_dir($include) && !is_link($include)) ( recursiveRemoveDir($include); ) else ( unlink($include); ) ) rmdir($dir); ) //Remove the tmp directory from the current directory recursiveRemoveDir("tmp");
In conclusion, I would like to point out one more fast way. The correctness of its use is very doubtful - send a command to be executed in SHELL.

System("rm -rf tmp");
I draw your attention to the high memory consumption and the fact that administrators often prohibit the use of such functions on the server for security purposes.

Tags: php, rmdir, glob, recursion

AT operating systems based on the Linux kernel is usually stored a large number of empty and non-empty directories. Some of them take up a fairly large amount of space on the drive, and also often become unnecessary. In this case, the correct option would be to remove them. There are several ways to perform cleaning, each of them is applicable in a specific situation. Let's review everything available methods in more detail, and you will choose the most suitable one, based on your needs.

In this article, we will talk about console utilities and additional tools, which are launched by entering commands. However, do not forget that often graphical shells are implemented in distributions. Accordingly, to delete a directory, you just need to go to it through the file manager, right-click on the icon and select "Delete". Don't forget to empty your trash after that. However, this option will not be applicable for all users, so we advise you to read the following guides.

Before starting to consider the methods, it is important to note that when entering a command, most often you will independently indicate the name of the folder that you want to delete. When you are not at its location, you must specify the full path. If possible, we recommend that you find out the parent directory of the object and go to it through the console. This action is performed in just a few minutes:


If you are unable to determine the location, you will have to enter the full path yourself when deleting, so you will need to know it.

Method 1: Standard Terminal Commands

In any command shell Linux distribution there is a set of basic utilities and tools that allow you to perform a wide variety of actions with system settings and files, including deleting directories. There are several such utilities, and each will be most useful in a particular situation.

rmdir command

First of all, I would like to touch on rmdir. It is designed to clean the system only from empty directories. Removes them permanently, and the advantage of this tool is the simplicity of its syntax and the absence of any errors. It is enough to write in the console rmdir folder , where folder is the name of the folder in the current location. The tool is activated by pressing the key Enter.

Nothing prevents you from specifying the full path to the directory if you can't navigate to the required location or you don't need to. Then the line becomes, for example, the following form: rmdir /home/user/folder/folder1 , where user- Username, folder is the parent directory, and folder1- folder to delete. Please note that a slash must be placed before home, and it must be absent at the end of the path.

rm command

The previous tool is one of the components of the rm utility. It is originally designed to delete files, but if you give it the appropriate argument, it will also delete the folder. This option is already suitable for non-empty directories; in this case, you need to enter rm -R folder (or the full path to the directory) into the console. Pay attention to the argument -R- it starts a recursive deletion, that is, it concerns the entire contents of the folder and itself. You must be case-sensitive when entering, because -r is a completely different option.

If you want to display a list of all deleted files and folders when using rm, then you need to slightly modify the line. Type in "Terminal" rm -Rfv folder and then activate the command.

Once the deletion is complete, information about all directories and separate objects previously located at the specified location.

find command

Our site already has material with examples of using find in operating systems developed on the Linux kernel. Of course, only the basic and most useful information is presented there. You can get acquainted with it by clicking on the following link, and now we propose to find out how this tool works when you need to delete directories.

  1. As is known, find serves to search for objects within the system. Through the use of additional options, you can find directories with a specific name and delete them immediately. To do this, type find in the console. -type d -name "folder" -exec rm -rf () \;, where folder is the name of the directory. Be sure to include double quotes.
  2. A separate line sometimes displays information that there is no such file or directory, but this does not mean that it was not found. Just find worked again after deleting the directory from the system.
  3. find ~/ -empty -type d -delete allows you to delete everything empty folders that are in the system. Some of them are available only to the superuser, so before find sudo should be added.
  4. The screen will display information about all found objects and the success of the operation.
  5. You can also specify only a specific directory in which the tool will search and clean. Then the line will look like this: find /home/user/Folder/ -empty -type d -delete .

This completes the interaction with the standard console utilities in Linux. As you can see, there are a large number of them and each is applicable in certain situations. If you have a desire to get acquainted with other popular teams, read our separate material at the link below.

Team rmdir will help us to delete an empty directory, that is, a directory without files and folders. The command syntax is very simple:

Rmdir DIRNAME

If the directory is not empty, you will see the following error message.

$ rmdir test rmdir: failed to remove `test": Directory not empty

2. How to remove nested directories in Linux?

With option -p you can delete a directory with a subdirectory nested within it.

$ rmdir -p dir1/dir2/dir3

Team rmdir -p dir1/dir2/dir3 is the equivalent of the command

$ rmdir dir1/dir2/dir3 dir1/dir2 dir1

3. Deleting a directory with contents

If you need to remove a directory with contents, you can use the command rm:

$ rm -rf DIRNAME

This command will remove a directory, including all files and subdirectories it contains. Use this command with caution, as recovering deleted files will not be an easy procedure.

4. Interactive removal

To avoid accidentally deleting the files you need, I recommend that you consider using the -i option to delete files and folders interactively (especially as root).

Recursive and interactive removal of a directory.

# rm -ir DIRNAME

Interactive file deletion.

# rm -i FILENAME

5. Useful aliases for rm and rmdir commands

You can use interactive deletion as the default behavior of the rm command.

Alias ​​to permanently run rm in interactive mode

# alias rm="rm -i"

When using the command rm actually the command will be executed rm-i. But it is necessary to pay attention to one nuance. Do not use the -i option later, as you will not get the desired effect. Consider an example:

In the following command, using -i will not give you the desired effect..

$ rm -irf DIRNAME

In this case, when using an alias, the above command must be run as follows:

$ rm -rf DIRNAME

All of the commands below are equivalent and perform the same functionality.

  • rm-fr
  • rm-rf
  • rm -r -f
  • rm -f -r

If you need an expert assessment of a building, I recommend ordering an inspection of finished buildings by specialists from the Technoplast Research and Production Center

Internet