Computing.Net > Forums > Unix > find command help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

find command help

Reply to Message Icon

Name: psiva_unix
Date: March 17, 2009 at 03:23:35 Pacific
OS: Linux
Product: Sun / --
Subcategory: Software Problems
Comment:

hi,

How to find and delete the files in current directory using find command in UNIX?

Thanks
MPS



Sponsored Link
Ads by Google

Response Number 1
Name: Curt R
Date: March 17, 2009 at 07:56:56 Pacific
Reply:

To find files in the present directory try:

find . -name [filename]

To delete said file:

rm [filename]

If you would like to run this through a script so you can invoke the script and have it look for a certain file, or group of files, and then delete them if they exist will take a little more work.

Something like the following might work:

#!/bin/sh
if [ $# -eq 0 ]
        then
               echo "Usage: $0 [ordinary file]"
               exit 1
        elif [ $# -gt 1 ]
        then
               echo "Usage: $0 [ordinary file]"
               exit 1
        elif [ -d "$1" ]
        then
               echo "Usage: $0 [ordinary file]"
               exit 1
        else
               if [ -s "$1" ]
                   then
                   find . -name $1
                   rm $1
                   echo "$1 deleted"
               else
                   echo "File does not exist or is zero bytes"
               fi
fi


0

Response Number 2
Name: Prachurrjya
Date: June 11, 2009 at 05:14:54 Pacific
Reply:

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

It will delete all the files in current directory and directories below current directory


0

Response Number 3
Name: nails
Date: June 11, 2009 at 06:50:43 Pacific
Reply:

This is more efficient:

find . -type f |xargs rm

as it does not spawn a process for each file that is removed.

If you want to restrict find to only the current directory, check this out:

http://www.computing.net/answers/un...
http://www.computing.net/answers/un...


0

Response Number 4
Name: ghostdog
Date: June 11, 2009 at 18:14:49 Pacific
Reply:

>> find . -type f |xargs rm

also if you have GNU find,

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

same as using xargs

0

Response Number 5
Name: psiva_unix
Date: June 25, 2009 at 11:52:30 Pacific
Reply:


find /home/personal/ -type f -name "*.txt*" -exec rm { } \;

the command will delete the all *.txt files from /home/personal/ folder.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: find command help

Find command www.computing.net/answers/unix/find-command/6221.html

Clean up the / fs with find command www.computing.net/answers/unix/clean-up-the-fs-with-find-command/1417.html

find command skipp std error & exec www.computing.net/answers/unix/find-command-skipp-std-error-amp-exec/7960.html