I'm suppose to write a command that would list all files that end in .tx with an optional t at the end.
So far all I got is listing the files that end in .tx which is:
ls *.tx
Now my problem is how do I add the optional t at the end.
In bash with shopt -s extglob you can do this:
ls -d *.tx?(t)
In bash with shopt -s nullglob you can do this:
ls -d *.txt *.tx
But this will show the directory content if no such file exists.
If ls is not required:
find . -type f '(' -name '*.txt' -o -name '*.tx' ')'
This would show files in subdirectories, too. With GNU find this can be avoided with find . -maxdepth 1.
-d option. As written now, if you have a directory called dir.tx, then either of those commands will list the contents of dir.tx in addition to listing the text files. -d will prevent that. (2) -d will help the second command in another way. Currently, if it is executed in a directory where there are no text (.txt or .tx) files, it will list all the files in the current directory. With -d, it will just say .. … (Cont’d)
find command can be shortened to find . -type f -regex '.*\.txt?' (assuming GNU find).
.txand.txtor really want to know if an "optional" character can be attached?ls *.tx *.txt?