- Mastering File Path Handling in Linux: Tips and Tricks to Extract File Names
- Using basename to retrieve the filename
- Using dirname to retrieve the directory path
- Using readlink to retrieve the directory path from a relative path or filename
- Maximum length for file names and path names
- Using realpath to show the full file path
- Extracting the filename from a file path in C
- Using zsh to remove the file path and extension
- Other simple code samples for extracting file names from file paths in Linux
- Conclusion
- Absolute Path vs Relative Path in Linux
- Understanding Absolute and Relative Path in Linux
- 1. Absolute Path
- 2. Relative Path
- Absolute Path vs Relative Path
- Conclusion
- About the author
- Denis Kariuki
- Get Absolute File Path in Linux
- Use readlink to get file path
- Use realpath to get full file path
- Use the find command to get the absolute file path
- Print full path with the ls command
- Conclusion
Mastering File Path Handling in Linux: Tips and Tricks to Extract File Names
Learn how to easily extract file names from file paths in Linux, Unix, and macOS using tools such as basename, dirname, and readlink. Follow file naming conventions and best practices for hassle-free file path handling.
- Using basename to retrieve the filename
- Using dirname to retrieve the directory path
- Using readlink to retrieve the directory path from a relative path or filename
- Maximum length for file names and path names
- Using realpath to show the full file path
- Extracting the filename from a file path in C
- Using zsh to remove the file path and extension
- Other simple code samples for extracting file names from file paths in Linux
- Conclusion
- How to get file name from path in Linux?
- How do I get the filename from the file path?
- How to get file name from path in shell?
- How to get only filename from file path in Unix?
If you have ever worked with file paths in Linux, Unix, or macOS, you know how crucial it is to be able to extract file names from a file path. Whether you are working on a simple shell script or a large-scale project, being able to extract the filename from a file path can save you a lot of time and effort.
In this article, we will discuss various methods and techniques to extract the filename from a file path in Linux, Unix, and macOS using bash shell scripting. We will also provide tips and best practices for handling file paths .
Using basename to retrieve the filename
One of the simplest and most commonly used methods to retrieve the filename from a file path is by using the basename command. The basename command takes a file path as an argument and returns only the filename.
The syntax for using basename is as follows:
For example, to retrieve the filename from the path /usr/local/bin/bash , you can use the following command:
You can also use the -o option with basename to specify multiple expressions in logical OR mode. If you are using the -o option, you need to enclose the expressions in parentheses.
Using dirname to retrieve the directory path
If you want to retrieve the directory path from a file path, you can use the dirname command. The dirname command takes a file path as an argument and returns only the directory path.
The syntax for using dirname is as follows:
For example, to retrieve the directory path from the path /usr/local/bin/bash , you can use the following command:
This will return /usr/local/bin .
Using readlink to retrieve the directory path from a relative path or filename
If you have a relative path or filename and want to retrieve the directory path, you can use the readlink command. The readlink command takes a file path as an argument and returns the directory path.
The syntax for using readlink is as follows:
For example, to retrieve the directory path from the relative path ../file.txt , you can use the following command:
This will return the full path of the file.
Maximum length for file names and path names
When working with file paths , it is important to consider the maximum length for file names and path names. In Linux, the maximum length for a file name is 255 bytes, and the maximum combined length of the file name and path name is 4096 bytes. The maximum length for a path and file name combined for archive or retrieve operations on Linux is still 1024 bytes.
It is also important to note that the Linux kernel cannot correctly parse a file path with a “/” character.
Using realpath to show the full file path
If you want to retrieve the full file path, you can use the realpath command. The realpath command takes a file path as an argument and returns the full path of the file.
The syntax for using realpath is as follows:
For example, to retrieve the full path of the file ../file.txt , you can use the following command:
This will return the full path of the file.
Extracting the filename from a file path in C
If you are working with C#, you can use the GetFileName() method of the Path class to extract the filename from a file path.
The syntax for using GetFileName() is as follows:
string fileName = Path.GetFileName('/path/to/file');
Using zsh to remove the file path and extension
If you are using zsh, you can use the $file:t:r command to remove the file path and extension and leave only the file name.
The syntax for using $file:t:r is as follows:
Other simple code samples for extracting file names from file paths in Linux
In Shell , for example, file name from path linux code example
Conclusion
In this article, we discussed various methods and techniques to extract the filename from a file path in Linux, Unix, and macOS using bash shell scripting. We also provided tips and best practices for handling file paths .
By using the methods and techniques discussed in this article, you can easily extract the filename from a file path and simplify your file path handling in scripts. Remember to consider the maximum length for file names and path names, and follow best practices for file naming conventions to avoid issues with file paths.
Absolute Path vs Relative Path in Linux
When handling files and directories on Linux, you must know how to work with paths for quick navigation and access to files. A path defines the route to access a given folder or file. Linux’s directory structure resembles the roots of a tree, where everything starts from the root down to the branches until you reach the target file or folder.
Today’s guide focuses on understanding paths on Linux. We will dig in on absolute and relative paths in Linux to understand their differences and how to use each when locating the path to a file or folder.
Understanding Absolute and Relative Path in Linux
The Linux file system is all about files and directories— how you access these files or folders is using paths. Every path starts from the root directory, represented as a slash (/). From the root directory, you can have the separators. You can use the relative or absolute path.
1. Absolute Path
You use the absolute path when you specify the path to a file or folder, starting with the root directory. This path begins with the root (/), followed by the separators to the other directories in the middle before you reach the target file.
To understand the absolute path in Linux, let us have an example of using the pwd command.
You will note that our current path, in this case, is /home/linuxhint. The first slash (/) represents the root directory. All users created in a Linux system get stored in the home directory. In this case, our user is named linuxhint. So, this absolute path is the home directory of linuxhint and it contains all the other child directories.
Suppose we wanted to list the contents of the Downloads directory for the named user, you can do so using its absolute path, as shown.
The absolute path in Linux allows access to files and folders in other locations without navigating into their parent directory.
2. Relative Path
Unlike the absolute path that starts from the root directory, the relative paths start from the present working directory. Thus, the relative path changes depending on your current directory.
For instance, referencing the earlier example of accessing the contents of the /Downloads, our command would change as follows, if we wanted to use the absolute path:
The target folder is in the current directory, meaning we can directly reference it without needing to add the root directory.
The relative path allows using single dot (.) and double dot (..) to specify paths up or down the current directory. These dots are not visible unless you use them to access the target file.
The single dot specifies the current directory, while the double dot represents its parent directory.
We are in the /home/linuxhint/Downloads/names path.
Suppose we wanted to list the contents of the Downloads/ without typing its absolute path, we could use the double dots for the relative path to access the parent directory with the command below.
Using the relative path in such a case is more convenient, unlike the absolute path, which would require typing the whole path as shown.
Similarly, if we wanted to move higher in the parent directory, we could add more double dots with a separator. In the below example, we have accessed the home directory.
If we wanted to access a directory in the child directory of our current directory, we could specify its relative path using single quotes.
Absolute Path vs Relative Path
The absolute path specifies the path to a folder or file starting from the root directory followed by the user’s home directory. However, the relative path specifies the path to a file or folder starting with the current directory.
Use the relative path when accessing a child or parent directory starting from the current directory. Use the absolute path to access a file or folder down the file system or from another location without switching the current directory.
Conclusion
This guide has discussed what the absolute and relative paths mean in Linux. We have seen the examples in each case and how to choose either option depending on the location of your target file or folder. With this post, you will quickly understand how to work with paths in Linux.
About the author
Denis Kariuki
Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.
Get Absolute File Path in Linux
Here are different ways to get the absolute file paths in Linux command line.
You can get the full path of a directory with the pwd command:
But how do you get the absolute path of a file in Linux?
There are several ways to print the full path of files:
- readlink
- realpath
- find
- combining ls and pwd
Let me show you these commands one by one. But before that, I suggest brushing up on the basics of the absolute and relative path concept first.
Use readlink to get file path
The purpose of the readlink command is to resolve symbolic links. You can use it to display the full path of a file like this:
[email protected]:~$ readlink -f sample.txt /home/abhishek/sample.txt [email protected]:
Use realpath to get full file path
The realpath command is used for resolving the absolute file names. Among other uses, it can show the full path to a file.
Take a look at this example:
[email protected]:~$ realpath sample.txt /home/abhishek/sample.txt [email protected]:~$
If you use it with a symbolic link, it will show the real path of the original file. You can force it to not follow the symbolic link:
Here’s an example where it showed the full path to the source file by default and then I forced it to show the symbolic link, not its original file.
[email protected]:~$ realpath linking-park /home/abhishek/Documents/ubuntu-commands.md [email protected]:~$ realpath -s linking-park
Use the find command to get the absolute file path
Here’s the thing with the find command. Everything is relative to the directory you give it for search location.
If you give it . it will show the relative path. If you give it the absolute path of the directory, you’ll get the absolute path of the files you are searching for.
Use the command substitution with the find command like this:
You can run it to find the full path of a single file:
[email protected]:~$ find $(pwd) -name sample.txt /home/abhishek/sample.txt
Or, you can use it with a bunch of files matching a certain pattern:
[email protected]:~/Documents/eBooks$ find $(pwd) -name "*.pdf" /home/abhishek/Documents/eBooks/think-like-a-programmer.pdf /home/abhishek/Documents/eBooks/linux-guide.pdf /home/abhishek/Documents/eBooks/absolute-open-bsd.pdf /home/abhishek/Documents/eBooks/theory-of-fun-for-game-design.pdf /home/abhishek/Documents/eBooks/Ubuntu 1804 english.pdf /home/abhishek/Documents/eBooks/computer_science_distilled_v1.4.pdf /home/abhishek/Documents/eBooks/the-art-of-debugging-with-gdb-and-eclipse.pdf
Print full path with the ls command
Now this one is a bit tricky and messy.
You can use the environment variable PWD with ls command like this to display the files and directories with their absolute path:
You get an output like this:
[email protected]:~/test$ ls -ld $PWD/* -r--rw-r-- 1 abhishek abhishek 0 Jul 27 16:57 /home/abhishek/test/file2.txt drwxrwxr-x 2 abhishek abhishek 4096 Aug 22 16:58 /home/abhishek/test/new
However, to print the full path of a file with the ls command, you’ll have to use it like this:
Not the cleanest solution but it works.
[email protected]:~$ ls -l $PWD/sample.txt -rw-r--r-- 1 abhishek abhishek 12813 Sep 7 11:50 /home/abhishek/sample.txt [email protected]:~$
Conclusion
I showed four different ways to get the full file path in Linux. The find and ls commands are common while realpath and readlink are hardly known to many Linux users. It’s always good to learn new things, isn’t it?