about 'file' command

Forum rules
Before you post please read how to get help
ckonn
Level 2
Level 2
Posts: 50
Joined: Wed Oct 01, 2014 7:03 pm

about 'file' command

Postby ckonn » Mon May 01, 2017 10:49 am

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 ckonn on Tue May 02, 2017 8:12 am, edited 2 times in total.
linux mint 17.3 mate

User avatar
Flemur
Level 13
Level 13
Posts: 4635
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: about 'file' command

Postby Flemur » Mon May 01, 2017 10:59 am

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.
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?

lmuserx4849
Level 4
Level 4
Posts: 492
Joined: Wed Dec 17, 2014 2:55 am

Re: about 'file' command

Postby lmuserx4849 » Tue May 02, 2017 12:38 am

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 -

zcot
Level 1
Level 1
Posts: 24
Joined: Wed Oct 19, 2016 6:08 pm

Re: about 'file' command

Postby zcot » Tue May 02, 2017 6:30 pm

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 13
Level 13
Posts: 4635
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: about 'file' command

Postby Flemur » Tue May 02, 2017 7:10 pm

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
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?

lmuserx4849
Level 4
Level 4
Posts: 492
Joined: Wed Dec 17, 2014 2:55 am

Re: about 'file' command

Postby lmuserx4849 » Wed May 03, 2017 2:03 am

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 9
Level 9
Posts: 2501
Joined: Sun Aug 09, 2015 10:00 am

Re: about 'file' command

Postby deepakdeshp » Fri May 26, 2017 7:51 am

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, and keeps the forum clean.
I am using Mint 18.3 Cinnamon 64 bit with AMD processor . Memory 8GB

donalduck
Level 3
Level 3
Posts: 102
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: about 'file' command

Postby donalduck » Fri May 26, 2017 5:28 pm

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.


Return to “Scripts & Bash”