howtouselinux

4 effective Ways to Get file path in Linux

Table of Contents

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 4 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, readlink command or python os module.

  • The pwd command is used to find the path of the current working directory. It will give you the full path from the root directory to your current location.
  • The find command can be used to find files and directories in a directory hierarchy. It can also be used to find the location of a specific file.
  • The readlink command is used to display the location of a file.
  • The os.path.abspath function in Python will print the absolute path to a file or folder.

 

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

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.

Using Cloud Linux server to practice Linux commands

Cloud Linux server is a great way to experiment with different Linux commands and learn more about the operating system. Even if you run some harmful commands by mistake, you can create a new server immediately.

Check with the online support for any Linux questions. This can save you a lot of time in learning Linux.

Get file path with readlink command in Linux

The second method is to use the readlink command. This command will print the path of the file or folder that you specify. To use this command, simply type “readlink -f ” into your terminal then, type in the name of the file or folder that you want to find the path for and press enter. The output will be the file path of the specified file or folder.

# readlink -f howtouselinux
/root/howtouselinux

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.

To use this command, type “find” into your terminal followed by the name of the file or folder that you want to find the path for. Then, press enter. The output will be a list of all files and folders that match your search criteria, including the file path for each one.

# find /root -type f -name “howtouseilnux”
/root/howtouseilnux

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.

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.