I need to gather a list of files and put this in a textfile. However, the files should not include their extension.
The command I have now without removing file extensions:
ls -1 /a/dir/*/dir/* > textfile
All selected files will have a an extension of .[a-z]{3}
(the reason I need this is because I need a list of unique base names, and unique
will solve that after this step)
Something like that would list the file in the current directory removing everything after the first dot.
find . -maxdepth 1 -type f | xargs -iZ basename Z | cut -d"." -f1 > /tmp/resultfile
It would eventually have an issue with hidden files resulting in a blank line in the result file. Basename is here only to get rid of the "./" begining the filenames in the find result.
I always like to include a bash only solution:
and with an optional if statement to limit the file to your requested extension:
In this similar serverfault question I explain the "${file%.*}" parameter expansion.
How about:
Should do what you want.
It's much more simple than one thinks:
So in this manner:
You can do whatever you want between do and done, echo is just an example.
man bash (Parameter Expansion):
A more generic answer, finds extensions of any length:
I then use the following command to get a list of extensions in a dir:
You can also use cut like this :
It will only work if you don't have any file or directories with a "." in the name
You have given an explicit definition for extensions,
But, the tags
linux
andbash-scripting
suggest a unix environment.Unless you are doing this with Cygwin on Windows.
These points may not matter in that case...
md5
)script.sh
)