Mais três opções.
-
Use find
com -mindepth 1
e -delete
:
−mindepth levels
Do not apply any tests or actions at levels less than levels (a non‐negative integer).
−mindepth 1 means process all files except the command line arguments.
-delete
Delete files; true if removal succeeded. If the removal failed, an error message is issued.
If −delete fails, find’s exit status will be nonzero (when it eventually exits). Use of
−delete automatically turns on the −depth option.
Test carefully with the -depth option before using this option.
# optimal?
# -xdev don't follow links to other filesystems
find '/target/dir with spaces/' -xdev -mindepth 1 -delete
# Sergey's version
# -xdev don't follow links to other filesystems
# -depth process depth-first not breadth-first
find '/target/dir with spaces/' -xdev -depth -mindepth1 -exec rm -rf {} \;
2. Use find
, mas com arquivos, não diretórios. Isso evita a necessidade de rm -rf
:
# delete all the files;
find '/target/dir with spaces/' -type f -exec rm {} \;
# then get all the dirs but parent
find '/target/dir with spaces/' -mindepth 1 -depth -type d -exec rmdir {} \;
# near-equivalent, slightly easier for new users to remember
find '/target/dir with spaces/' -type f -print0 | xargs -0 rm
find '/target/dir with spaces/' -mindepth 1 -depth -type d -print0 | xargs -0 rmdir
3. Vá em frente e remova o diretório pai, mas recrie-o. Você poderia criar uma função bash para fazer isso com um comando; aqui está um simples one-liner:
rm -rf '/target/dir with spaces' ; mkdir '/target/dir with spaces'