You are not logged in.
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 ?
Thanks
Offline
# find / -name "myfile*"
and
# rm -r ~/dir
both work fine for me
Offline
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
locate filenameglob
I'd recommend slocate... same thing, but security enhanced
Offline
dusty:~ $ ls -l /usr/bin/locate
lrwxrwxrwx 1 root root 7 2004-12-11 17:51 /usr/bin/locate -> slocate
Dusty
Offline
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
Are you sure you want to delete user phrakture? [Y/N]
*hesitates*
Dusty
Offline
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
:shock:
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
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
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 ?
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...
"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
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
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
Thanks for the tip. :-)
Offline