I am ignorant as to whether ls is capable of displaying a filetype column.
Doing a quick online search and searching the man did not reveal such a capability. Is it capable of doing this?
Clarification: This is especially helpful if files do not have an extension.
Update
As of 2018-04-28 the answers have been quite interesting however they did not provide what I was looking for. More specifically,
filedoes give the actual filetypes but it doesn't integrate well withls. Specifically, I usels -lhtrpG --group-directories-first --color=alwaysls -Fis anlssolution however it provides symbols not a column of actual filetypes.
For this reason I have not marked any of the answers as the answer I am looking for.
25 Answers
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 5 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 terminatorsBut, 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 terminatorsThat 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 textThis 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" \;' 7 For clarity, I'm going to point out that you can see the file type in a basic sense with ls, using the -F flag (classify) which appends a symbol to the filename depending on its type:
‘-F’
‘--classify’
‘--indicator-style=classify’ Append a character to each file name indicating the file type. Also, for regular files that are executable, append ‘*’. The file type indicators are ‘/’ for directories, ‘@’ for symbolic links, ‘|’ for FIFOs, ‘=’ for sockets, ‘>’ for doors, and nothing for regular files.You can see that information slightly less cryptically displayed in the first letter of the output of ls -l, though. wjandrea's answer describes this in more detail.
But I don't think this is what you mean by file type. The ls command does not look inside regular files - only at directory listings (which store filenames) and inodes (which store metadata, including the "type" in the sense mentioned earlier).
So, the ls command cannot show the file type in the sense of whether it is a JPG image or a binary file or a text file or a LibreOffice document of some kind, because it does not have that information.
For that, as singrium's answer points out, you need the file command, which looks at the first 50-100kB or so of files' contents to determine their type.
One form of filetype is whether a file is a regular file, directory, device, symlink, etc. ls can show this using options -l or -F.
Option
-lwill show the filetype as a single character at the start of a listing, e.g:$ ls -l drwxrwxr-x 2 user user 4096 Dec 11 00:18 DIR -rw-rw-r-- 1 user user 0 Dec 11 00:18 FILE lrwxrwxrwx 1 user user 4 Dec 11 00:19 LINK -> FILEWhere
-is a regular file,dis a directory, andlis a symlink.Option
-Fwill show the filetype as a suffix, e.g:$ ls -F DIR/ FILE LINK@Where no suffix is a regular file,
/is a directory, and@is a symlink.
More info on these is available in info coreutils 'ls invocation', summed up here:
For -l:
‘-’ regular file
‘b’ block special file
‘c’ character special file
‘C’ high performance (“contiguous data”) file
‘d’ directory
‘D’ door (Solaris 2.5 and up)
‘l’ symbolic link
‘M’ off-line (“migrated”) file (Cray DMF)
‘n’ network special file (HP-UX)
‘p’ FIFO (named pipe)
‘P’ port (Solaris 10 and up)
‘s’ socket
‘?’ some other file typeFor -F:
nothing for regular files
‘*’ regular files that are executable
‘/’ directories
‘@’ symbolic links
‘|’ FIFOs
‘=’ sockets
‘>’ doors here is another way using paste to merge two output of ls and file command
paste <(ls -dl *) <(file * | cut -d: -f2)