Prune Option for Find Command on AIX

I need to delete all files from the working directory and its sub directories using the find command, for that I am using -prune option but some how I am having a syntax issue.

I have tried the below, please help me correct the syntax

find . -name \* -type f -exec rm -f {} \;

>> Works but only remove the files from the current directory

find /global/. ! -name -prune -type f -name\* -exec rm -f {} \;

>> Syntax error

Thanks in advance

I take it you want to leave all the directories intact, yes? Otherwise a simple rm -rf would suffice.

find . -name \* -type f -exec rm -f {} \;

Since you are not interested in the name of the files you don't need them to filter your result. Simply:

find . -type f -exec rm -f {} \;

I hope this helps.

bakunin

2 Likes

By default find descends into all subdirectories.
The -prune is for NOT descending. For example

find . \! -name . -prune -type f -exec rm -f {} +

sets the prune flag for all directories but the start directory i.e. will not descend i.e. only deletes files in the start directory.

1 Like

Without seeing the error message you got, it's difficult to diagnose, however:-

  • If there are lots of files, your use of -name * unquoted may have expanded for each file so much that you exceeded the command line length
  • You might speed things up a little (if there are many many files) by using xargs bolted on like this find . -type f | xargs rm -f so it runs fewer rm commands (collects them up appropriate to the maximum command line length rather than one for each file) which may run quicker
  • You might speed things up a little (if there are many many files) by using \+ instead of \; if your AIX version supports it.

If you do want to specify an expression to match the file name, always quote it to avoid command line expansion. You might well have got it working with find . -name "*" ......

I hope that these help,
Robin

1 Like

Generally I discourage from find | xargs , because of its unsafe space handling.
For demonstration

# printf "%s\n" file1 "filename with spaces"
file1
filename with spaces
# printf "%s\n" file1 "filename with spaces" | xargs ls
file1: No such file or directory
filename: No such file or directory
with: No such file or directory
spaces: No such file or directory

The {} + works like xargs - without such problems.
(Linux/GNU find, before it got the {} + feature, had the old work-around find -print0 | xargs -0 .)

2 Likes