9

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

for example:

$ ls
''$'\n''Test'

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 Answers3

12

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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 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 04 '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 04 '18 at 22:47
  • @SergiyKolodyazhnyy Thanks for your answer, These commands are working and the directory is removed, But it shows this error on command line find: ‘\nTest/’: No such file or directory – Tara S Volpe Jun 04 '18 at 22:49
  • 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 04 '18 at 22:50
  • @TaraSVolpe Are the directories nested ? I.e. is \nTest a subdirectory of something else with newline ? – Sergiy Kolodyazhnyy Jun 04 '18 at 22:51
  • @TaraSVolpe Add -depth flag before -type d as steeldriver suggests – Sergiy Kolodyazhnyy Jun 04 '18 at 22:51
  • 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 05 '18 at 01:01
  • 2
    find -type d -empty -delete (-delete implies -depth). – Kevin Jun 05 '18 at 02:51
  • 1
    If the directories don't have to be empty, use rm -rf */ like a normal person. Using find -depth for that is just silly. – Peter Cordes Jun 05 '18 at 09:19
  • "POSIX-ly", you don't have -delete at all. – Charles Duffy Jun 05 '18 at 17:12
  • @CharlesDuffy OK, will edit that – Sergiy Kolodyazhnyy Jun 05 '18 at 17:48
10

You could use shell globs instead of find:

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

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.

Byte Commander
  • 107,489
  • the glob will only find files/directories in the current directory, not recursively like find does – ilkkachu Jun 05 '18 at 09: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 05 '18 at 10:26
6

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.

dessert
  • 39,982
  • 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 05 '18 at 09: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 05 '18 at 09:15