Introduction
In daily work with the Linux command line you often have to manipulate file routes to get only the name of the file or directory that contains it. The commandsbasenameanddirnameare part of the basic set of tools that allow these operations to be carried out quickly and without the need for complex regular expressions. This article explains what they do, how they are used and shows practical examples that you can apply to your scripts and the terminal.
What is basename
The commandbasenameRemove the last component from a route, i.e. the name of the file or the last directory in the chain. Its simplest syntax isbasename ruta [sufijo]. If a suffix is provided,basenameit removes it from the result whenever it matches the end of the name. This feature is useful when you want to remove a file extension like.txtor.logwithout using additional tools.
What is dirname
For his part,dirnamereturns everything that precedes the last component of the route, i.e. the directory containing the specified file or subdirectory. If the route does not contain any bars,dirnamereturns a point (.) which represents the current directory. This tool is ideal for building relative routes or to change to the work directory before running another operation.
Syntax and options
Both commands share a very similar syntax. The general form is:
basename cadena [sufijo]dirname cadena
There are no long options like--helpin some distributions, but can be used--versionto view the version and--helpto show a brief help. In practice, the most important thing is to understand how the input chain is treated.
Some points to take into account:
- The final bars are ignored;
basename /home/user/returnuser. - If the chain consists of bars only,
basename ////returns empty anddirnamereturn/. - Special characters such as spaces or comandines should be protected with quotes or escapes.
Practical examples
Let's imagine the route./var/log/nginx/error.log.
basename /var/log/nginx/error.logreturnerror.log.basename /var/log/nginx/error.log .logremoves the suffix and sampleerror.dirname /var/log/nginx/error.logreturn/var/log/nginx.
Another common case is to work on relative routes:
basename src/main.c→main.cdirname src/main.c→src
When you need to remove several extensions, you can chain the use ofbasename:
basename archivo.tar.gz .gz→archivo.tar- Then apply again
basename archivo.tar .tar→archivo
Combine both
In many scripts it is necessary to get both the name of the file and its directory to perform operations like copying, renaming or backup. A common pattern is:
NAME =$(basenam'$ROTA ')
DIR =$(dirname '$ROTA ')
With these two variables you can, for example, rebuild a route in another location:
cp '$RITA '' / backup /$DIR /$NAME '
Or change the extension while maintaining the same directory:
NEWNAME =$(basenam'$Ruta '.log)
mv '$RITA '' '$DIR /$NEWNAME.txt '
Cases of use in scripts
System management scripts often process file lists generated byfindorls. For example, to rename all files.bakeliminating the extension:
for f in $(find. -name '*.bak ');
base =$(basenam'$f '.bak)
mv '$f '$base '
done
To create a destination directory that reflects the origin structure:
src = / home / user / projects / project1 / src / main. c
dest _ dir = / tmp / backup /$(dirname '$src ')
mkdir -p $dest _ dir
cp $src $dest _ dir /
Tips and tricks
- It always locks the variable in quotes to prevent special spaces or characters from breaking the command.
- If you just need the name without extension and you don't know what it is, you can use
basename $f ${f##*.}in Bash, butbasenamewith a known suffix is more legible. - In environments where
basenameis not available (e.g. very minimal embedded systems), you can achieve the same result with Bash expansion parameters:${ruta##*/}equivalent tobasenameand${ruta%/*}equivalent todirname. - Verify that the variable is not empty before using it; a poorly formed route can produce unexpected results.
Conclusion
The commandsbasenameanddirnameare essential tools for any Linux user or administrator working with file routes. Its simplicity, combined with the ability to remove suffix and obtain directories, make them ideal for automation scripts, log processing and system maintenance tasks. Dominating its use will allow you to write code cleaner, safer and more efficient in the command line.


