linux find not working from other mount points - linux

This works:
:/# find . -name *gedit*
It finds all given files on local disk as well as a backup disk mounted on /media/backup/root
However, change directory to /media/backup/root/ and the same command does not work:
:/media/backup/root# find . -name *gedit*
It finds nothing.
Why?

You need quotes around your search term
:/media/backup/root# find . -name "*gedit*"

The first parameter to find (in this case .) tells find where it should start searching.
So when your current directory is / (The root of the file system) the whole system gets searched. Whereas when your current directory is /some/other/path only this path downward is searched.
If you want to always search the whole file system use
find / -name "*gedit*"
The quotes are important, they prevent the shell from expanding the *.

Related

How does the find command search for files

Quarrying out from another thread Move file that has aged x minutes, this question came up:
How does the find command found typically in Linux search for files in the current directory?
Consider a directory that contains a fairly large amount of files, then:
Firstly find MY_FILE.txt returns immediately and secondly find . -name MY_FILE.txt takes much longer.
I used strace -c to see what happens for both and I learned that the second command invokes a directory scan, which explains why it's slower.
So, the first command must be optimized. Can anybody point me to the appropriate resource or provide a quick explanation how this might be implemented?
The syntax for find is find <paths> <expression>, where paths is a list of files and directories to start the search from. find starts from those locations and then recurses (if they're directories).
When you write find . -name MY_FILE.txt it performs a recursive search under the ./ directory. But if you write find MY_FILE.txt then you're telling it to start the search at ./MY_FILE.txt, and so it does:
$ strace -e file find MY_FILE.txt
...
newfstatat(AT_FDCWD, "MY_FILE.txt", 0x556688ecdc68, AT_SYMLINK_NOFOLLOW) = -1 ENOENT (No such file or directory)
...
(No such file or directory)
: No such file or directory
+++ exited with 1 +++
Since the path doesn't exist, it only takes a single system call to determine that there's no such file. It calls newfstat(), gets a No such file or directory error, and that's that.
In other words, find MY_FILE.txt isn't equivalent to find . -name MY_FILE.txt. Heck, I wouldn't even call it useful because you're not asking it to search. You're just asking it to tell you if MY_FILE.txt exists in the current directory or not. But you could find that out by simply calling ls MY_FILE.txt.
Here's the difference:
[~]$ cd /usr
[/usr]$ find . -name sha384sum
./bin/sha384sum
[/usr]$ find sha384sum
find: ‘sha384sum’: No such file or directory
The first one performs a recursive search and finds /usr/bin/sha384sum. The second one doesn't recurse and immediately fails bcause /usr/sha384sum doesn't exist. It doesn't look any deeper. It's done in a nanosecond.

Linux "find" returns all files

A few days ago I was reading about the Linux find tool and based on that I issued the following command to see if I have the Python.h file:
find . 'Python.h'
The problem is that all files in current dir and subdirs are returned. Shouldn't I get what I'm looking for?
You left out the parameter specifier -name:
find ./ -name 'Python.h'
find will recurse through all directories in the current directory. If you just want to see whether you have a file in the current directory, use ls:
ls Python.h
Use -name switch:
find . -name 'Python.h'
Otherwise it takes the name as location to look at.

Fast way to find file names in Linux and specify directory

This command is slow: find / -name 'program.c' 2>/dev/null
1) Any faster alternatives?
2) Is there an alternative to the above command to search for a file within a specific nested directory (but not the entire system)?
The first / in your command is the base directory from which find will begin searching. You can specify any directory you like, so if you know, for example, that program.c is somewhere in your home directory you could do find ~ -name 'program.c' or if it's in, say, /usr/src do find /usr/src -name 'program.c'
That should help with both 1 and 2.
If you want a command that's not find that can be faster you can check out the mlocate stuff. If you've done a recent updatedb (or had cron do it for you overnight) you can do locate <pattern> and it will show you everywhere that matches that pattern in a file/directory name, and that's usually quite fast.
For fast searching, you probably want locate
It is usually setup to do a daily scan of the filesystem, and index the files.
http://linux.die.net/man/1/locate
although locate & updatedb is for the whole system, the search usually is faster.

search/find a folder that has a name with integer numbers in linux from terminal

I am using fedora 15 on my machine. Actually i am trying to find the folders with name like apache-tomcat-7.0.37 by searching in the entire file system
Actually i had some folders like below in the path /opt/tomcat/
apache-tomcat-7.0.37
apache-tomcat-6.0.34
apache-tomcat-7.0.67
.........
And some folders at /usr/share/tomcat/
apache-tomcat-4.0.7
apache-tomcat-6.0.4
apache-tomcat-8.0.6
.........
So i want is to locate/find/search all these folder paths from linux terminal by using a command.
I have googled a lot and got some commands like locate and find as below
find / -name apache-tomcat*
locate apache-tomcat
The above commands are listing all the folder including extra unwanted information of folders, so actually what i want is , need to search for only for the folders that has the name like apache-tomcat-x.x.x,apache-tomcat-xx.xx.xx
Here in the folder name the starting words apache-tomcat is same and only the integer part (version number) changes. So i want to find all the folders with different version number like by using regular expressions in place of integer numbers to find the folders
so can anyone please let me know how to search the folder of the above scenario by using a command with regular expressions or someting like that which find all the folders with name apache-tomcat-xx.x.xxx.....
This should find all files, diretories, links, etc. that have the pattern apache-tomcat-X.Y.Z where X, Y, and Z are integers.
find . -regextype sed -regex ".*/apache-tomcat-[0-9]\+\.[0-9]\+\.[0-9]\+"
If you're looking only for directories, use this variant:
find . -type d -regextype sed -regex ".*/apache-tomcat-[0-9]\+\.[0-9]\+\.[0-9]\+"
If you want to search the entire system starting at /, use this variant:
find / -type d -regextype sed -regex ".*/apache-tomcat-[0-9]\+\.[0-9]\+\.[0-9]\+"
You can provide a suitable regular expression to locate in order to do a fast search of your entire system:
locate -b --regex "apache-tomcat-[0-9]+.[0-9]+.[0-9]+$"
As with any use of locate, the file database it uses will need to be sufficiently up-to-date. If you have sufficient permissions, you can do sudo updatedb to force an update.

Newbie: linux command

Two questions to ask:
1. I am using scp command to copy a file from a remote server, I am wondering how can I specify the place where to paste the copied file on my local computer?
for example, if I wanna copy a test.txt file from a remote server and paste it on my local computer under /home/myname/tmp/ what is the proper command?
is it
scp SERVER_ADDRESS /home/myname/tmp/
2. If I want to search a file whose name contain text of "test" , what is the command I should use? I mean search for any file with name test , ('_' is a wildcard)
--------------------------- update ------------------------
what is the difference between "find" and "grep"?
1:
scp SERVER_ADDRESS:/path/to/remote/file.txt /path/to/local/file.txt
2:
find . -name "*test*"
This will search for files/directories containing "test" anywhere in the filename. The search will start from the current directory . To search in another path, use find /path/ -name "*test*". If you only want to search in files, that is, exclude directories, then add -type f before the -name option.
First man scp is your friend (as are all man pages in general).
Yes: in full, that'd be like scp server:/path/to/file.txt /local/path/.
Your main options here are:
locate test (if you have locate installed and its database is up to date)
-or-
find /path/name -name '*test*' to find any named files inside the /path/name directory and all its children.

Resources