Skip to content
Advertisement

Excluding a file extension through wildcards

I have a directory which files with multiple extensions such as *.asc, .txt..csv etc. I am using this path in a script where I can only change the file names, which is passed through variable.

/home/myuser/mydirectory/{fileNameVariable}

I can pick all the .csvs by passing *.csv as the variable value or *.txt for respective files. However what I want to do is pick all files (.csv, .txt etc.) except *.asc.

Is there a way through using wildcard I can exclude only one file extension and pick the rest?

Advertisement

Answer

You can use brace expansion to select only the names and extensions you want and exclude any of the extensions not named. For example to locate all .csv and .txt files within a directory, you can use:

$ ls -al *.{csv,txt}    ## note: no space after the separating comma

If you had many test1..., test2..., etc. files and you only wanted to select the test2*.csv and test2*.txt files you could narrow the initial glob to:

$ ls -al test2*.{csv,txt}

Of course, you can use your path and variable name in place of test2 above.

Advertisement