Delete files with a length of x or more characters

bash

Solution

Since the question can have 2 interpretations, both answers are given:

1. To delete files with 6 or more characters in the FILE NAME:

rm ??????*

Explanation:

- `??????`: The `?`'s are to be entered literally. Each `?` matches any single character. So here it means "match any 6 characters"

- `*`: The `*` wildcard matches zero or more characters

- Therefore it removes any file with 6 or more characters.

Alternatively:

find -type f -name "??????*" -delete

Explanation:

- `find`: invoke the find command

- `-type f`: find only files.

- `-name "??????*"`: match any file with at least 6 characters, same idea as above.

- `-delete`: delete any such files found.

2. To delete files with 6 or more characters in its CONTENTS:

find -type f -size +5c -delete

Explanation:

- `find`: invoke the find command

- `-type f`: find only files (not directories etc)

- `-size +5c`: find only files greater than 5 characters long. Note: recall that `EOF` (end of file) counts as a character in this case. If you'd like to exclude `EOF` from your counter, change it from 5 to 6.

- `-delete`: delete any such files found

Problem

I'm reviewing for an exam and one of the questions is asking me to write a single command that will delete the files in a given directory that are at least 6 characters long. Example: person@ubuntumachine:~$ ls ``` abc.txt, abcdef.txt, 123456.txt, helloworld.txt, rawr.txt ``` The command would delete the files "abcdef.txt", "12346.txt" and "helloworld.txt". I'm aware the at the * would be used at some point but I'm not sure what to use to indicate 6 characters long... Thank you <3

Original source