Skip to content

Latest commit

 

History

History
198 lines (127 loc) · 2.99 KB

File metadata and controls

198 lines (127 loc) · 2.99 KB

Nice 👌 this screenshot is showing three very useful Linux commands for working with file paths. Let’s break them down with examples:


1. basename

  • Removes the directory part of a path and only shows the file name.
basename /home/user/docs/report.txt

Output:

report.txt

You can also remove the file extension:

basename /home/user/docs/report.txt .txt

Output:

report

2. dirname

  • Removes the filename and gives the directory path.
dirname /home/user/docs/report.txt

Output:

/home/user/docs

3. realpath

  • Shows the absolute full path of a file.
  • Useful if you are in a relative directory.
cd /home/user/docs
realpath report.txt

Output:

/home/user/docs/report.txt

Yes 👍 there are many more path and filename related commands/utilities in Linux (apart from basename, dirname, realpath). These are super useful when writing shell scripts. Let me give you a list with examples:


1. readlink

  • Prints the absolute path of a symlink or file.
readlink -f myfile

Output:

/home/user/project/myfile

2. pwd

  • Shows the current working directory.
pwd

Output:

/home/user/docs

3. ls -d

  • Can be used to print directory path only.
ls -d /etc/*

4. stat

  • Shows detailed information about a file (size, permissions, last modified, etc).
stat report.txt

5. file

  • Identifies the file type (text, binary, image, etc).
file script.sh

Output:

script.sh: Bourne-Again shell script, ASCII text executable

6. find

  • Locate files or directories by name/path/size.
find /home/user -name "*.txt"

7. realpath --relative-to

  • Get relative path instead of full absolute path.
realpath --relative-to=/home/user /home/user/docs/file.txt

Output:

docs/file.txt

8. Parameter expansion in Bash (built-in, no external command)

You don’t always need external commands like basename. Bash itself can handle this:

#!/bin/bash
FILE="/etc/.nginx/nginx.conf"
echo "${FILE##*/}"   # basename -> nginx.conf
echo "${FILE#*/}"   # etc/.nginx/nginx.conf
echo "${FILE%/*}"    # dirname -> /etc/.nginx
echo "${FILE%%/*}"    # 
echo "${FILE##*.}"   # extension-> conf
echo "${FILE#*.}"   # nginx/nginx.conf
echo "${FILE%.*}"    # filename without extension -> /etc/.nginx/nginx
echo "${FILE%%.*}"    # /etc/

👉 So apart from basename, dirname, and realpath, the important related ones are:

  • readlink
  • pwd
  • stat
  • file
  • find
  • Bash parameter expansion (##, % operators)

Do you want me to prepare a cheatsheet table with all these commands + examples (like your screenshot style)?

👉 Do you want me to also show how you can combine these commands in a script (for example, to check a file’s path and name separately)?