Showing Unix filetypes
The way this question is presented it appears as the user is trying to display type of a file in Unix filetype sense. For such purpose, one doesn't need a script, since there already exist utilities that are capable of displaying such information.
In particular, stat
command is well suited for such purpose. By default, stat
shows long output with plenty of information, but we can use -c
, --format
, or --printf
(note that it interprets backslash escape symbols) options to achieve desired output. For example,
$ stat -c '%n: %F' /etc/passwd /etc/resolv.conf /etc
/etc/passwd: regular file
/etc/resolv.conf: symbolic link
/etc: directory
$ stat --printf '%n: %F\n' /etc/passwd /etc/resolv.conf /etc
/etc/passwd: regular file
/etc/resolv.conf: symbolic link
/etc: directory
Another command which we can use is find
, which has -printf
option, but differs in conversion specifiers (the %<letter>
symbols) and the fact that it outputs filetype in letters (see the man find
for filetype to letter correspondence):
$ find /etc/passwd /etc/resolv.conf /etc -prune -printf "%p:%y\n"
/etc/passwd:f
/etc/resolv.conf:l
/etc:d
Of course, if there is a need, we can always make use of [
or test
command, but that'll require a little more work; however this way we don't have to rely on anything external; it's a portable way, although probably not ideal.
#!/bin/sh
check_filetype(){
if [ -f "$1" ];then
if [ -h "$1" ]; then
printf "%s\n" "$1: symbolic link"
else
printf "%s\n" "$1: regular"
fi
elif [ -d "$1" ]; then
printf "%s\n" "$1: directory"
elif [ -c "$1" ]; then
printf "%s\n" "$1: character device"
elif [ -p "$1" ]; then
printf "%s\n" "$1: named pipe"
elif [ -S "$1" ]; then
printf "%s\n" "$1: socket"
fi
}
for arg
do
check_filetype "$arg"
done
And that's how it would work:
$ ./checkft.sh /etc/passwd /etc/resolv.conf /etc test.fifo /dev/tty1
/etc/passwd: regular
/etc/resolv.conf: symbolic link
/etc: directory
test.fifo: named pipe
/dev/tty1: character device
Showing file content
If we wanted to get detailed information about the file type as far as contents are concerned, there's two ways to approach things. One, via mime-type and the other - via magic number, the first few bits of a file. While mime-type is originally existed for the purpose of determining filetype on the internet, nowadays many desktop applications rely on querying mime-type information for correctly displaying icons of files and opening them with proper applications.
For that purpose, there exists mimetype
command:
$ mimetype /etc/ /etc/resolv.conf /etc/passwd test.fifo
/etc/: inode/directory
/etc/resolv.conf: inode/symlink
/etc/passwd: text/plain
test.fifo: inode/fifo
And for performing tests with "magic numbers" we'd use file
command:
$ file /etc/ /etc/resolv.conf /etc/passwd test.fifo
/etc/: directory
/etc/resolv.conf: symbolic link to ../run/resolvconf/resolv.conf
/etc/passwd: ASCII text
test.fifo: fifo (named pipe)
which also can do mimetype
's job:
$ file --mime-type /etc/ /etc/resolv.conf /etc/passwd test.fifo
/etc/: inode/directory
/etc/resolv.conf: inode/symlink
/etc/passwd: text/plain
test.fifo: inode/fifo