Linux user file path

What is a full path name?

I would like to understand what full path name really means. I have a file in my home directory called my_script. I assumed the full path name is ./my_script, meaning that it is in the root folder, but I am not sure. Can someone please enlighten me on that?

Can you ask «is it possible to write a command that will display the list of files with their names?» in another new question?

6 Answers 6

No, your assumption is wrong. The full path name for my_script file from your home directory is: /home/your_user_name/my_script . When you type ./my_script in terminal you actually try to execute the script (if is executable) and it will be executed only if your current working directory is /home/your_user_name/ . To execute the script you can use also the full file path which is, as I said /home/your_user_name/my_script .

It is believed that a UNIX path name looks and feels like Internet addresses, thus result into compatibility. The full path name of the current working directory can be found in terminal by using the following command:

To find out the full path for your user home directory, you can use:

echo ~ echo $HOME echo /home/$USER 

The above three commands are equivalent.

To find out the full path name for a file you can use readlink command. For example, in your case:

The home folder need not reside in /home, so /home/$USER is not necessarily equivalent with the other two examples. (e.g. /root for the root user)

Full path name really means the full path to that file or folder from the filesystem’s / directory.

For example, the full path to your script is:

/home/your_username/my_script 

Or, the full path name to the grep executable is

As for the ./my_script , the symbol . stands for the current directory, so you actuallly say «Look under the current directory for a file or folder named my_script»

In order to understand the full path, you must first know the starting point.

The root directory / it is the starting point of *nix based operating systems. It contains all the other directories both system’s and user’s.

User’s home directory /home/USERNAME/ or ~/ for short, contains user’s files and directories. For example Pictures, Music, Documents, etc. Each of these directories is referenced as /home/USERNAME/DIRECTORY for example Documents is located at /home/USERNAME/Documents .

Like with directories, files are referenced in the same way, for example a file named my_script located at the home directory of the user sosytee can be referenced using the full path /home/sosytee/my_script or ~/my_script for short.

Both files and directories can be referenced/accessed using their full paths from everywhere in the system. Additionally one can access them using only their name if it is in the same directory. For example if the user is at ~/ when using the terminal, he can access my_script file by using just my_script .

Читайте также:  Показать содержимое папки linux

Additionally one can access directories and files by using their name only, if they are placed at his PATH variable. You can see what is store in PATH by using echo $PATH .

Simple examples on how to access files using the command line:

  • The user is currently at /home/USERNAME/ and wants to use the cat command on a file located at /home/USERNAME/Documents named foo.txt:
  • The user is inside ~/Documents and wants to run a script named foo.sh located ad ~/Scripts/Foo : sh ../Scripts/Foo/foo.sh or sh ~/Scripts/Foo/foo.sh

by all means this is just a summary.

Источник

Get the Full Path of a File in Linux

Every file and folder in Linux has a path that directs the user to it. This path is required for programs and scripts to locate and access files. There are various ways to locate the path to a file or folder if you need to. We can get a full file path with different commands on a Linux machine. In other words, if its arguments were supplied, this command would return the pathnames of the files that would be run in the current context.

In this tutorial, we’ll show you how to obtain a file’s complete path in Linux.

The file path in Linux

In Linux, there are two different kinds of paths: absolute and relative. A forward slash (/) designates the root directory, which is where an absolute path always begins. On the other hand, a relative route begins in the current directory.

In the file system, the root directory is the one with the highest level. The forward slash (/) symbol in Linux designates the root directory.

For example, the absolute path to the file “script” is /home/webmaster/script. If we are under /home/webmaster directory, the relative path to /home/webmaster/script is ./script. We can use cat script or vim script commands to view the content of the file “script”.

The “readlink” command is used to resolve symbolic links. We will see how to use the “readlink” command to find the full path. In the following example, we will use the “readlink” command to get the full path of the filename “new.txt”. In the output, we will get the actual filepath,

Example

Output

Using the realpath command

The “realpath” command is used for resolving the absolute file names. In the following example, we will use the “realpath” command to get the full path of the filename “hsperfdata_root”. In the output, we will get the full path of the file,

Example

Output

Using the ls command

The “ls” command when used with the environment variable “PWD” prints the full path of the file. In the below example, we will use the “ls” command to display the full path of the filename “nates.txt”. In the output, we will get the full path of the file with other details such as file creation date, time, and permissions,

Читайте также:  Linux команды консоли сети

Example

Output

-rw-r--r-- 1 webmaster webmaster 0 Oct 16 07:31 /tmp/nates.txt

