39.3k views
3 votes
. How do you find and remove files whose names contain embedded spaces? What would the Linux command(s) be?

User Alizeyn
by
5.3k points

1 Answer

4 votes

Answer:

The Linux command is find.

Step-by-step explanation:

The Linux command find suffices for finding and removing files whose names contain embedded spaces.

The command find supports tests of different kinds and predefined (and user-defined) actions in conjunction like that one looking for files with embedded spaces and then delete them.

We can achieve this using the following piece of code (as example):

find . -regex '.* +.*' -delete

The command find will look for files in the current working directory ( . ), execute the test -regex, to evaluate files in the current directory using the case-sensitive regular expression test (regex) (it could be also -iregex) that evaluates those files having names with one or more embedded spaces ( +) between zero or more characters before (.*) and after (.*) of these embedded space(s) ( +).

Look carefully that one or more embedded spaces is (are) represented in regular expressions using a space before the metacharacter (+), and characters are represented using the metacharacter (.) before (*), that is, (.*)

In other words, the quantifier (+) represents one or more occurrences (or matches) and quantifier metacharacter (*) zero or more times cases for that evaluation.

So, after testing all files available in the current directory with -regex test, find will execute the action -delete for all files matching the result of such an evaluation (and finally obtaining what we are looking for).

The command find has several other tests (-name, -iname, -ctime, and many more) that can be used with logical operators (-and, -or, -not), some predefined actions like -delete (aforementioned), -ls (list), -print and -quit, and also offers possibilities for user-defined actions, as mentioned before.

User Valentun
by
5.0k points