The standard ls
command in Linux/Unix systems shows only filenames by default. This behavior is by design for terminal space efficiency, but becomes problematic when:
- Debugging scripts that require absolute paths
- Documenting file locations in technical documentation
- Processing outputs in pipelines where context matters
While ls doesn't have a direct flag for full paths, these methods work:
# Method 1: Prefix with directory
ls -d "$PWD"/*
# Method 2: For specific files
ls -d "$PWD"/filename.ext
When you need more control:
# Recursive listing with full paths
find "$PWD" -type f -exec ls -l {} \;
# Formatted output with paths
ls | xargs -I{} echo "$PWD/{}"
Processing log files in a backup script:
#!/bin/bash
for logfile in $(ls -d /var/log/app/*.log); do
gzip "$logfile"
aws s3 cp "$logfile.gz" s3://backup-bucket/
done
When ls isn't enough:
find . -printf '%p\n'
- More flexible path formattingrealpath *.txt
- Resolves relative to absolute pathsreadlink -f filename
- Canonical path resolution
The standard ls
command in Linux/Unix systems displays only filenames by default. When you run:
ls /var/log
You'll see output like:
syslog auth.log kern.log
This is often sufficient for basic navigation, but sometimes you need the complete absolute path.
While not exactly using ls
, this gives similar output with full paths:
find /var/log -maxdepth 1 -type f -printf '%p\n'
This outputs:
/var/log/syslog
/var/log/auth.log
/var/log/kern.log
For a specific file, you can use:
readlink -f filename
Or for multiple files with ls:
ls /var/log | xargs -I {} readlink -f /var/log/{}
For the current directory:
ls -d "$PWD"/*
For any directory:
ls -d /path/to/dir/*
Add this to your ~/.bashrc
:
function lsf() {
ls -1 "$@" | while read file; do
echo "$(pwd)/$file"
done
}
Then use:
lsf /var/log
If you have GNU coreutils version 8.25 or later:
ls --format=verbose /var/log
Combine with awk for path extraction:
ls --format=verbose /var/log | awk '{print $NF}'
For recursive directory listing with full paths:
find $(pwd) -type f
Or using ls with find:
find /path/to/dir -type f -exec ls -l {} \;