قالب وردپرس درنا توس
Home / Tips and Tricks / How to delete files and directories in the Linux Terminal

How to delete files and directories in the Linux Terminal



  Bash Terminal on Ubuntu Laptop Concept
Fatmawati Achmad Zaenuri / Shutterstock.com

The commands rm and rmdir delete files and directories under Linux, Mac OS and other Unix-like operating systems. They are similar to the del and deltree commands in Windows and DOS. These commands are very powerful and have many options.

It is important to know that files and directories deleted with rm and rmdir will not be moved to the Trash. They are immediately removed from your computer. If you accidentally delete files with these commands, you can restore them only through a backup.

Removing Files with rm

The simplest case is deleting a single file in the current directory. Enter the command rm a space and then the name of the file to be deleted.

  rm file_1
.txt

If the file is not in the current working directory, specify a path to

  rm ./path/to/the/file/file_1.txt[19659008Youcanassignmorethanonefilenameto rm  to hand over. This deletes all specified files. 

  rm file_2.txt file_3.txt 

Wildcards can be used to select groups of files to be deleted. The * represents multiple characters, and the represents a single character. This command deletes all PNG image files in the current working directory.

  rm * .png 

This command deletes all files that have a single character extension. For example, this would delete File.1 and File.2, but not File.12.

  rm *.? 

If a file is read-only, you will be prompted to delete the file. You must answer y or n and press Enter.

 rm command with read-only file

To reduce the risk of using rm use wildcard option -i (interactive). To do this, you must confirm the deletion of each file.

  rm -i * .dat 

 rm command in interactive mode

The option -f (force) is the opposite of interactive. You will not be asked for confirmation, even if files are read-only.

  rm -f filename 

Removing directories with rm

Use -d -d (directory) to remove an empty directory. You can use wildcard characters ( * and ? ) as well as filenames in directory names.

  rm -d directory 

If you specify more than one directory name, all names are deleted from the specified empty directories.

  rm -d directory1 directory2 / path / directory / directory3 

To delete non-empty directories, use the option -r (recursive). This means that the directories and all files and subdirectories they contain are removed.

  rm -r directory1 directory2 directory3 

If a directory or file is read-only, you are prompted to confirm the deletion. To delete non-empty directories and suppress these prompts, use the options -r (recursive) and -f (force) together.

  rm -rf [19659008] Here caution is required. Failure to use the  rm -rf  command may result in data loss or system failure. It is dangerous and caution is the best policy. To understand the directory structure and the files that are deleted by the command  rm -rf use the command  tree . 

apt-get . to install this package on your system if you are using Ubuntu or any other Debian-based distribution. Instead, use the package management tool of your Linux distribution on other Linux distributions.

  sudo apt-get install tree 

When you run the tree an easy-to-understand diagram of the directory structure and underlying files is created from where it is executed.

  Tree 

 Output Tree Command

You can also specify a path to the tree command to start the tree from another directory in the file system.

  tree path / to / directory 

The command rm also has - single-file system, --no-preserve-root, --preserve-root options, this however, they are only recommended for advanced users. If you detect an error, you may accidentally delete all system files. For more information, see the manual page of the command.

Removing directories with rmdir

There is another command named rmdir that you can use to delete directories. The difference between rm and rmdir is that rmdir can only delete empty directories. It will never delete files.

The simplest case is deleting a single empty directory. As with rm you can pass multiple directory names to rmdir or a path to a directory.

Delete a single directory in the current directory by passing its name to rmdir :

  Directory rmdir 

Delete multiple directories by adding a list of names rmdir :

  rmdir directory1 directory3 

delete Provide the full path to this directory:

  rmdir / path / to / directory 

If you try to delete a folder that is not empty is, you get an error message with rmdir . In the following example rmdir deletes the client directory successfully and in the background, but does not delete it because it contains files and the projects . The projects directory stays exactly as it was and the files it contains are not affected.

 Command rmdir with a non-empty folder

When rmdir returns the error "Directory not empty", processing the directories that were submitted to the command line becomes canceled. If you asked him to delete four directories and the first directory had files, rmdir would display the error message and do nothing else. You can force it to ignore these errors with the option - ignore-fail-on-non-empty to handle other directories.

In the following example, two folders were added to [19459009übergeben] rmdir these are work / reports and work / quotes . The option - ignore-fail-on-non-empty was added to the command. Folder work / reports contains files so rmdir can not delete it. The option - ignore-fail-on-non-empty forces rmdir to ignore the error and move to the next folder it must process, which is . Quotes . This is an empty folder, and rmdir deletes it.

This was the command used.

  rmdir --ignore-fail-on-not-empty work / reports / work / citations [19659008]   rmdir with the option --ignore-fail-on-non-empty  

With the option -p (Parents) you can delete a directory and delete its parent directories as well. This trick works because rmdir starts with the target directory and then jumps back to the parent. This directory should now be empty so that it can be deleted from rmdir and the process repeats the path specified for rmdir in the following example command issued to [19459010rmdir is:

  rmdir -p work / bills 

 rmdir command with option to remove the parents

Both bills The work Directories are deleted as desired.


Regardless of whether you use Bash or another shell, Linux provides flexible and powerful commands that allow you to delete directories and files directly from the terminal's command line. Some people prefer a workflow that revolves around the terminal. Others can not have a choice in the matter. You may work on servers without a GUI installed or in a remote session on a headless system such as a Raspberry Pi. These commands are perfect for this group of people.

Regardless of the type of workflow you prefer, these commands work very well for inclusion in shell scripts. When a script is triggered by a cron job, it can help automate routine administrative tasks such as deleting unwanted log files. When investigating this use case, think about the power of these commands, test everything carefully, and always keep a current backup.




Source link