Mac terminal document search - macos

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

Related

Command "locate" in MAC terminal

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)

Running all files in directory, what is go run ./cmd/*.go?

I am currently looking at this project (Mattermost) that has a certain line in the makefile that I'm confused about:
$(GO) run $(GOFLAGS) $(GO_LINKER_FLAGS) ./cmd/platform/*.go --disableconfigwatch &
What is the meaning of ./cmd/platform/*.go? What specific files are executed? The program executes correctly when I type it in the terminal.
I am trying to enter the command line arguments in an IDE but I need a specific entry file.....
Wikipedia
glob (programming)
In computer programming, in particular in a Unix-like environment,
glob patterns specify sets of filenames with wildcard characters. For
example, the Unix command mv *.txt textfiles/ moves (mv) all files
with names ending in .txt from the current directory to the directory
textfiles. Here, * is a wildcard standing for "any string of
characters" and *.txt is a glob pattern. The other common wildcard is
the question mark (?), which stands for one character.
Clearly, ./cmd/platform/*.go, starting in the current directory, looks in the cmd/platform directory for files matching the wildcard *.go.
The ls ./cmd/platform/*.go command will list the files on Linux.
So, the go run ./cmd/platform/*.go command compiles and runs these Go (*.go) source files. See Command go documentation: Compile and run Go program.

How to go to a subdirectory in CMD?

How can I go to a subdirectory without specifying its whole path?
So when I am in
C:/users/USERNAME/desktop/project/build/
How can I then navigate to
C:/users/USERNAME/desktop/project/build/directory 1/
I tried
cd /directory 1
But this results in
The system cannot find the path specified.
I want to use something as simple as the .. command, which goes one directory back without specifying its whole path. Is there something similar to change to a subdirectory?
On Windows the directory separator is \ ... the backslash character. The slash character / is used on Windows for parameters/options of a command/executable/script.
For compatibility reasons like #include instructions in C/C++/C# with a relative path using / as directory separator Windows accepts also paths with / and automatically convert them to \ when a string is interpreted as file/folder name with a path. That is done by the file IO functions used by Windows executables on accessing the file system of a storage device.
A path starting with a backslash is a path relative to root of current drive.
For that reasons cd /Directory 1 is executed as cd /D irectory 1 which means with interpreting /D at beginning of the directory argument string as optional option /D by command CD to change also the drive and the rest of the directory argument string not enclosed in double quotes as name of the subdirectory to change current directory to. The subdirectory irectory 1 does not exist on your machine.
If the directory name starts with any other character than letter D and using / instead of \ at beginning of the directory argument string like on using cd /Folder 1, there would be executed cd "C:\Folder 1" with drive C: being the current drive.
There are three types of relative paths:
Path is relative to current directory if path starts with .\ or with directory name.
Path is relative to parent directory of current directory if path starts with ..\.
Path is relative to root of current drive if path starts with \.
.\ and ..\ can be also used anywhere inside a path after a directory separator multiple times and Windows automatically resolves the path to an absolute path.
So correct would be using cd "Directory 1", or not recommended cd Directory 1.
The help of command CD output on running cd /? explains that the command CD does not require enclosing a path with one or more spaces in double quotes as usually required because a space is usually interpreted as separator between arguments to pass to a command, executable, or script.
It is nevertheless best practice to always enclose the path in double quotes because of some other characters require path being enclosed in double quotes to interpret (nearly) all characters in path as literal characters.
For example a directory has the name Test & Doc. With cd Test & Doc the Windows command processor executes cd Test and next tries to find an executable or script with Doc and Doc.* having a file extension listed in environment variable PATHEXT in current directory or any directory listed in environment variable PATH because of the ampersand is interpreted as unconditional AND operator for execution of multiple commands on a single command line. But on using cd "Test & Doc" the entire double quoted string is interpreted as name of a directory to change to by the Windows command processor.
BTW: All internal (included in cmd.exe ... the Windows command processor) and external commands (executables installed by default in %SystemRoot%\System32) can be started with the parameter /? in a command prompt window to get displayed the help for this command.
cd /?
cmd /?
pushd /?
popd /?
More details on Windows commands can be found at
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line
Single line with multiple commands using Windows batch file
The Microsoft documentation about Naming Files, Paths, and Namespaces offers even more details about file names and how paths are interpreted by Windows kernel. Everybody writing program or script code for applications or scripts executed on Windows should have read this documentation page at least once from top to bottom.

what does -d command with command unzip in shell scripting

I am using the following command to unzip a file
unzip "/cygdrive/c/auto/-"$new"-test.zip" -d "/cygdrive/c/auto/"
Just want to know the use of -d "/cygdrive/c/auto/"
what is -d doing here
i have searched many web pages but fnd out that it uncompress the file.
From man unzip:
[-d exdir]
An optional directory to which to extract files. By default,
all files and subdirectories are recreated in the current direc-
tory; the -d option allows extraction in an arbitrary directory
(always assuming one has permission to write to the directory).
This option need not appear at the end of the command line; it
is also accepted before the zipfile specification (with the nor-
mal options), immediately after the zipfile specification, or
between the file(s) and the -x option. The option and directory
may be concatenated without any white space between them, but
note that this may cause normal shell behavior to be suppressed.
In particular, ``-d ~'' (tilde) is expanded by Unix C shells
into the name of the user's home directory, but ``-d~'' is
treated as a literal subdirectory ``~'' of the current direc-
tory.
-d is your destination directory
Next time, you can use "man unzip" in your ssh.

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