Using the find command

The “find” command looks for files in a directory hierarchy, this command can also be used to print a file’s absolute path. We can use this command to look for files or directories that meet specific requirements. The result will be a list of all files and folders, together with their respective file paths, that fit your search parameters.

In the following example, we see how to use the “find” command to get the full path of a filename “nates.txt”. In the output, we will get the full path of the file,

Example

$ find $PWD -type f -name nates.txt

Output

Conclusion

In this article, we learned about how to determine a file’s full path, and we explored some actual instances. The use of the “readlink” and “realpath” commands was covered initially. Later we implemented “ls” and “find” commands to get the full path of a file in Linux.

These are the several scenarios that are used to get the full path of a file in Linux. I hope you find these examples of the commands useful and that it’s easy to learn and use Linux.

Источник

How to set your $PATH variable in Linux

Telling your Linux shell where to look for executable files is easy, and something everyone should be able to do.

A path through nature

Thomas Hendele on Pixabay (CC0). Modified by Opensource.com. CC BY-SA 4.0.

Being able to edit your $PATH is an important skill for any beginning POSIX user, whether you use Linux, BSD, or macOS.

When you type a command into the command prompt in Linux, or in other Linux-like operating systems, all you’re doing is telling it to run a program. Even simple commands, like ls, mkdir, rm, and others are just small programs that usually live inside a directory on your computer called /usr/bin. There are other places on your system that commonly hold executable programs as well; some common ones include /usr/local/bin, /usr/local/sbin, and /usr/sbin. Which programs live where, and why, is beyond the scope of this article, but know that an executable program can live practically anywhere on your computer: it doesn’t have to be limited to one of these directories.

When you type a command into your Linux shell, it doesn’t look in every directory to see if there’s a program by that name. It only looks to the ones you specify. How does it know to look in the directories mentioned above? It’s simple: They are a part of an environment variable, called $PATH, which your shell checks in order to know where to look.

View your PATH

Sometimes, you may wish to install programs into other locations on your computer, but be able to execute them easily without specifying their exact location. You can do this easily by adding a directory to your $PATH. To see what’s in your $PATH right now, type this into a terminal:

Читайте также:  Linux add root users

You’ll probably see the directories mentioned above, as well as perhaps some others, and they are all separated by colons. Now let’s add another directory to the list.

Set your PATH

Let’s say you wrote a little shell script called hello.sh and have it located in a directory called /place/with/the/file. This script provides some useful function to all of the files in your current directory, that you’d like to be able to execute no matter what directory you’re in.

Simply add /place/with/the/file to the $PATH variable with the following command:

export PATH=$PATH:/place/with/the/file

You should now be able to execute the script anywhere on your system by just typing in its name, without having to include the full path as you type it.

Set your PATH permanently

But what happens if you restart your computer or create a new terminal instance? Your addition to the path is gone! This is by design. The variable $PATH is set by your shell every time it launches, but you can set it so that it always includes your new path with every new shell you open. The exact way to do this depends on which shell you’re running.

Not sure which shell you’re running? If you’re using pretty much any common Linux distribution, and haven’t changed the defaults, chances are you’re running Bash. But you can confirm this with a simple command:

That’s the «echo» command followed by a dollar sign ($) and a zero. $0 represents the zeroth segment of a command (in the command echo $0, the word «echo» therefore maps to $1), or in other words, the thing running your command. Usually this is the Bash shell, although there are others, including Dash, Zsh, Tcsh, Ksh, and Fish.

For Bash, you simply need to add the line from above, export PATH=$PATH:/place/with/the/file, to the appropriate file that will be read when your shell launches. There are a few different places where you could conceivably set the variable name: potentially in a file called ~/.bash_profile, ~/.bashrc, or ~/.profile. The difference between these files is (primarily) when they get read by the shell. If you’re not sure where to put it, ~/.bashrc is a good choice.

For other shells, you’ll want to find the appropriate place to set a configuration at start time; ksh configuration is typically found in ~/.kshrc, zsh uses ~/.zshrc. Check your shell’s documentation to find what file it uses.

This is a simple answer, and there are more quirks and details worth learning. Like most everything in Linux, there is more than one way to do things, and you may find other answers which better meet the needs of your situation or the peculiarities of your Linux distribution. Happy hacking, and good luck, wherever your $PATH may take you.

This article was originally published in June 2017 and has been updated with additional information by the editor.

Источник

Оцените статью
Adblock
detector