0

How would you accomplish the file command without using it. I am trying to print all files in the current directory and their type.

./type.bash *
Desktop (Directory) 
Documents (Directory) 
Downloads (Directory) 
Music (Directory) 
list (Ordinary file)
| improve this question | | | | |
  • file is a POSIX tool and file -i is reasonably well defined. Why "without using it"? – Kamil Maciorowski Jan 10 at 9:47
  • Yeah I know, but I am having a test soon and my teacher said maybe he was going to ask us similar question on the test. – zellez Jan 10 at 10:04
  • What is in type.bash? That doesn’t look like it uses file. – G-Man Says 'Reinstate Monica' Jan 27 at 2:46
1

You can use stat.

$ touch regular_file
$ mkdir directory
$ stat -c %F regular_file directory/
regular empty file
directory
$
$ stat --format="%n: %F" regular_file directory
regular_file: regular empty file
directory: directory
| improve this answer | | | | |
1

Here is a minimalistic example if you just want to distinguish files from directories, but you are not interested in the file type.

find . -mindepth 1 -type d -printf '%f (Directory)\n' && find . -type f -printf '%f (Ordinary file)\n'

Using the find command, first list the directories, and then the files in the second command. The purpose of -mindepth 1 in the first command is to skip the . indicating the current directory.

Since find is recursive you'll probably want to add the -maxdepth 1 option.

| improve this answer | | | | |
0

If your teacher wants you to replicate the output you have shown in the question, they probably want you to do something like this:

#!/bin/sh

for f
do
        if [ -d "$f" ]
        then
                printf '%s (%s)\n' "$f" Directory
        elif [ -f "$f" ]
        then
                printf '%s (%s)\n' "$f" 'Ordinary file'
        else
                printf '%s (%s)\n' "$f" Other
        fi
done

which is pure bash.

If you prefer to use an external program, you could do:

#!/bin/sh

for f
do
        case "$(ls -ld "$f")" in
           d*)
                printf '%s (%s)\n' "$f" Directory
                ;;
           -*)
                printf '%s (%s)\n' "$f" 'Ordinary file'
                ;;
           *)
                printf '%s (%s)\n' "$f" Other
        esac
done
| improve this answer | | | | |
-1

With such a question, the first thing you should do is reading file manpage (man 1 file). As said in its documentation:

 The filesystem tests are based on examining the return from a stat(2) system
 call.  The program checks to see if the file is empty, or if it's some sort of
 special file.  Any known file types appropriate to the system you are running
 on (sockets, symbolic links, or named pipes (FIFOs) on those systems that
 implement them) are intuited if they are defined in the system header file
 <sys/stat.h>.

 The magic tests are used to check for files with data in particular fixed for‐
 mats.  The canonical example of this is a binary executable (compiled program)
 a.out file, whose format is defined in <elf.h>, <a.out.h> and possibly
 <exec.h> in the standard include directory.  These files have a “magic number”
 stored in a particular place near the beginning of the file that tells the
 UNIX operating system that the file is a binary executable, and which of sev‐
 eral types thereof.  The concept of a “magic” has been applied by extension to
 data files.  Any file with some invariant identifier at a small fixed offset
 into the file can usually be described in this way.  The information identify‐
 ing these files is read from /etc/magic and the compiled magic file
 /usr/share/misc/magic.mgc, or the files in the directory /usr/share/misc/magic
 if the compiled file does not exist.  In addition, if $HOME/.magic.mgc or
 $HOME/.magic exists, it will be used in preference to the system magic files.

file relies on magic number. So if you want to implement a file equivalent by yourself, you have first to use stat to check if it's a regular file and if it isn't empty and then, use magic number. Consider using libmagic that would do this for you.

| improve this answer | | | | |
  • ok thanks for the help – zellez Jan 10 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.