How do I display filetype with ls?
I think the best way to display a file type is using this command:
file <filename>
If you want to list the types of all files in a directory, just use:
file *
For more details about the arguments, see:
man file
file
is definitely the right choice to get the file type information you want. To combine its output with that of ls
I suggest to use find
:
find -maxdepth 1 -type f -ls -exec file -b {} \;
This finds every file in the current directory and prints the output of ls -dils
as well as the output of file -b
for it, each on an own line. Example output:
2757145 4 -rw-rw-r-- 1 dessert dessert 914 Apr 26 14:02 ./some.html
HTML document, ASCII text
2757135 4 -rw-rw-r-- 1 dessert dessert 201 Apr 13 15:26 ./a_text_file
UTF-8 Unicode text, with CRLF, LF line terminators
But, as you don't want a filetype line but rather a filetype column, here's a way to get rid of the newline character between the lines:
find -maxdepth 1 -type f -exec sh -c "ls -l {} | tr '\n' '\t'; file -b {}" \;
Sample output:
-rw-rw-r-- 1 dessert dessert 914 Apr 26 14:02 ./some.html HTML document, ASCII text
-rw-rw-r-- 1 dessert dessert 201 Apr 13 15:26 ./a_text_file UTF-8 Unicode text, with CRLF, LF line terminators
That new column is quite long, so let's cut everything from the first comma:
find -maxdepth 1 -type f -exec sh -c "ls -l {} | tr '\n' '\t'; file -b {} | cut -d, -f1" \;
The output of that looks like this:
-rw-rw-r-- 1 dessert dessert 914 Apr 26 14:02 ./some.html HTML document
-rw-rw-r-- 1 dessert dessert 201 Apr 13 15:26 ./a_text_file UTF-8 Unicode text
This is not quite handy, so how about an alias
? With the following line in your ~/.bash_aliases
file you just need to run lsf
to get the above output for the current directory.
alias lsf='find -maxdepth 1 -type f -exec sh -c "ls -l {} | tr '"'\n'"' '"'\t'"'; file -b {} | cut -d, -f1" \;'