A fájlnév és a kiterjesztés kibontása a Bash-ban

Bevezetés

There are many reasons why you might want to extract the filename and extension of a file in Bash:

  • To manipulate the file name or extension – You may want to extract the filename or extension in order to modify it, such as adding a prefix or suffix to the file name, or changing the file extension.

  • To create a file with a unique name – You may want to extract the filename and extension in order to create a new file with a unique name, such as by adding a timestamp or a random number to the file name.

  • To use the file name or extension in a script or command – You may want to extract the filename or extension in order to use it as an argument or input for a script or command, such as to pass it to a program or to create a file with the same name as a directory.

  • To extract information from the file name or extension – You may want to extract the filename or extension in order to extract information from it, such as the date or the file type.

In this article, we’ll take a look at the three most common ways you can extract filename and file extension in Bash. We’ll take a look at each of them and give you the pros and cons, so you can make an educated decision on what approach suits you the best.

1. módszer: A alapnév parancs

A basename command can be used to extract the filename and extension from a file path:

filename=$(basename /path/to/file.txt)
echo $filename

Although this method is quite simple and easy to use, unfortunately, there is no way we can extract only the file name (with no extension) without any postprocessing.

Ön is használja a dirname command to extract the directory path separately:

directory=$(dirname /path/to/file.txt)
echo $directory

Előnyök:
  • Egyszerű használat
  • Handles filenames with spaces correctly
Hátrányok:
  • Only extracts the filename and cannot extract the extension separately without additional processing

Method 2: Using Parameter Expansion

Bash provides a feature called paraméterbővítés, which allows you to extract parts of a variable using a special syntax. For example, you can use the following syntax to extract the filename and extension from a file path stored in a variable:

filepath="/path/to/file.txt"
filename=${filepath##*/}
echo $filename

You can also use parameter expansion to extract the extension separately:

extension=${filename##*.}
echo $extension

Előnyök:
  • Rugalmas
  • Can extract both the filename and extension separately,
  • Handles filenames with spaces correctly
Hátrányok:
  • Requires a variable to store the file path

3. módszer: A awk parancs

A awk command is a powerful text processing tool that can be used to extract parts of a string. For example, you can use the following syntax to extract the filename and extension from a file path:

filepath="/path/to/file.txt"
filename=$(echo $filepath | awk -F/ '{print $NF}')
echo $filename

Tekintse meg gyakorlatias, gyakorlati útmutatónkat a Git tanulásához, amely tartalmazza a bevált gyakorlatokat, az iparág által elfogadott szabványokat és a mellékelt csalólapot. Hagyd abba a guglizást a Git parancsokkal, és valójában tanulni meg!

Ön is használja awk to extract the extension separately:

extension=$(echo $filename | awk -F. '{print $NF}')
echo $extension

Előnyök:
  • Erős
  • Can extract both the filename and extension separately
  • Handles filenames with spaces correctly
Hátrányok:
  • Syntax may be unfamiliar to some users
  • Requires piping the file path through awk

Következtetés

Overall, extracting the filename and extension of a file in Bash can be a useful technique for working with files and performing various tasks in the Bash shell.

Each of mentioned methods has its own advantages and disadvantages, and the best choice will depend on your specific needs and preferences. It is often useful to be familiar with multiple approaches so that you can choose the one that is most suitable for your situation.

Időbélyeg:

Még több Stackabus