Cracking IT Interview


file ​(Know file type)


This command helps to classify file format. Overall file test process consist of 3 steps : file-system, magic and language test.


In file system test it is able to print files like ".txt" text file, ".exe" executable file, ".dat" data file etc. Also it checks that file is empty file or not and it is done by the system call.


In second magic test files are being tested for particular fixed format like "a.out" which are binary executable files. if the file does not match with any of the magic entries (in path /usr/share/misc/magic) then it goes for the language tests, checks if it is a text file and gives the result like  ASCII, ISO-8859 or extended-ASCII character sets. 






Examples:


$file check_file
check_file: empty

$
$file file3.txt.gz
file3.txt.gz: gzip compressed data, was "file3.txt", from Unix, last modified: Sun Jan  1 01:18:32 2017

$
$file file5
file5: ASCII text

$
$file linkfile1
linkfile1: symbolic link to `file1.txt'

$
$file A_dir
A_dir: directory
$



Options with file command:


  • file -b  : brief, it tries to give you brief information of file.


  • file -i   : Gives the output in mime type rather than more human readable form.


  • file -k : Keep going, it goes to all the 3 tests even satisfies the present one.


  • file -m : it checks for all the line in file/ path for the magic file and gives the message invalid if not found. 


  • file -v : gives you the version with magic path


  • file -z : tries to look inside the zip file for information.



Examples:


$file ShortStory.txt.gz
ShortStory.txt.gz: gzip compressed data, was "ShortStory.txt", from Unix, last modified: Thu Dec 29 01:04:40 2016
$file -b ShortStory.txt.gz
gzip compressed data, was "ShortStory.txt", from Unix, last modified: Thu Dec 29 01:04:40 2016
$
$file linkfile1
linkfile1: symbolic link to `file1.txt'
$file -i linkfile1
linkfile1: application/x-symlink
$
$
$file -k linkfile1
linkfile1: symbolic link to `file1.txt'
$file -k text
text: empty
$
$file -m ./A_dir
./A_dir/file1.txt, 1: Warning: offset `ls' invalid
./A_dir/file1.txt, 1: Warning: type `ls' invalid
./A_dir/file2.txt, 1: Warning: offset `ls' invalid
./A_dir/file2.txt, 1: Warning: type `ls' invalid
file: could not find any magic files!
$
$file -v
file-5.03
magic file from /usr/share/misc/magic
$
$file -z ShortStory.txt.gz
ShortStory.txt.gz: UTF-8 Unicode English text, with very long lines (gzip compressed data, was "ShortStory.txt", from Unix, last modified: Thu Dec 29 01:04:40 2016)
$






​​

locate (File Location)


The locate program searches for the pattern of file names or path names in the locate database "/var/db/locate.database" and when it find the match print the full path on the screen. The database is updated periodically weekly "/etc/periodic/weekly/310.locate" or daily bases and path names are stored here which are publicly accessible.


Example:


$locate locate
/etc/locate.rc
/etc/periodic/weekly/310.locate
/home/cero/eduard/etc/locate.rc
/home/cero/eduard/etc/periodic/weekly/310.locate
/home/guest/charon/nmap-6.01/scripts/broadcast-novell-locate.nse
/home/guest/charon/nmap-6.01/scripts/broadcast-versant-locate.nse
/home/guest/ghlee/tomb/vi.locate
/home/guest/hackle/nmap-7.12/scripts/broadcast-novell-locate.nse
/home/guest/hackle/nmap-7.12/scripts/broadcast-versant-locate.nse
/home/guest/maciek11/nmap-6.25/scripts/broadcast-novell-locate.nse
/home/guest/maciek11/nmap-6.25/scripts/broadcast-versant-locate.nse


here, first "locate" is a command and second "locate" acts as a pattern, similarly:


$locate vikas
/home/suraj099/desktop/mycomputer/e/vikash
/home/suraj099/desktop/mycomputer/e/vikash/a2.txt
/home/suraj099/desktop/mycomputer/e/vikash/a3.txt
/home/suraj099/desktop/mycomputer/e/vikash/a4.txt
/home/suraj099/desktop/mycomputer/e/vikash/a6.txt
/home/dhiraj84/vikas.ksh
/home/guest/abhik84/vikas
/home/guest/abhik84/vikas/age
/home/guest/abhik84/vikas/education


Options with locate:


  • locate -s : prints statistic about the locate database.


  • locate -c : count the pattern result suppressing the normal output.


  • locate -i  : ignore case, takes both uppercase & lowercase letters.



Examples:


$locate -S

Database: /var/db/locate.database
Compression: Front: 15.64%, Bigram: 64.60%, Total: 11.55%
Filenames: 704734, Characters: 32110175, Database size: 3708941
Bigram characters: 1312947, Integers: 8696, 8-Bit characters: 373
$
$locate -c vikas
21
$
$locate -i LOCATE
/etc/locate.rc
/etc/periodic/weekly/310.locate
/home/cero/eduard/etc/locate.rc
/home/cero/eduard/etc/periodic/weekly/310.locate
/home/guest/charon/nmap-6.01/scripts/broadcast-novell-locate.nse
/home/guest/charon/nmap-6.01/scripts/broadcast-versant-locate.nse
/home/guest/gaoyuan/pros/org/apache/hadoop/fs/LocatedFileStatus.class
/home/guest/gaoyuan/pros/org/kohsuke/rngom/parse/compact/CompactSyntax$LocatedString.class
/home/guest/ghlee/tomb/vi.locate
/home/guest/hackle/nmap-7.12/scripts/broadcast-novell-locate.nse
/home/guest/hackle/nmap-7.12/scripts/broadcast-versant-locate.nse
/home/guest/maciek11/nmap-6.25/scripts/broadcast-novell-locate.nse
/home/guest/maciek11/nmap-6.25/scripts/broadcast-versant-locate.nse
$


NEXT->