Command "locate" in MAC terminal - macos

I started to study CLI on my computer (iMAC) and reached command locate. When I use this command search carried out not in the current directory but everywhere. In addition, this command scans all system and program files but don't scans Downloads or for example in the Music library.
In this regard, I had two questions:
If I don't specify a search directory where exactly will this command search my file (at root directory or somewhere else)?
How to specify search directory for this command (for example, I need to find a file that is only in downloads)?

Try the man page:
http://man7.org/linux/man-pages/man1/locate.1.html
You might also be interested in find:
http://man7.org/linux/man-pages/man1/find.1.html
I use find much more than locate, but the answers to your questions are in the docs.

As it turned out, on my MAC the Downloads folder for the user "everyone" is generally closed and the system files were open for reading! WOW! After changing the rights and updating the database everything worked as it should. Now the question remains: it was only I who had such default rights?

The Problem: You are looking for a Mac cli tool for optimized searching action (search engine indexing) , the locate tool (familiar from unix/linux) is not supported on mac (There's a workaround to enabling it but this is another topic)
Solution: Try mdfind terminal command (which is similiar to locate command in linux).
(From documentation) The mdfind command consults the central metadata store and returns a list of files that match the given metadata query. The query can be a string or a query expression.
Example:
/* input: */
mdfind -name configuration.yml
/* output: */
/Users/someUser/x/y/z/configuration.yml
/Users/someUser/a/b/c/d/e/f/g/h/i/configuration.yml
/System/Library/someApp/someVersion/someDir/configuration.yml
The optional flags are:
-0 Prints an ASCII NUL character after each result path. This is useful when used in conjunction with xargs -0.
-live Causes the mdfind command to provide live-updates to the number of files matching the query. When an update causes the query results to change the number of matches
is updated. The find can be cancelled by typing ctrl-C.
-count Causes the mdfind command to output the total number of matches, instead of the path to the matching items.
-onlyin dir
Limit the scope of the search to the directory specified.
-name fileName
Searches for matching file names only.
-literal Force the provided query string to be taken as a literal query string, without interpretation.
-interpret Force the provided query string to be interpreted as if the user had typed the string into the Spotlight menu. For example, the string "search" would produce the
following query string:
(* = search* cdw || kMDItemTextContent = search* cdw)

Related

Why is `ls -lF` listing files with the prefix "~$" not present in the directory?

I'm currently practicing basic Shell Commands in WSL, Windows Subsystem for Linux (I do not have a linux system but I want to get familiar with commands).
I start a bash session on the command prompt window and navigate to my desktop using cd . In desktop I noticed that after using ls -lF some files with the prefix ~$ appear, such as: '~$executable.x'* or '~$file.txt'
These files are not currently present under the desktop directory, but I was able to remember that they were at one point (varying from a week to months ago).
When I do the same process in powershell windows (not using linux commands) I noticed that files displayed match the desktop and no extra files are listed.
I was wondering if anyone could explain what ~$ means in this context? my intuition is telling me they are backed up files that are somehow hidden in the desktop. After googling, all I could find is that ~ reefers to the home. I also understand that $ is the default prompt symbol for the bash shell when it is waiting for me to type something, but I'm still confused on why it would show up as a prefix for the name of a file.
Hope I made my question clear.
I'm currently reading "Linux® Command Line and Shell Scripting BIBLE" by Blum and Bresnahan but I could not find an answer there, this is my last resource after many googling attempts. Any other source for more information on the topic would be helpful.
On Windows, files that start with ~ are used for hidden files. More specifically,, the prefix ~$ are often used as backups for programs, should they crash before writing updates to a file (e.g. Microsoft Word, etc.)
From Wikipedia:
The tilde symbol is used to prefix hidden temporary files that are created when a document is opened in Windows. For example, when you open a Word document called “Document1.doc,” a file called “~$cument1.doc” is created in the same directory. This file contains information about which user has the file open, to prevent multiple users from attempting to change a document at the same time.
See: Why does Word make temporary files?
Relevant superuser question: https://superuser.com/questions/405257/what-type-of-file-is-file

How do you match an exact file extension under Windows/command line/file open dialog?

I am trying to get a directory listing of only files with a given extension. At first blush this seems to be a simple thing to do, however check out this simple example:
C:\CODE\metcal>dir /b *.exe
metcal.exe
metcal.exe1
Notice that this returns metcal.**exe** and metcal.**exe1** as matches.
With python files a similar thing happens:
C:\CODE\metcal>dir /b *.py
metcal.py
metcal.pyc
Notice again Windows has determined that *.py takes anything that starts with *.py so it captures the .pyc files as well.
Is there a way to get only the extensions that match exactly? In the above python files example I would like the following to occur (obviously with the correct syntax substituted for *.py)
C:\CODE\metcal>dir /b *.py
metcal.py
As a note the matching under Windows not as simple as it seems.
*.exe matches foo.exe, foo.exe1, foo.exeabcde but not foo.exe.bak
There are other questions on SO that are similar that are related to long/short file names. The *.py and *.pyc example here should not introduce name mangling machinery.
**I have experimented on XP and Win7 machines and this behavior is not consistent at the cmd Prompt and file open dialogs. This inconsistant behavior makes me suspect this problem is related to settings of somekind. **
It's because windows wildcards on extensions check both long and short names as explained in this answer:
https://superuser.com/questions/238900/winxp-dir-command-3-and-4-char-extensions-are-the-same#238930
Solution there is to disable 8.3 names creation and then striping them on ntfs volumes which will also improve performance.
Microsoft Docs: Fsutil 8dot3name
Remarks:
Permanently removing 8dot3 file names and not modifying registry keys that point to the 8dot3 file names may lead to unexpected application failures, including the inability to uninstall an application. It is recommended you first back up your directory or volume before you attempt to remove 8dot3 file names.
So if you want to get only those extensions (.py and .pyc), you should try like this way :
#echo off
dir /b *.py*
pause
You can use the Unix ls command from the Windows Subsystem for Linux to do this, assuming you have WSL installed. It's freely available from Microsoft. From your Windows command prompt, type wsl followed by the ls command to list the files you want.
So, for your example, wsl ls metcal.py returns only metcal.py. To get the same results as you're seeing with dir, use wsl ls metcal.py*.

Search files through case sensitive extension on command line

Search files through case sensitive extension on the Windows command line. I want the search a list of jpg files in my directory where some files has extension xxx.jpg and some yyy.JPG .
Here I want the list of all files who has extension yyy.JPG
Any help.
Thanks in advance
dir *.jpg|findstr /e /L ".JPG"
shoud deliver that list; only when the end of the lines literally match the string will findstr allow the line through.
command.com doesn't care about case, but the DOS API does pay attention. So, writing a simple DOS program to search the files will allow you to match what you want.
You can also use most other programming languages (even BASIC!) to get a directory, and it will take you literally. You could even get what you want by getting a directory listing from command.com and then piping it through the find command:
dir | find ".JPG"
Since you asked this on Stackoverflow (programmer's site) it's assumed you want to write a program. If not, youll want to try superuser.com instead.

Mac terminal document search

I have a document I want to find using the terminal on my mac. While using the cd and ls and tab tab commands, what is the command to search for a folder or document in terminal?
Thus, to locate a file typically found on an HFS Extended volume on a server that has netboot clients, enter:
find / -name "Mac NC #" -print &
This should return all occurrences of files or directories on "/" whose names include the string specified in quotes, and print it to standard io (the screen). The ampersand "&" specifies to do this in the background.
Note: In this example, the search string is in quotes; these quotes are necessary because of the space characters included in the string. Searching for a name that is all one word with no illegal characters, such as "hostconfig", does not require quotes. As in traditional BSD unix, the backslash character, "\", may be used before an illegal character to cause it to be parsed as part of the search string:
find / -name Mac\\ NC\\ # -print &
You can also use the "locate" command, which is easy to use and faster than find. Simply type "locate" followed by the name or part of a name of files you are looking for and press Return. You should get a list displaying the paths to those files. The locate command searches a database of files on the drive, which can get out of date. If you are searching for a file that you know exists but is not found, you can run the 'weekly' script manually to update this database. To do so type the following command:
sudo sh /etc/weekly
Press Return, enter your password, and the weekly script will run and update your locate database.
Execute "man find" or "man locate" at the command line to get more information on using these commands.
source: http://support.apple.com/kb/TA25275

Find file in directory from command line

In editors/ides such as eclipse and textmate, there are shortcuts to quickly find a particular file in a project directory.
Is there a similar tool to do full path completion on filenames within a directory (recursively), in bash or other shell?
I have projects with alot of directories, and deep ones at that (sigh, java).
Hitting tab in the shell only cycles thru files in the immediate directory, thats not enough =/
find /root/directory/to/search -name 'filename.*'
# Directory is optional (defaults to cwd)
Standard UNIX globbing is supported. See man find for more information.
If you're using Vim, you can use:
:e **/filename.cpp
Or :tabn or any Vim command which accepts a filename.
If you're looking to do something with a list of files, you can use find combined with the bash $() construct (better than backticks since it's allowed to nest).
for example, say you're at the top level of your project directory and you want a list of all C files starting with "btree". The command:
find . -type f -name 'btree*.c'
will return a list of them. But this doesn't really help with doing something with them.
So, let's further assume you want to search all those file for the string "ERROR" or edit them all. You can execute one of:
grep ERROR $(find . -type f -name 'btree*.c')
vi $(find . -type f -name 'btree*.c')
to do this.
When I was in the UNIX world (using tcsh (sigh...)), I used to have all sorts of "find" aliases/scripts setup for searching for files. I think the default "find" syntax is a little clunky, so I used to have aliases/scripts to pipe "find . -print" into grep, which allows you to use regular expressions for searching:
# finds all .java files starting in current directory
find . -print | grep '\.java'
#finds all .java files whose name contains "Message"
find . -print | grep '.*Message.*\.java'
Of course, the above examples can be done with plain-old find, but if you have a more specific search, grep can help quite a bit. This works pretty well, unless "find . -print" has too many directories to recurse through... then it gets pretty slow. (for example, you wouldn't want to do this starting in root "/")
I use ls -R, piped to grep like this:
$ ls -R | grep -i "pattern"
where -R means recursively list all the files, and -i means case-insensitive. Finally, the patter could be something like this: "std*.h" or "^io" (anything that starts with "io" in the file name)
I use this script to quickly find files across directories in a project. I have found it works great and takes advantage of Vim's autocomplete by opening up and closing an new buffer for the search. It also smartly completes as much as possible for you so you can usually just type a character or two and open the file across any directory in your project. I started using it specifically because of a Java project and it has saved me a lot of time. You just build the cache once when you start your editing session by typing :FC (directory names). You can also just use . to get the current directory and all subdirectories. After that you just type :FF (or FS to open up a new split) and it will open up a new buffer to select the file you want. After you select the file the temp buffer closes and you are inside the requested file and can start editing. In addition, here is another link on Stack Overflow that may help.
http://content.hccfl.edu/pollock/Unix/FindCmd.htm
The linux/unix "find" command.
Yes, bash has filename completion mechanisms. I don't use them myself (too lazy to learn, and I don't find it necessary often enough to make it urgent), but the basic mechanism is to type the first few characters, and then a tab; this will extend the name as far as it can (perhaps not at all) as long as the name is unambiguous. There are a boatload of Emacs-style commands related to completion in the good ol' man page.
locate <file_pattern>
*** find will certainly work, and can target specific directories. However, this command is slower than the locate command. On a Linux OS, each morning a database is constructed that contains a list of all directory and files, and the locate command efficiently searches this database, so if you want to do a search for files that weren't created today, this would be the fastest way to accomplish such a task.

Resources