You are not logged in.

#1 2005-03-14 17:26:06

N3xt
Member
Registered: 2005-02-28
Posts: 22

How do you recursive search and delete along w/ directories?

Hey Everyone,

Two things have been bugging me for quite some time, and I was wondering if anyone could answer them.  How do I recursively search for a file throughout the entire computer -I can't seem to use find to do this, it's only limited to the current directory, and how can I recursively delete an entire directory all in one command -Usually I have to rm /dir/* -r && rmdir /dir, which doesn't even get all the hidden contents hmm?

Thanks

Offline

#2 2005-03-14 17:28:48

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: How do you recursive search and delete along w/ directories?

# find / -name "myfile*"

and

# rm -r ~/dir

both work fine for me

Offline

#3 2005-03-14 17:32:33

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: How do you recursive search and delete along w/ directories?

to find a file anywhere on the computer you can use

find / -name "filenameglobpattern"

OR you can use locate:

locate filenameglob

but you have to run 'updatedb' periodically (its a cron job in default Arch that runs daily at midnight if your comp is on) because locate uses a database of files instead of searching directly

rm -r /path/to/anydir

will remove 'anydir' and all its contents. Sometimes you have to use rm -rf to force it if there are special read-only files, but that's rare unless you're doing something strange or stupid as root or working with an unpackaged directory from a tar somebody else made.

Dusty

Offline

#4 2005-03-14 17:40:48

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: How do you recursive search and delete along w/ directories?

Dusty wrote:
locate filenameglob

I'd recommend slocate... same thing, but security enhanced

Offline

#5 2005-03-14 17:50:35

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: How do you recursive search and delete along w/ directories?

dusty:~ $ ls -l /usr/bin/locate
lrwxrwxrwx  1 root root 7 2004-12-11 17:51 /usr/bin/locate -> slocate

Dusty

Offline

#6 2005-03-14 17:52:26

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: How do you recursive search and delete along w/ directories?

Dusty wrote:
dusty:~ $ ls -l /usr/bin/locate
lrwxrwxrwx  1 root root 7 2004-12-11 17:51 /usr/bin/locate -> slocate

Dusty

watch it, hippy! *shakes fist*

Offline

#7 2005-03-14 18:34:28

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: How do you recursive search and delete along w/ directories?

Are you sure you want to delete user phrakture? [Y/N]

*hesitates*

lol

Dusty

Offline

#8 2005-03-14 18:34:58

dp
Member
From: Zürich, Switzerland
Registered: 2003-05-27
Posts: 3,378
Website

Re: How do you recursive search and delete along w/ directories?

phrakture wrote:
Dusty wrote:
dusty:~ $ ls -l /usr/bin/locate
lrwxrwxrwx  1 root root 7 2004-12-11 17:51 /usr/bin/locate -> slocate

Dusty

watch it, hippy! *shakes fist*

lol


The impossible missions are the only ones which succeed.

Offline

#9 2005-03-14 19:22:48

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: How do you recursive search and delete along w/ directories?

:shock:



lol  lol  lol

ps. Skoal is going to get a high score on this thread! *points at all the smileys*


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#10 2005-03-14 19:32:25

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: How do you recursive search and delete along w/ directories?

We should be ashamed of ourselves. Its not proper etiquette to take a Newbie Forum thread off topic.

Sorry about that N3xt, I hope your original questions were answered.

Dusty

Offline

#11 2005-03-14 20:36:56

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: How do you recursive search and delete along w/ directories?

N3xt wrote:

Hey Everyone,

Two things have been bugging me for quite some time, and I was wondering if anyone could answer them.  How do I recursively search for a file throughout the entire computer -I can't seem to use find to do this, it's only limited to the current directory, and how can I recursively delete an entire directory all in one command -Usually I have to rm /dir/* -r && rmdir /dir, which doesn't even get all the hidden contents hmm?

Thanks

As was previously stated,

find / -iname "*partialname*"

would return all files having partialname in the filename.
find works as follows.
the first element in the command, is the directory to start with. It recurses by default, so it will search starting at the root. If you wanted to search only in home dir, then you would put something like /home/user instead of just /
the -iname means "case insensitive" it will match partialname and PARTialName, or any combination thereof.
The last part is the regular expression. I listed the one I most commonly use. It will match anything before and after the string listed (that is what the *s mean). So, it would match FILEpartialname.tar.gz for instance.

to remove a directory, and even all the hidden files in it, you do
rm -rf /path/to/dir
for instance, say you had a directory in your home dir called foo, and you wanted to delete it.
I would type
rm -rf /home/mysuername/foo

the problem you were having with not deleting hidden files is because you were using
rm -r /home/myusername/foo/*

the * is a shell expansion when used like this. It will expand to all the names of the files in that directory. It will NOT expand to hidden files.
consider this.
say the directory foo had the following files in it.

(eliott@tungsten test)$ ls -alR
.:
total 3
drwxrwxr-x  5 eliott eliott 168 Mar 14 12:31 .
drwxrwxr-x  8 eliott eliott 224 Mar 14 12:31 ..
-rw-rw-r--  1 eliott eliott   0 Mar 14 12:31 file1
-rw-rw-r--  1 eliott eliott   0 Mar 14 12:31 file2
drwxrwxr-x  2 eliott eliott  48 Mar 14 12:31 .test
drwxrwxr-x  2 eliott eliott  48 Mar 14 12:31 .test2
drwxrwxr-x  2 eliott eliott  72 Mar 14 12:31 tree1
 
./.test:
total 1
drwxrwxr-x  2 eliott eliott  48 Mar 14 12:31 .
drwxrwxr-x  5 eliott eliott 168 Mar 14 12:31 ..
 
./.test2:
total 1
drwxrwxr-x  2 eliott eliott  48 Mar 14 12:31 .
drwxrwxr-x  5 eliott eliott 168 Mar 14 12:31 ..
 
./tree1:
total 1
drwxrwxr-x  2 eliott eliott  72 Mar 14 12:31 .
drwxrwxr-x  5 eliott eliott 168 Mar 14 12:31 ..
-rw-rw-r--  1 eliott eliott   0 Mar 14 12:31 .file3

if you did rm -rf /home/eliott/test/*
it leave you with the following:

(eliott@tungsten test)$ ls -al
total 2
drwxrwxr-x  4 eliott eliott  96 Mar 14 12:33 .
drwxrwxr-x  8 eliott eliott 224 Mar 14 12:31 ..
drwxrwxr-x  2 eliott eliott  48 Mar 14 12:31 .test
drwxrwxr-x  2 eliott eliott  48 Mar 14 12:31 .test2

See how it deleted the subdirectories, but not the hidden files? And didn't even delete the test dir itself?
Now to delete everything, rm -rf /home/eliott/test does the trick. It removes the directory itself, so naturally everything below it is gone.

there are expansion regexp that will work on hidden files, but it is easy to screw up that way, and it is best avoided. For instance, I accidentally tried to delete hidden files once, and ended up expanding to the .., so it crawled back up my tree and started removing all files. Now you see why * does not naturally expand to include . hidden files...
wink


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#12 2005-03-14 21:43:11

jerem
Member
From: France
Registered: 2005-01-15
Posts: 310

Re: How do you recursive search and delete along w/ directories?

rm -rf whatever deletes the directory and all the stuff it contained.

find /wheretosearch -name 'whattosearch' finds the items matching a  pattern, ie if you want all the file names which contain "music" you would type :

find /whatever -name '*music*'

* represents a joker string (several caracters)
? represents a joker caracter (only one)

find /whatever -name 'pattern' -exec command {} ;

This command executes a command for each file matching the pattern, ie if you want to delete all files with 'music' in the name, you do :

find / -name 'music' -exec rm {} ;

Tip with Arch : when you do a massive upgrade, run a :

find / -name '*.pacnew'

to see which files need to be edited accordngly to the new packages being installed.

the locate command is like find except is looks in a database(faster) rather than in the fs. Drawback : you need to update the database

updatedb
locate

Offline

#13 2005-03-14 22:55:32

smith
Member
From: Crescent City, CA
Registered: 2005-02-19
Posts: 77

Re: How do you recursive search and delete along w/ directories?

One problem with the following command

find /whatever -name 'pattern' -exec command {} ;

is that the exec option will call 'commad' every time a match is found.  That's going to be very slow when a lot of files match 'pattern'.  Best use xargs, which only invokes command once.

find /whatever -name 'pattern' | xargs command

I have nothing to say, and I am saying it.

Offline

#14 2005-03-15 07:32:37

jerem
Member
From: France
Registered: 2005-01-15
Posts: 310

Re: How do you recursive search and delete along w/ directories?

Thanks for the tip. :-)

Offline

Board footer

Powered by FluxBB