about 'file' command
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
about 'file' command
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
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: about 'file' command
With almost all commands you can do
or
to get the parameters and such. Also handy if you're not up on 'vi-style' commands, is something like
then you can look at a.txt with a text editor.
Code: Select all
man find
Code: Select all
find --help
Code: Select all
man find > a.txt
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Your data and OS are backed up....right?
Re: about 'file' command
file will display the type/mimetype. For size look at: du, or ls, or stat.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!
...
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
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.
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 -
Re: about 'file' command
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:
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
Re: about 'file' command
You could make a little script, here's a very crude one:
$ cat bin/File
Then
You could use awk, cut, etc, to extract the size only.
Here's another one:
Then
1122 = bytes, set with "-b" on du; can have other size formats.
Edit:
$ cat bin/File
Code: Select all
ls -l $1 | tr "\n" " " ; file $1
Code: Select all
$ File a.txt
-rw-rw-r-- 1 username username 1122 May 2 16:53 a.txt a.txt: ASCII text
Here's another one:
Code: Select all
$ cat bin/File
du -ab $1 | tr "\n" " " ; file $1
Code: Select all
$ File a.txt
1122 a.txt a.txt: ASCII text
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?
Your data and OS are backed up....right?
Re: about 'file' command
Good catch on the CMD --help or CMD -h.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.
...
-
- Level 20
- Posts: 12341
- Joined: Sun Aug 09, 2015 10:00 am
Re: about 'file' command
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
Regards,
Deepak
Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
Re: about 'file' command
Hi ckonn,
a simpler answer to your question could be to define a bash function like this:
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.
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"); }
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.