about 'file' command

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
ckonn
Level 3
Level 3
Posts: 180
Joined: Wed Oct 01, 2014 7:03 pm

about 'file' command

Post by ckonn »

Hello,

I would like to know what argument should I add to the 'file' command, so it will print not only the type of the specific file, but also the size of the file in human readable format. or this could be made using a pipe of two commands!

thanks in advance
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Flemur
Level 20
Level 20
Posts: 10097
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: about 'file' command

Post by Flemur »

With almost all commands you can do

Code: Select all

man find
or

Code: Select all

find --help 
to get the parameters and such. Also handy if you're not up on 'vi-style' commands, is something like

Code: Select all

man find > a.txt
then you can look at a.txt with a text editor.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
lmuserx4849

Re: about 'file' command

Post by lmuserx4849 »

ckonn wrote: ...
I would like to know what argument should I add to the 'file' command, so it will print not only the type of the specific file, but also the size of the file in human readable format. or this could be made using a pipe of two
commands!
...
file will display the type/mimetype. For size look at: du, or ls, or stat.
The documentation pointed to by the URL's is also available on your system via the manual system.
I believe all of these are part of the GNU core utilities.
See also bash manual/reference.

If you are not familar with the manual system, all you need to know to get started is type man CMD (replace CMD) at a terminal command prompt.
man -k STRING (replace STRING) will search a basic part of the manual documentation and display a list of man page names.

The man system uses a pager called less. Some simple commands to type:
  • Get out: q
  • Top: g
  • Bottom: G
  • Forward Search: /STRING (replace STRING)
  • Backward Search: ?STRING (replace STRING)
  • Get stuck: Hit ESC
  • Up/Down: Arrow keys work
Assuming you want to view all files that end with .sh. It could be as simple as:
ls -lh *.sh
du -h *.sh
stat -c '%s %n' *.sh

A little fancier:

Code: Select all

for f in ./*.sh; do type=$(file -b "$f"); stat -c "%n@$type@%s" "$f";  done | column -ts '@'

# The snippet below is the same as the above, except not all on one line. Notice the semi-colons are removed.
#
  for f in ./*.sh; do                               # f will contain a file name, one after another. *.sh is a glob pattern.  Always double-quote file names.
      type=$(file -b "$f")                      # Capture the output of the command. $() is call command substitution. 
      stat -c "%n@$type@%s" "$f"     # -c takes a format string.  Output separated by "@".
  done | column -ts '@'                     #  Output from the for-done loop (stdout) is being piped (|) into the column command (stdin). Easier to read.
P.S.
The man pages on your system can easily be converted to HTML, TEXT, or PDF. The examples below use "FIND".

Create a HTML file and open in your browser (Change FIREFOX if necessary)
zcat "$(man -w FIND)" | groff -mandoc -Thtml > FIND.html ; firefox FIND.html

Create a text file and open in your favorite editor (Change KATE and options)
man FIND | col -b | kate --startanon --new --stdin

Create a PDF document and open it in your favorite pdf viewer (Change OKULAR and options)
man -t FIND | ps2pdf - | okular --unique -
User avatar
zcot
Level 9
Level 9
Posts: 2781
Joined: Wed Oct 19, 2016 6:08 pm

Re: about 'file' command

Post by zcot »

offtopic since I'm not giving an answer, but additionally, I find myself always typing the command and --help before anything as mentioned above, then branch out from there.

so:

Code: Select all

file --help
Usage: file [OPTION...] [FILE...]
Determine type of FILEs.

      --help                 display this help and exit
  -v, --version              output version information and exit
  -m, --magic-file LIST      use LIST as a colon-separated list of magic
                               number files
  -z, --uncompress           try to look inside compressed files
  -Z, --uncompress-noreport  only print the contents of compressed files
  -b, --brief                do not prepend filenames to output lines
  -c, --checking-printout    print the parsed form of the magic file, use in
                               conjunction with -m to debug a new magic file
                               before installing it
  -e, --exclude TEST         exclude TEST from the list of test to be
                               performed for file. Valid tests are:
                               apptype, ascii, cdf, compress, elf, encoding,
                               soft, tar, text, tokens
  -f, --files-from FILE      read the filenames to be examined from FILE
  -F, --separator STRING     use string as separator instead of `:'
  -i, --mime                 output MIME type strings (--mime-type and
                               --mime-encoding)
      --apple                output the Apple CREATOR/TYPE
      --extension            output a slash-separated list of extensions
      --mime-type            output the MIME type
      --mime-encoding        output the MIME encoding
  -k, --keep-going           don't stop at the first match
  -l, --list                 list magic strength
  -L, --dereference          follow symlinks (default if POSIXLY_CORRECT is set)
  -h, --no-dereference       don't follow symlinks (default if POSIXLY_CORRECT is not set)
  -n, --no-buffer            do not buffer output
  -N, --no-pad               do not pad output
  -0, --print0               terminate filenames with ASCII NUL
  -p, --preserve-date        preserve access times on files
  -P, --parameter            set file engine parameter limits
                               indir        15 recursion limit for indirection
                               name         30 use limit for name/use magic
                               elf_notes   256 max ELF notes processed
                               elf_phnum   128 max ELF prog sections processed
                               elf_shnum 32768 max ELF sections processed
  -r, --raw                  don't translate unprintable chars to \ooo
  -s, --special-files        treat special (block/char devices) files as
                             ordinary ones
  -C, --compile              compile file specified by -m
  -d, --debug                print debugging messages
User avatar
Flemur
Level 20
Level 20
Posts: 10097
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: about 'file' command

Post by Flemur »

You could make a little script, here's a very crude one:

$ cat bin/File

Code: Select all

ls -l $1 |  tr "\n" " " ; file $1
Then

Code: Select all

$ File a.txt
-rw-rw-r-- 1 username username 1122 May  2 16:53 a.txt a.txt: ASCII text
You could use awk, cut, etc, to extract the size only.

Here's another one:

Code: Select all

$ cat bin/File
du -ab $1 |  tr "\n" " " ; file $1
Then

Code: Select all

$ File a.txt
1122	a.txt a.txt: ASCII text
1122 = bytes, set with "-b" on du; can have other size formats.

Edit:

Code: Select all

du -ab $1 |  tr "\n" " " | tr $1 " " ; file $1

Code: Select all

$ File a.txt
1122	      a.txt: ASCII text
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
lmuserx4849

Re: about 'file' command

Post by lmuserx4849 »

zcot wrote:offtopic since I'm not giving an answer, but additionally, I find myself always typing the command and --help before anything as mentioned above, then branch out from there.
...
Good catch on the CMD --help or CMD -h.
deepakdeshp
Level 20
Level 20
Posts: 12333
Joined: Sun Aug 09, 2015 10:00 am

Re: about 'file' command

Post by deepakdeshp »

If you want to know about man command

Code: Select all

man man
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
donalduck
Level 4
Level 4
Posts: 234
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: about 'file' command

Post by donalduck »

Hi ckonn,

a simpler answer to your question could be to define a bash function like this:

Code: Select all

show () { paste <(du -h "$1") <(file -b "$1"); }
note that this simple function only accept 1 file argument...

and if you are satisfied, add this definition in your home .bashrc

of course the entire bash manual is a must read as already said.
Locked

Return to “Scripts & Bash”