I have some folders with \n character it their names.

for example:

$ ls

Thats refer to a folder with Test name and a empty line before its name.

So when I run some scripts like this, in its parent directory:

while IFS= read -r d; do 
    rmdir $d
done < <(find * -type d)

It shows:

rmdir: failed to remove '': No such file or directory
rmdir: failed to remove 'Test': No such file or directory

Because it runs twice, once on \n and the another on Test, because the folder name has two lines.

So how can I solve this issue such that, script knows \nTest is just one folder?

  • 3
    You need to use find's -print0 directive, and the -d read option. See stackoverflow.com/a/40189667/7552 – glenn jackman Jun 4 '18 at 22:27
  • 1
    @glennjackman Please answer! – dessert Jun 4 '18 at 22:27
  • @glennjackman Thanks for your reply but find * -type d -print0 | while IFS= read -d '' file ; do rmdir $file ; done command have this output rmdir: failed to remove 'Test': No such file or directory. – Tara S Volpe Jun 4 '18 at 22:34
  • 5
    You must quote the variable : rmdir "$file" – glenn jackman Jun 4 '18 at 22:40
  • 1
    @glennjackman Just post this as answer. It's a proper solution and besides comments are not the best place for that. Upvote already implied :) – Sergiy Kolodyazhnyy Jun 5 '18 at 0:56

You've only single command there, so it's sufficient to call find with -exec flag calling rmdir:

find -depth -type d -exec rmdir {} \;

Or use the -delete option as in find -type d -delete, but it won't work with non-empty directories. For that you will also need -empty flag. Note also, -delete implies -depth so that may be skipped. Thus another viable alternative that keeps everything as one process:

find -type d -empty -delete

If the directory not empty, use rm -rf {} \;. To isolate only directories with \n in filename we can combine bash's ANSI-C quoting $'...' with -name opption:

find  -type d -name $'*\n*' -empty -delete

POSIX-ly, we could handle it this way:

find -depth  -type d -name "$(printf '*\n*' )" -exec rmdir {} \;

It is worth mentioning that if your goal is removal of directories, then -delete is sufficient, however if you want to execute a command on directory then -exec is most appropriate.

See also

| improve this answer | |
  • 2
    … use rm -rf with care. ;P Doesn’t find have a -delete option btw? It deletes empty directories and throws errors for non-empty ones like rmdir – might be less expensive. – dessert Jun 4 '18 at 22:41
  • 3
    @dessert It does and I added that in, but -delete won't remove non-empty directories. Hence, the careful use of rm -rf – Sergiy Kolodyazhnyy Jun 4 '18 at 22:47
  • 6
    Always dry-run such find commands without any destructive payload (i.e. remove the -delete or -exec whatever \;) to check if the list of affected files is actually correct and does not contain stuff that should not get deleted... – Byte Commander Jun 4 '18 at 22:50
  • 2
    This is askubuntu.com so we can assume GNU find. Use -exec rmdir {} + to batch multiple args onto one rmdir command line. And BTW "but it won't work with non-empty directories." applies to rmdir as well, so that's not actually a difference from -delete. It's a difference from rm -r. – Peter Cordes Jun 5 '18 at 1:01
  • 2
    find -type d -empty -delete (-delete implies -depth). – Kevin Jun 5 '18 at 2:51

You could use shell globs instead of find:

for d in */ ; do 
    rmdir "$d"

The shell glob */ matches all folders in the current directory. This for-loop construction takes care of correct word splitting automatically.

Note that depending on your shell options, this might ignore hidden folders (name starting with a .). That behaviour can be changed to match all files for the current session using the command shopt -s dotglob.

Also don't forget to always quote your variables.

| improve this answer | |
  • the glob will only find files/directories in the current directory, not recursively like find does – ilkkachu Jun 5 '18 at 9:54
  • 1
    You are right @ilkkachu. That could be changed by enabling the globstar shell option shopt -s globstar and using a **/*/ glob instead. – Byte Commander Jun 5 '18 at 10:26

Both answers written so far call rmdir once per directory, but as rmdir can take multiple arguments I wonder: Isn’t there a more efficient way?

One could simply do

rmdir */

and that’s definitely the easiest and most efficient way, but it may throw an error in the case of many directories (see What is the maximum length of command line arguments in gnome-terminal?). If you want this approach to work recursively, enable the globstar shell option with shopt -s globstar and use **/*/ instead of */.

With GNU find (and if we don’t just want to use -delete), we could do

find -depth -type d -exec rmdir {} +

which builts the command line “in much the same way that xargs builds its command lines” (man find). Substitute -depth for -maxdepth 1 if you don’t want it to work recursively.

A third and IMO brilliant way is explained by steeldriver in this answer:

printf '%s\0' */ | xargs -0 rmdir

This uses the shell builtin printf to build a zero-delimited argument list, this list is then piped to xargs which calls rmdir exactly as often as necessary. You can make it work recursively with shopt -s globstar and **/*/ instead of */ as above.

| improve this answer | |
  • 2
    find is recursive, but the OP's loop isn't. To replicate that behaviour, use find -maxdepth 1 -type d -exec rmdir {} +. (-depth is redundant in that case.) Neat trick with printf to emulate find -print0, I hadn't seen that before. – Peter Cordes Jun 5 '18 at 9:08
  • 1
    Oh! I saw the ls and the while loop, I missed that they were redirecting from a process substitution with find instead of piping ls into the loop and just not showing it for some reason. That find * is really buried. Yes it's recursive, and will fail if there are too many directory entries in the directory, including non-directories because it doesn't use */. find . would be much better, because find is faster at stating than bash's glob expansion. – Peter Cordes Jun 5 '18 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.