How to list all file types recursively?
I'd like to print a list of all file types in the current directory and below. I got this far with a little help from google. I post it since there was not an instant result in my search. Hopefully it will save someone time down the road. Or some better bashers can share a better way:)
Code:
user@system $ find ./ -type f | awk -F . '{print $NF}' | sort --unique Code:
user@system $ touch install/somefile Code:
user@system $ find ./ -type f | awk -F . '{print $NF}' | sort --unique | awk -F / '{print $NF}' ICYWTK: I was looking for the types so I could set the permissions recursively too. Example: Code:
user@system $ find ./ -type f -name '*.png' -exec chmod 644 {} \; |
So you have already solved your problem? Maybe you should post solutions as well in case others would like to know how.
|
Quote:
Code:
find . -type f -printf '%f\n' # prints filenames with leading directories removed Quote:
|
You're pretty close.
You can use the "basename" command to remove the path from the files. It only runs on a single file name at a time, but you can get around this using the "xargs" command with "-l". To make this run smoothly, we need to use null-terminated file names, since otherwise xargs will treat spaces in file names as separators. The following will give you only the file names under a given directory: $ find . -type f -print0 | xargs -0l basename The awk command actually has a way to filter based on a regular expression. So, rather than saying: awk -F . '{print $NF}' All you have to do is: awk -F . '/\./{print $NF}' That looks for a '.' character, which you have to escape with a backslash, otherwise '.' matches any character. So, putting all this together: $ find . -type f -print0 | xargs -0l basename | awk -F . '/\./{print $NF}' | sort --unique What could be simpler? ;) |
Quote:
I timed the original discovery against this idea. Reg-ex version: Code:
user@system $ time find . -type f -print0 | xargs -0l basename | awk -F . '/\./{print $NF}' | sort --unique Original: Code:
user@system $ time find ./ -type f | awk -F . '{print $NF}' | sort --unique | awk -F / '{print $NF}' For comparison I also put the -printf option in the mix. Code:
user@system $ time find ./ -type f -printf '%f\n' | awk -F . '{print $NF}' | sort --unique It looks like the winner! However, the same -printf should work in the reg-ex version too, which can eliminate the pipe to basename. Let's see how it does. Code:
user@system $ time find . -type f -printf '%f\n' | awk -F . '/\./{print $NF}' | sort --unique example: Code:
user@system $ touch README Code:
user@system $ time find ./ -type f -printf '%f\n' | awk -F . '{print $NF}' | sort --unique |
Quote:
Quote:
I had another idea which might make things faster: Code:
find . -type f -printf '%f\n' | awk -F. '!($NF in exts){print $NF; exts[$NF]=1}' Quote:
|
All times are GMT -5. The time now is 05:31 AM. |