Linux basename Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the basename command in Linux to extract the filename from a full file path. The lab covers understanding the basename command, using it to extract filenames and directory names, and combining it with other Linux commands for more complex file and directory operations. The basename command is a useful tool for working with files and directories in the Linux command line.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/mv("`File Moving/Renaming`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/cd -.-> lab-422570{{"`Linux basename Command with Practical Examples`"}} linux/ls -.-> lab-422570{{"`Linux basename Command with Practical Examples`"}} linux/mv -.-> lab-422570{{"`Linux basename Command with Practical Examples`"}} linux/touch -.-> lab-422570{{"`Linux basename Command with Practical Examples`"}} end

Understand the basename Command

In this step, you will learn about the basename command in Linux. The basename command is used to extract the filename from a full file path.

First, let's create a sample file path:

cd ~/project
mkdir -p files/subdir
touch files/subdir/example.txt

Now, let's use the basename command to extract the filename from the full path:

basename files/subdir/example.txt

Example output:

example.txt

As you can see, the basename command extracts the filename example.txt from the full path files/subdir/example.txt.

You can also use basename to extract the directory name from a full path:

basename files/subdir

Example output:

subdir

In this case, the basename command extracts the directory name subdir from the full path files/subdir.

The basename command is a useful tool for working with files and directories in the Linux command line. It can be combined with other commands to perform more complex file and directory operations.

Use basename to Extract Filename from Full Path

In this step, you will learn how to use the basename command to extract the filename from a full file path.

Let's start by creating some sample files and directories:

cd ~/project
mkdir -p files/docs files/images
touch files/docs/report.txt files/images/photo.jpg

Now, let's use the basename command to extract the filenames from the full paths:

basename files/docs/report.txt
basename files/images/photo.jpg

Example output:

report.txt
photo.jpg

As you can see, the basename command extracts the filenames report.txt and photo.jpg from the respective full paths.

You can also use basename to extract the directory name from a full path:

basename files/docs
basename files/images

Example output:

docs
images

In this case, the basename command extracts the directory names docs and images from the full paths.

The basename command is a powerful tool for working with files and directories in the Linux command line. It can be combined with other commands to perform more complex operations.

Combine basename with Other Linux Commands

In this final step, you will learn how to combine the basename command with other Linux commands to perform more complex file and directory operations.

Let's start by creating some sample files and directories:

cd ~/project
mkdir -p files/docs files/images
touch files/docs/report.txt files/images/photo.jpg

One common use case for basename is to extract the filename from a full path and then perform an operation on that file. For example, let's list all the files in the files/docs directory:

ls files/docs

Example output:

report.txt

Now, let's combine basename with ls to list the filenames only:

ls files/docs | xargs basename

Example output:

report.txt

In this example, we use xargs to pass the output of ls files/docs to the basename command, which then extracts the filenames.

Another common use case is to combine basename with mv or cp to rename or copy files. For example, let's rename the report.txt file to document.txt:

mv files/docs/report.txt files/docs/$(basename files/docs/report.txt .txt).new.txt

Example output:

In this example, we use basename to extract the filename report.txt from the full path, and then append .new.txt to the filename to create the new filename document.new.txt.

The basename command can be combined with many other Linux commands to perform a wide range of file and directory operations. Experiment with different combinations to see how you can use basename to streamline your workflow.

Summary

In this lab, you learned about the basename command in Linux, which is used to extract the filename from a full file path. You started by creating sample files and directories, and then used the basename command to extract the filenames and directory names from the full paths. The basename command is a powerful tool for working with files and directories in the Linux command line, and can be combined with other commands to perform more complex file and directory operations.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like