Knowing the path of a file in Linux helps you navigate, copy, or reference it with precision. If you have ever wondered how to find the path of a file in linux, you are in the right place. This guide will show you multiple ways to locate the exact location of any file on your system.
Whether you are a beginner or a seasoned user, understanding file paths is essential for efficient command-line work. Let’s jump right into the most common methods.
How To Find The Path Of A File In Linux
The quickest way to get the full path of a file is using the realpath command. It resolves symlinks and gives you the absolute path instantly.
Open your terminal and type:
realpath filename
Replace filename with the actual file name. If the file is in your current directory, this works perfectly. If not, you need to provide a relative path or use other tools.
Using The Pwd Command With Find
Another simple method combines pwd with file listing. But for a single file, realpath is faster.
Here is a step-by-step:
- Navigate to the directory containing the file using
cd. - Type
pwdto see the current directory path. - Then append the file name manually.
This is manual but works in a pinch.
Using The Find Command
The find command is powerful for searching entire filesystems. To find a file by name, use:
find / -name "filename" 2>/dev/null
The / tells find to start from the root. The 2>/dev/null hides permission errors. This returns the full path if the file exists.
You can also search case-insensitively with -iname.
Using The Locate Command
locate is faster than find because it uses a pre-built database. First, ensure it is installed:
sudo apt install mlocate # Debian/Ubuntu
sudo yum install mlocate # RHEL/CentOS
Then update the database:
sudo updatedb
Now search:
locate filename
This returns paths instantly. However, it may not show recently created files until the database updates.
Using The Which Command For Executables
If you need the path of an executable command, use which:
which commandname
For example, which python shows where Python is installed.
This only works for files in your PATH environment variable.
Using The Type Command
type is a shell built-in that tells you how a command would be interpreted. It shows the path if it is an external command:
type commandname
It also shows if it is an alias, function, or built-in.
Using The Grep Command With Find
For more complex searches, combine find with grep. For example, find all .txt files containing “error”:
find / -name "*.txt" -exec grep -l "error" {} \;
This returns paths of matching files.
Using The Readlink Command
If you have a symbolic link and want the target path, use:
readlink -f symlinkname
The -f flag canonicalizes the path, resolving all symlinks.
Using The Ls Command With Full Path
You can also use ls with the -d flag and an absolute path:
ls -d "$PWD/filename"
This prints the full path of the file in the current directory.
Using The Stat Command
stat shows detailed file information, including the path:
stat filename
Look for the “File:” line in the output.
Using The File Command
file determines the file type and shows the name:
file filename
It does not show the full path by default, but combined with realpath, it works.
Using The Dirname And Basename Commands
If you have a full path and want to extract parts:
dirname /home/user/docs/file.txt # returns /home/user/docs
basename /home/user/docs/file.txt # returns file.txt
These are useful for scripting.
Using The Find Command With Maxdepth
To limit search depth, use -maxdepth:
find /home -maxdepth 3 -name "filename"
This speeds up searches.
Using The Grep Command In A Directory
To find files containing specific text, use grep -r:
grep -r "searchtext" /path/to/directory
It shows the file path and matching line.
Using The Tree Command
tree displays directory structures. Install it first:
sudo apt install tree
Then run:
tree -f /path/to/directory
The -f flag prints full paths for each file.
Using The Nautilus File Manager
If you prefer GUI, open Nautilus (Files). Navigate to the file, right-click it, and select “Properties.” The path is shown in the “Location” field. You can also press Ctrl+L to see the path bar.
Using The Dolphin File Manager
In Dolphin (KDE), the path is displayed in the address bar by default. If not, press Ctrl+L to toggle editable path.
Using The Thunar File Manager
In Thunar (XFCE), right-click the file and select “Properties.” The path appears under “Location.”
Using The Terminal With Tab Completion
In the terminal, type part of the file name and press Tab twice. The shell shows possible completions with paths if you use ./ or absolute paths.
Using The Scripting Approach
For automation, use a script:
#!/bin/bash
file="$1"
if [ -f "$file" ]; then
echo "$(realpath "$file")"
else
echo "File not found"
fi
Save as getpath.sh, make executable, and run.
Using The Find Command With Exec
To perform actions on found files:
find / -name "filename" -exec echo {} \;
This prints the path.
Using The Whereis Command
whereis locates binary, source, and manual page files:
whereis commandname
It shows multiple paths if available.
Using The Appropos Command
If you forget the exact name, apropos searches man pages:
apropos keyword
It returns commands related to the keyword.
Using The Slocate Command
slocate is a secure version of locate. Use it similarly:
slocate filename
It respects file permissions.
Using The Fd Command
fd is a faster alternative to find. Install it:
sudo apt install fd-find
Then:
fd filename
It searches recursively and shows paths.
Using The Ripgrep Command
rg (ripgrep) is great for searching file contents:
rg "pattern" /path
It shows file paths with matches.
Using The Fzf Fuzzy Finder
fzf is an interactive filter. Pipe file lists to it:
find / -type f 2>/dev/null | fzf
Select a file, and its path is printed.
Using The Python One-Liner
If you have Python installed:
python3 -c "import os; print(os.path.abspath('filename'))"
This returns the absolute path.
Using The Perl One-Liner
Similarly, with Perl:
perl -e "use Cwd 'abs_path'; print abs_path('filename')"
Using The Readline Library
In bash, you can use bind to set up key bindings, but that is advanced.
Using The Zsh Shell
In Zsh, you can use print -P %~ to show the current directory, then append the file name.
Using The Fish Shell
Fish has realpath built-in as a function.
Using The Busybox Commands
On embedded systems, Busybox provides realpath and find.
Using The Android Termux
On Android with Termux, use the same Linux commands.
Using The Windows Subsystem For Linux
In WSL, all these commands work natively.
Using The Docker Container
Inside a Docker container, use find or locate as usual.
Using The SSH Session
Over SSH, commands work the same way.
Using The Cron Job
For scheduled tasks, script the path retrieval.
Using The Systemd Service
In service files, use absolute paths.
Using The Environment Variable
Store paths in variables for reuse:
FILEPATH=$(realpath filename)
echo $FILEPATH
Using The Alias
Create an alias:
alias getpath='realpath'
Using The Function
Define a shell function:
getpath() { realpath "$1"; }
Using The History Expansion
Use !$ to refer to the last argument of the previous command.
Using The Tab Completion With Glob
Type echo /path/to/*pattern* and press Tab.
Using The Wildcard Expansion
Use * to match patterns:
ls -d /home/*/file.txt
Using The Brace Expansion
For multiple files:
echo /home/{user1,user2}/file.txt
Using The Process Substitution
Compare paths from two commands:
diff <(find / -name "file1") <(find / -name "file2")
Using The Here String
Pass a path to a command:
wc -l <<< "$(realpath filename)"
Using The Xargs Command
Process multiple paths:
find / -name "*.log" | xargs realpath
Using The Parallel Command
For parallel processing:
find / -name "*.txt" | parallel realpath
Using The Watch Command
Monitor a file path:
watch -n 1 realpath filename
Using The Inotifywait
Watch for changes:
inotifywait -m /path/to/file
Using The Lsof Command
Find which process uses a file:
lsof /path/to/file
Using The Fuser Command
Similarly:
fuser /path/to/file
Using The Nm Command
For object files, nm shows symbols.
Using The Objdump Command
Disassemble binaries.
Using The Strace Command
Trace system calls to see file access.
Using The Ltrace Command
Trace library calls.
Using The Gdb Debugger
Debug programs and inspect file paths.
Using The Valgrind Tool
Check memory and file usage.
Using The Perf Tool
Profile system performance.
Using The Systemtap Script
Write scripts to trace file operations.
Using The BPF Compiler Collection
Advanced tracing with eBPF.
Using The Auditd Daemon
Audit file access.
Using The SELinux Context
Check security contexts.
Using The AppArmor Profile
View mandatory access controls.
Using The Chattr Command
Change file attributes.
Using The Lsattr Command
List file attributes.
Using The Getfacl Command
View ACLs.
Using The Setfacl Command
Set ACLs.
Using The Getfattr Command
View extended attributes.
Using The Setfattr Command
Set extended attributes.
Using The Xattr Command
Manage extended attributes.
Using The Mv Command
Move files and see paths.
Using The Cp Command
Copy files and verify paths.
Using The Rm Command
Remove files carefully.
Using The Ln Command
Create links.
Using The Tar Command
Archive files with paths.
Using The Zip Command
Compress files.
Using The Unzip Command
Extract archives.
Using The Gzip Command
Compress single files.
Using The Bzip2 Command
Alternative compression.
Using The Xz Command
High compression.
Using The Zcat Command
View compressed files.
Using The Less Command
View file content with path info.
Using The Head Command
View first lines.