باش میں فائل کا نام اور ایکسٹینشن نکالیں۔

تعارف

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: کا استعمال کرتے ہوئے بیس نام کمان

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

آپ کو بھی استعمال کر سکتے ہیں dirname command to extract the directory path separately:

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

پیشہ:
  • استعمال کرنے کے لئے آسان
  • Handles filenames with spaces correctly
Cons:
  • Only extracts the filename and cannot extract the extension separately without additional processing

Method 2: Using Parameter Expansion

Bash نامی ایک خصوصیت فراہم کرتا ہے۔ پیرامیٹر کی توسیع, 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

پیشہ:
  • لچکدار
  • Can extract both the filename and extension separately,
  • Handles filenames with spaces correctly
Cons:
  • Requires a variable to store the file path

طریقہ 3: کا استعمال کرتے ہوئے عجیب کمانڈ

۔ 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

بہترین طرز عمل، صنعت کے لیے منظور شدہ معیارات، اور چیٹ شیٹ کے ساتھ Git سیکھنے کے لیے ہمارے ہینڈ آن، عملی گائیڈ کو دیکھیں۔ گوگلنگ گٹ کمانڈز کو روکیں اور اصل میں سیکھ یہ!

آپ بھی استعمال کر سکتے ہیں awk to extract the extension separately:

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

پیشہ:
  • طاقتور
  • Can extract both the filename and extension separately
  • Handles filenames with spaces correctly
Cons:
  • Syntax may be unfamiliar to some users
  • Requires piping the file path through awk

نتیجہ

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.

ٹائم اسٹیمپ:

سے زیادہ Stackabuse