Ekstrahirajte ime datoteke in pripono v Bashu

Predstavitev

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. način: Uporaba osnovno ime Ukaz

O 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.

Prav tako lahko uporabite dirname command to extract the directory path separately:

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

Prednosti:
  • Preprost za uporabo
  • Handles filenames with spaces correctly
Slabosti:
  • Only extracts the filename and cannot extract the extension separately without additional processing

Method 2: Using Parameter Expansion

Bash ponuja funkcijo, imenovano razširitev parametrov, 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

Prednosti:
  • Prilagodljiva
  • Can extract both the filename and extension separately,
  • Handles filenames with spaces correctly
Slabosti:
  • Requires a variable to store the file path

3. način: Uporaba vau ukaz

O 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

Oglejte si naš praktični, praktični vodnik za učenje Gita z najboljšimi praksami, standardi, sprejetimi v panogi, in priloženo goljufijo. Nehajte Googlati ukaze Git in pravzaprav naučiti it!

Vous uporaba pouvez aussi awk to extract the extension separately:

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

Prednosti:
  • Zmogljiv
  • Can extract both the filename and extension separately
  • Handles filenames with spaces correctly
Slabosti:
  • Syntax may be unfamiliar to some users
  • Requires piping the file path through awk

zaključek

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.

Časovni žig:

Več od Stackabuse