Unix locate command for present working directory - unix

The locate command in Unix will return the path related to the attribute given (for example, locate abc_file). What is the command to locate a file in the present working directory structure?

If you use wildcards (as *) locate considers the pattern to be an absolute path. Therefore you could e.g. do:
locate "$PWD*/abc_file"
This will find all files abc_file under your $PWD and its subfolders (as long as they are in the locate database).

On the other hand you can also always use oldschool find:
find . -name abc_file

Related

What command can search for a specific file in my home directory?

Writing a beginner shell script and it's asking for to locate and verify if a file under a specific name is located within my home directory but I'm not sure what command would best fit that.
To find any file in linux, use this command:
find . -name file_name
point means current directory
you can change it to /home/
thanks

Azure databricks %run magic command unable to find file path

I have 2 notebooks under the root folder in my workspace. Calling one notebook from the other with %run magic cmd returns error saying file path not found. This is my command:
%run /Users/name#comp.com/notebookB $arg1=val1 $arg2=val2
Found the solution, turns out the run command needs to be in a cell all by itself - no other code and no comments. I have seen some solutions online say that the cell should not contain any comments, but turns out it should not contain any other code either.
It's really better to use relative names instead of absolute (starting with /):
If both notebooks are in the same folder, then you can refer to another using syntax ./notebook_name
If another is in the folder above, then use ../notebook_name
if another is in the subfolder, then use subfolder/notebook_name

Robot Framework: does not accept relative path to json file [duplicate]

