Skip to Content

3 effective Ways to Get file path in Linux

In Linux, every file and folder has a path that points to it. This path is necessary for applications and scripts to find and access files.

If you need to find the path to a file or folder, there are several ways that you can do so. In this blog post, we will discuss 3 different methods for finding the file path in Linux.

To find the file path in Linux, you can use the following commands. 

  • pwd
  • readlink -f file
  • find / -type f -name “file”
  • import os;os.path.abspath(“file”)
  • find / -type f -name *.mp3 -size +10M
  • find . -type f -name “*.txt” 
  • find . -type f -not -name “*.html” 

 

Methods to get file path in Linux

To get the file path in Linux, you can use pwd, find command or python os module.

  • pwd: The pwd command finds the path of the current working directory. It will give you the full path from the root directory to your current location.
  • find: The find command is used to locate files and directories in a directory hierarchy. It can also be used to find the location of a specific file.
  • os.path.abspath(): This is a function in Python used to print the absolute path to a file or folder. It helps to determine the full path of a file or directory within the file system.

 

 Here are the detailed steps for each command:

  1. Using pwd command:
    • Open a terminal and navigate to the directory containing the file.
    • Run the pwd command to display the current working directory, which represents the file path.
  2. Using find command:
    • Open a terminal and specify the starting directory for the search (e.g., / for the entire file system).
    • Run the find command with the -name option followed by the file name to search for the file.
    • The output will display the file path(s) of the matching file(s) found by the find command.
  3. Using Python code:
    • Open a text editor and create a Python script (e.g., file_path.py).
    • Use the os module to access file-related functionalities.
    • Implement code to retrieve the file path using methods such as os.getcwd() to get the current working directory, or os.path.abspath() to get the absolute path of a specific file.
    • Save the Python script and run it using the Python interpreter to obtain the file path.

 

Get file path with pwd Command in Linux

The most simple way to get file path in Linux is using pwd command. To use this command, type “pwd” into your terminal and press enter. This command will print the current working directory.The output will be the file path.

The pwd command prints the current working directory, telling where you are currently located in the filesystem. This command comes to your rescue when you get lost in the filesystem, and always prints out the absolute path.

# pwd
/root

For example, if you are currently in the “/home/user/Documents” directory, the output of the pwd command will be:

/home/user/Documents

This indicates that the current working directory is “/home/user/Documents“.

The pwd command is a shorthand for “print working directory” and provides a simple way to retrieve the file path of the current directory in Linux.

Understanding file path in Linux

There are two types of paths in Linux: absolute and relative.

  • An absolute path always starts from the root directory, which is represented by a forward slash (/).
  • A relative path, on the other hand, starts from the current directory.

 

You can use the pwd command to print the current working directory.

The root directory is the highest level directory in the file system. In Linux, the root directory is represented by a forward slash (/).

For example, the absolute path to the /home/howtouselinux/file.txt file is /home/howtouselinux/file.txt.

If you are under /home/howtouselinux directory, the relative path to the /home/howtouselinux/file.txt file is ./file.txt. You can use cat ./file.txt or cat file.txt command to get the content of this file.

If you are under /home directory, the relative path to the /home/howtouselinux/file.txt file is howtouselinux/file.txt. You need to use cat howtouselinux/file.txt command to get the content of the file.

 

Check file path with find command in Linux

Another way to find the file path in Linux is to use the find command.

This command allows you to search for files or folders that match a certain criteria.  It can find directories and files by the name, file type, or extension, file size, file permissions, etc.

The find command will search for the specified file and display the file path(s) of the matching file(s) it finds.

To check the file path of a specific file using the find command in Linux, you can follow these steps:

  1. Open a terminal: Launch a terminal or command prompt on your Linux system. This will provide you with a command-line interface.
  2. Use the find command:For example, to find a file named “example.txt” within the entire file system, you would run:
    find / -name "example.txt"
    

    In this example, / represents the root directory, which is the starting point for the search. You can replace / with a specific directory path if you know the approximate location of the file.

  3. View the file path: The file path will be shown relative to the starting directory specified in the command.For example, if the file “example.txt” is located in the /home/user/Documents directory, the output will display the file path:
    /home/user/Documents/example.txt
    

    The leading / represents the root directory, and the subsequent path indicates the location of the file within the file system hierarchy.

By following these steps and using the find command in Linux, you can search for and obtain the file path of a specific file in the file system.

 

Find file path with python os.path.abspath

The fourth method for getting the file path in Linux is to use the os.path.abspath function in Python. This function will print the absolute path to a file or folder.

To use this function, first open up a Python interpreter by typing “python” into your terminal. Then, type in the following code:

import os
os.path.abspath("howtouseilnux")
'/root/howtouseilnux'

The output will be the absolute path to the “howtouseilnux” file.

How to change directory path in Linux

To change directories, you can use the cd command. For example, if you want to change to the /home directory, you would type: cd /home

  • You can use the ~ symbol to represent the home directory in Linux. For example, if you want to change to the /home/username directory, you can just type: cd ~/username
  • You can use the .. symbol to represent the parent directory. For example, if you are in the /home/username directory and you want to change to the /home directory, you can just type: cd ..
  • You can use the . symbol to represent the current directory. For example, if you want to list the contents of the current directory, you can just type: ls .

 

Linux commands for file

  • To list the contents of a directory, you can use the ls command. For example, to list the contents of the /home directory, you would type: ls /home
  • To create a new directory in Linux, you can use the mkdir command. For example, to create a new directory called test in the current working directory, you would type: mkdir test
  • To rename a file or directory, you can use the mv command. For example, to rename the file foo.txt to bar.txt, you would type: mv foo.txt bar.txt
  • To copy a file, you can use the cp command. For example, to copy the file foo.txt to the /home directory, you would type: cp foo.txt /home
  • To move a file, you can use the mv command. For example, to move the file foo.txt to the /home directory, you would type: mv foo.txt /home
  • To delete a file, you can use the rm command. For example, to delete the file foo.txt, you would type: rm foo.txt
  • To view the contents of a file, you can use the cat command. For example, to view the contents of the file foo.txt, you would type: cat foo.txt

 

Tips about Linux command

  • To find out more about a particular command, you can use the man command. For example, to find out more about the ls command, you would type: man ls
  • You can use wildcards to match multiple files. For example, the * wildcard matches any number of characters, so if you want to list all of the files in the current directory that start with the letter a, you would type: ls a*
  • You can use the up and down arrow keys on your keyboard to scroll through previous commands. This can be handy if you want to repeat a command or edit it slightly.
  • You can use the tab key to auto-complete file and directory names. For example, if you type ls /ho and then press the tab key, the system will automatically fill in the rest of the home directory for you.

 

There are many different ways to find the file path in Linux. Which method you use will depend on your needs and preferences. Hopefully, this blog post has helped you learn about some of the different methods that are available. Thanks for reading.