5

I have a script that, when a user enters a file name in a directory, performs automatic tagging operations on that file (mp3 tagging).

My problem: It gets really tedious typing in the exact file name of every song I want to tag, and I was wondering if it is possible to implement tab autocompletion when I input the name of a file.

The beginning of my script:

Input file name and locate file

echo  "Enter name of file to be tagged"
read -e FileName
FileFindTest=$(find ~/Downloads/"$FileName")
echo "$FileFindTest"
Data643
  • 51

2 Answers2

4

Surprisingly (for me), read does simple file / folder autocomplete with the -e option.

To use it in your example (I've noted my changes)

echo  "Enter name of file to be tagged:"

cd ~/Downloads      ## cd to Downloads folder for autocomplete

read -e FileName

FileFindTest="$(find ~/Downloads/"$FileName")"  ## quoted 

echo "$FileFindTest"

In this case, when asking for input it will autocomplete files/folder in your Downloads folder.


Example, suppose you have the following files in your Downloads folder:

~/Downloads
│
├───Pop/
│   └───PopSong.mp3
├───Song1.mp3
└───Song5.mp3

Then when you will have the following results (pressing Tab when <TAB> is shown)

P<TAB>
Pop/

S<TAB>
Song1.mp3 Song5.mp3

PopSong.mp3<TAB>
# (nothing found here, as it's searching in Downloads/ only not Pop/)

Pop/P<TAB>
PopSong.mp3
kiri
  • 28,986
0

I believe you would benefit from reading on How to create script with auto-complete?

If the cd workaround doesn't cut it for you, you could try this approach an replace the static values with a listing. Or you can go head on into Programmable Completion from Bash Manual.

SbiN
  • 101