Prune Option for Find Command on AIX


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Prune Option for Find Command on AIX
# 1  
Old 12-02-2018
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

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

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

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

>> Syntax error


Thanks in advance
# 2  
Old 12-02-2018
Quote:
Originally Posted by rosebud123
I need to delete all files from the working directory and its sub directories
I take it you want to leave all the directories intact, yes? Otherwise a simple rm -rf would suffice.

Code:
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:

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

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 3  
Old 12-02-2018
By default find descends into all subdirectories.
The -prune is for NOT descending. For example
Code:
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.
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 12-03-2018
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
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 12-04-2018
Generally I discourage from find | xargs , because of its unsafe space handling.
For demonstration
Code:
# 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.)
These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

find with prune option help needed

Hello, I am using ksh93 (/usr/dt/bin/dtksh) on Solaris and am stuck when trying to use find with the -prune option. I need to search a directory (supplied in a variable) for files matching a certain pattern, but ignore any sub-directories. I have tried: find ${full_path_to_dir_to_search}... (9 Replies)
Discussion started by: gary_w
9 Replies

2. UNIX for Dummies Questions & Answers

AIX find command using prune option

Hi, I am trying to find some files in a directory and then remove/list them if they are 30 days old. I also have 2 directories in that directory which I need to skip. Can someone please tell me what is the correct syntax? find /developer/. -name "lost+found" "projects" -prune -o -type f... (2 Replies)
Discussion started by: tkhan9
2 Replies

3. Shell Programming and Scripting

help with find command and prune option

Hi I have a directory say mydir and inside it there are many files and subdirectories and also a directory called lost+found owned by root user I want to print all files directories and subdirectorres from my directory using find command except lost+found If i do find . \( -name... (3 Replies)
Discussion started by: xiamin
3 Replies

4. Shell Programming and Scripting

find with prune option

Hi, I want to list files only from the current dir and its child dir (not from child's child dir). i have the following files, ./ABC/1.log ./ABC/2.log ./ABC/ABC1/A.log ./ABC/ABC1/B.log ./ABC/ABC1/XYZ/A1.log ./ABC/ABC1/XYZ/A2.log Here i want to list only the log file from current... (1 Reply)
Discussion started by: apsprabhu
1 Replies

5. UNIX for Dummies Questions & Answers

help me out with find command , -prune option

Hi , Kindly help me out .:) i want to find only the file t4 in directory t3. i am in dir t . the tree is as follows. if i give, find . o/p is . ./t4 ./t1 ./t1/t2 ./t1/t2/t3 ./t1/t2/t3/t4 ./t1/t2/t4 ./t1/t4 directories are like t/t1/t2/t3 and each directory has file t4. my... (7 Replies)
Discussion started by: bhuvaneshlal
7 Replies

6. Solaris

correct usage of find's -prune option

I know one of the more seasoned veterans probably opened this thread looking for their chance to refer me to the site's search feature and let me tell you. I'VE LOOKED!!!! And I didn't find anything helpful... So, I've got a windows background and I'm fond of its search feature which comes... (6 Replies)
Discussion started by: ProGrammar
6 Replies

7. UNIX for Dummies Questions & Answers

Using prune with find command

Hi, I am using a find command like below in my script: find /outfiles -type f -name cat -o -name vi -o -name grep 2>/dev/null Which will search for files like "cat" , "vi" or "grep" in the "/outfiles" and subdirectories. I want to ignore a particular subdirectory from the search. I... (4 Replies)
Discussion started by: deepakgang
4 Replies

8. UNIX for Dummies Questions & Answers

find command with prune help

I have a directory named https-abcd Under that I have some directories, files and links. One of those directories is with name logs and the logs directory has lot of files in it. I need to tar the whole https-abcd directory excluding the logs directory only, I should get all the links, files and... (2 Replies)
Discussion started by: venu_nbk
2 Replies

9. UNIX for Advanced & Expert Users

Find command with prune and exec

Hi, I'm using the following command to get a list of files on the system. find /releases -type f -exec ls -l > /home/sebarry/list.txt '{}' \; however, its searching a directory I don't want it to search so I know I have to use prune but I don't seem to be able to get prune and exec to work... (1 Reply)
Discussion started by: Sebarry
1 Replies

10. UNIX for Dummies Questions & Answers

Use -prune with find command on AIX

I am trying to get a list of top level directories below the search path but I don't want to descend subdirectories. The find command listed below returns me the list I want but it also returns subdirectories. I can't seem to get the -prune option to work the way I want. How would I modify the... (5 Replies)
Discussion started by: FuzzySlippers
5 Replies
Login or Register to Ask a Question