How to Delete Files and Folders via SSH: Difference between revisions


(Created page with "There are times when you'll need to remove multiple instances of a certain file that is strewn about your websites files. Maybe you, accidentally created the ''same file'' in...")
 
Line 18: Line 18:


  $ rm -rf foldername/
  $ rm -rf foldername/
Or
$ rmdir foldername/


To delete all files/folders in the current directory, without deleting the directory itself, you would need to use:
To delete all files/folders in the current directory, without deleting the directory itself, you would need to use:
Line 24: Line 27:


'''Note:''' ''Please be very careful when using rm command, as it might have devastating effects on your website/server, should you delete a system file or a folder.''
'''Note:''' ''Please be very careful when using rm command, as it might have devastating effects on your website/server, should you delete a system file or a folder.''


=== Using  find and -delete ===
=== Using  find and -delete ===

Revision as of 11:34, 13 July 2017

There are times when you'll need to remove multiple instances of a certain file that is strewn about your websites files. Maybe you, accidentally created the same file in every directory within your website and they're too numerous to delete manually. Well, theres a command for that. Multiple actually.

Using RM

The simplest way to delete a file a file using the following command: rm

RM allows you to specify multiple files to be deleted in a single command. Like so:

$ rm file.html file1.html file3.html

However, you would soon notice that listing all the files/folders that need to be deleted can become quite cumbersome and time consuming. Fortunately, rmaccepts several arguments, which can assist us in our task. For example; if we take the above example, we could type:

$ rm file*.html

This would match all files starting with file and ending in .html and subsequently delete them.

To delete an entire directory:

$ rm -rf foldername/

Or

$ rmdir foldername/

To delete all files/folders in the current directory, without deleting the directory itself, you would need to use:

$ rm -rf *

Note: Please be very careful when using rm command, as it might have devastating effects on your website/server, should you delete a system file or a folder.

Using find and -delete

$ find . -name 'duplicate.html' -type f -delete 

The -delete option means that find will directly delete the matching files as it traverses the directories.

If you need to target only files, using -type f flag will mean that find will only process files and not directories. To target directories, you would want to use -type d.

Using find and -exec

$ find . -name \*.html -type f -exec rm -f {} \;
$ find . -name \*.html -type f -exec rm -f {} +

The -exec option allows find to execute an arbitrary command per line. The first variant {} \; will run the command once per file. Whereas the second will group as many of the commands as possible by replacing {} with as many parameters (files) as possible.

$ find . -name \*.html -type f -exec rm -f {} \;:

An example of the above would be translated into the following if targeting a directory with multiple html files that needed to be removed.

$ find . -name \*.html -type f -exec rm -f file1.html
$ find . -name \*.html -type f -exec rm -f file2.html
$ find . -name \*.html -type f -exec rm -f file3.html
$ find . -name \*.html -type f -exec rm -f file4.html 


$ find . -name \*.html -type f -exec rm -f {} +:

An example of the above would be translated into the following if targeting a directory with multiple html files that needed to be removed.

$ find . -name \*.html -type f -exec rm -f file1.html file2.html  file3.html  file4.html 

Using find and xargs

$ find . -name \*.html -type f -print0 | xargs -0 rm -f

Piping the output to xargs is used form more complex per-file commands than is possible with -exec. The option -print0 tells find to separate matches with null instead of a newline, and -0 tells xargs to expect null-separated input. This option is safer when dealing with filenames that contain whitespace.