When creating filepaths and URLs, I noticed that many times the path starts with ./ or ~/.
What is the difference between filepaths that start with ./ and ~/?
What do each of them mean?
For the sake of completeness ...
Just path is a file or directory named path in the current directory.
./path is a file or directory named path in the current directory, with the directory spelled out. The dot directory . represents the current directory, and path is the name of the file or directory within this directory.
~/path is a shorthand for $HOME/path where $HOME is a variable which refers to your home directory. Typically your home directory will be somewhere like /home/you or /Users/you where you is your account name. (The command echo "$HOME" will display your home directory.) The expanded value is an absolute path (unless you have messed up the value of $HOME thoroughly), as indicated by the initial slash.
/path is an absolute path which refers to a file or directory named path which is in the root directory /. Every file on Unix is ultimately somewhere in the directory tree which starts with the root directory.
A file name which begins with $ includes the value of a shell variable in its name (like for example $HOME above); you have to know the value of that variable to determine whether it ends up containing a relative or an absolute path. Similarly, ~ at the beginning of a file name gets replaced ("expanded") by the shell to a different string, as outlined above.
(Technically, it's possible for a file name to begin with a literal dollar sign or tilde, too; you would then have to quote or backslash-escape that character to avoid having the shell expand it to something else. This is rather inconvenient, so these file names tend to be rare in practice.)
In the following exposition, we refer to the result of any such replacements, and ignore the complication of possible quoting.
Every file name which begins with / is an absolute path (aka full path) which explains how to reach a particular node starting from the root directory. For example, /var/tmp/you/reminder.txt refers to a file or directory reminder.txt (probably a file, judging from the name; but Unix doesn't care what you call your files or directories) which is in the directory you which is in the directory tmp which is in the directory var which is in the root directory.
Every file name which doesn't begin with / is a relative path which indicates how to reach a particular file or directory starting from the current directory. The special directory .. is the parent directory (that is, the directory which contains this directory) and the special directory . is the current directory. So path/there refers to the file or directory there inside the directory path in the current directory; and (hover the mouse over the gray area to display the spoiler)
there/.././and/back/.. is a (wicked complicated) way to refer to the directory and in the current directory, where we traverse the there directory and then move back to the current directory; then stay in the current directory; then refer to the directory back inside the directory and, but then move back to the parent directory of that, ending up with ./and.
In addition to ~/ for the current user's home directory, some shells and applications allow the notation ~them/ to refer to the home directory of the user account them. Also, some web server configurations allow each user to have a public web site in their directory ~/public_html and the URL notation http://server/~them/ would serve up the site of the user account them for outside visitors.
The current directory is a convenience which the shell provides so you don't have to type long paths all the time. You can, if you want to.
/bin/ls /home/you/Documents/unix-101/directories.txt
is a longwinded but perfectly valid way to say (assuming you are in your home directory),
ls Documents/unix-101/directories.txt
You could also say
cd Documents/unix-101
ls directories.txt
and until you cd again, all your commands will run in this directory.
See What exactly is current working directory? for a longer exposition of this related concept.
A "directory" is sometimes called a "folder" by people who are not yet old enough to prefer the former.
Tangentially, don't confuse the directory name . with the Bourne shell command which comprises a single dot (also known by its Bash alias source). The command
. ./scriptname
runs the commands from the file ./scriptname in the context of the current shell instance, as opposed to in a separate subshell (which is what just ./scriptname does). In other words, this command line invokes the dot command on a file scriptname in the dot directory.
The Bourne shell (and derivatives like Bash, Zsh, etc) use single quotes to prevent variable expansion and wildcard expansion, and double quotes to permit variable expansion, but inhibit wildcard expansion in a string. The quoting rules on Windows are different, and generally use double quotes to keep whitespace-separated values as a single string (and % instead of $ for variable substitutions).
./ means "starting from the current directory". . refers to the current working directory, so something like ./foo.bar would be looking for a file called foo.bar in the current directory. (As a side note, .. means refers to the parent directory of the current directory. So ../foo.bar would be looking for that file one directory above.)
~/ means "starting from the home directory". This could have different meanings in different scenarios. For example, in a Unix environment ~/foo.bar would be looking for a file called foo.bar in your home directory, something like /home/totzam/foo.bar. In many web applications, ~/foo.bar would be looking for a file called foo.bar in the web application root, something like /var/http/mywebapp/foo.bar.
./ is the current directory
~/ is the home directory of the current user
./ means that path is relative to your current position.
~/ means that path is relative to your home directory.
I will explain a simple example of it. As developers mentioned:
./ is current directory.
~/ is the home directory of the current user.
How both of the file path expressions can help us? Suppose you want to execute a script (.sh) and you're in the same directory where file exists then you can simply do it ./filename.sh
I mostly use ~/ to access my home directory files like .bashrc when I want to add any config in it. It's easier since the file path expression (for home directory) feels much easier and makes accessibility to the file from anywhere, without worrying about the path or changing the path.
. represents current directory
.. represents the parent directory
~ represents the home directory for the current user. Home directory is also represented by HOME env variable. you can do echo $HOME on the shell to see it.
These are generally used to specify relative paths. The / in the end of each notation is a separator that you can use when using these notations together.
Ex:
$ cd ../.. # Go 2 directories backwards
$ cd ~ # Takes you to $HOME directory
$ cd . # Does nothing :) As it literally means go to the directory that you are already present in.
$ cd ~/dir1 $ go to `$HOME/dir1`
On Unix, in any directory if you do ls -a you would see that . and .. will be mentioned (even for empty directory). Like mentioned, these have special meaning and are generated by default in Unix systems and are generally helpful to specify relative paths (i.e, path to a different directory relative to your current directory)
cd command is harmless. So, just play around by combining notations with cd command. You will eventually get a grip of them.

Creating a terminal command in Minix

I want to create a command that runs an executable created by compiling a c program. I couldn't find a proper solution. Let's say I have a file named myprogram.c and compile it and have myprogram as . I want to type myprogram in any folder in my system and run it. How can I achieve this?
First find out what your PATH is
echo $PATH
For you this outputs
/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin/usr/X11R7/bin:usr/X11R‌​‌​6/bin:/usr/local/sbin:/usr/local/bin
Then assuming your program is in the /usr/myprog directory, append /usr/myprog to your PATH (don't forget to separate directories with a colon :)
export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin/usr/X11R7/bin:usr/X11R‌​‌​6/bin:/usr/local/sbin:/usr/local/bin:/usr/myprog
Doing this tells the system when you don't specify an absolute path (like ./myprogram) to look in all the directories in PATH. It is good to add the absolute path of your executable to PATH because adding . to your PATH is frowned upon by some (see this question).
You have to add it in your PATH from your shell rc file
You place the executable into a directory that your shell already searches for programs, or you add your program's location to that list.
the $PATH environment variable contains this information. You can add myProgram's location to it, i.e. export PATH=$PATH:/new/dir, or just print out $PATH and copy myProgram into one of the paths listed there already.

Seemingly invalid No such file or directory error

I'm attempting to open a directory in Unix. If I enter the command
ls
I see the directory listed in my current directory but if I endter
cd [directory_name]
I get the error
No such file or directory
I'm also not able to auto complete the directory name using the 'tab' key. Does anyone know what may be causing this?
Check whether you are using the right capitalization? It's case sensitive. Add this to your ~/.inputrc if you want bash to not care about the case of the file.
set completion-ignore-case on
This is example:
user#stackoverflow:~$ ls
users questions file.txt
user#stackoverflow:~$ cd /questions
user#stackoverflow:~/questions$
Make sure that you're trying to access a valid folder and not a file.
To further explain:
List the current directory's contents (either one):
ls .
ls
List the home directory's contents (wherever you are):
ls ~
List the root directory's contents (wherever you are):
ls /

Resources