I want to make a script that shows what type of file is
for example :
./file.sh haha test lala ssss
haha --> file
test --> directory
lala --> symbolic link
ssss --> executable
Im trying to do like this but I dont get the same result ...
#!/bin/bash
function check_symboliclink
{
echo "Write symbolic link"
read something
find $something -type l
echo "$something symbolic link";
}
check_symboliclink
Is there someone have better idea to do like the example ?
You can just use
file
command.Example:
UPDATE
If you can't use
file
command here is another solution.In this script i use
for var in "$@"
, where"$@"
means all passed arguments. So usingfor
you can loop through all passed argumentsYou can add another check if you want, read this.
for example, if you want to add check
file is a socket
:just add same if block before
done
word:I change only condition
[ -S $var ]
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,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 theman find
for filetype to letter correspondence):Of course, if there is a need, we can always make use of
[
ortest
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.And that's how it would work:
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:And for performing tests with "magic numbers" we'd use
file
command:which also can do
mimetype
's job: