@laks wrote:
Lets begin Day-5,
- How to identify file type without opening?
- First cut it then paste and finally fold a file
- FileSystem hierarchy structure
- Split one big file into multiple small files
- How to check for free disk fedori?
- Remember this !!!!
- How long the machine is up and running?
- Who is it?
- What's your hostname and version
How to identify file type without opening?
Do you remember ,often you get some junk mails , claiming they have sent you a 'special' attachment.
You have downloaded it but not sure whether to open it or not,because your afraid of virus (wait...virus? they
are irrevelant with linux ..lets
try another example).hmm..you have downloaded a file named "something.pdf" but when you tried to open with pdf reader ,it didn't op
en.
what to do now?the best way is to check to the file type before opening or playing them using "file" command.Here comes few examples -
`
[efg@fedori C-pdf]$ file Glade-Tutorial.pdf
Glade-Tutorial.pdf: PDF document, version 1.4[efg@fedori ~]$ file MalgudiDays.mp3
MalgudiDays.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1,
128 kbps, 44.1 kHz, St ereo
`
[OT, did you heard about "Malgudi Days" by R.K.Narayan,My favorite one is "A Tiger for Maigudi" a story from t
iger's point of view.]
[efg@fedori ~]$ file extlib.ext3
extlib.ext3: Linux rev 1.0 ext3 filesystem datafile command don't use extention part to determine the file type. For example, we created dummy file named "e
xt2fs.pdf" like
[efg@fedori ~]$ echo "this file contains some random text" > ext2fs.pdf
we can't determine the file with 'ls' output -
[efg@fedori ~]$ ls -ltr ext2fs.pdf
-rw-rw-r--. 1 efg efg 36 2011-05-08 13:32 ext2fs.pdf
Now lets try 'file' -
[efg@fedori ~]$ file ext2fs.pdf
ext2fs.pdf: ASCII text
As you can see ,file says just a "ASCII text" file not pdf.Its always better to verify the downloaded file type before running or opening them.
First cut it then paste and finally fold a file:
From our secretfile,if you want cut first column then try
[efg@fedori ~]$ cut -f1 -d' ' secretfile
This
This
Note
If
I'm
option f1 means cut first column and d refers to delimiter. you want to cut second column use -f2 instead of -
f1.I created two files (1.txt and 2.txt)
[efg@fedori ~]$ cat 1.txt
file1: line1
file1: line2
[efg@fedori ~]$ cat 2.txt
file2: line1
file2: line2
In order to merge the lines of these two files we do paste -
[efg@fedori ~]$ paste 1.txt 2.txt
file1: line1 file2: line1
file1: line2 file2: line2
finally if we use fold, it will wrap each line to specified width,below will wrap the text to 5 characters per line.
[efg@fedori ~]$ fold -w 5 1.txt
file1
: lin
e1
file1
: lin
e2FileSystem hierarchy structure
Now its time to understand linux filesystem hierarchy structure.Its not hard, good news is you already learned
them without realizing it :).
Remember , when you first logged in ,it checked /etc/passwd file and finds the entry "/home/efg" ?Normally Configuration files go to /etc directory. and all Users home directory will be /home/user-name except
admin's home which is /root.And also remember when we searched for commands location with $PATH which gave something like-
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/efg/bin/usr/local/bin and /usr/bin will have user commands/appliations.
/usr/sbin will contain root user commands.Here is more -
/tmp - any temporary files created and deleted in memory RAM. If you create some file
at /tmp directory and reboot the machine.These files won't be there when the machine
comes up.
/var - will contain log files
/proc - Real time process information stored by kernel.
/dev - will contains device files
/boot - this directory content is used for booting process.
/lib - contains system library files and kernel modules
/opt - third party softwares can be installed here.
/root - again, system administrators home directory. (try ls /root and check out your admin's
secret contents :D ,if possible. )
/mnt - where external devices can be mounted.
All above directories are subdirectory of / - the root directorySplit one big file into multiple small files :
[efg@fedori ~]$ ls -l secretfile
-rw-------. 1 efg efg 218 2011-05-08 12:27 secretfile
the file has 218 bytes as size. If we want to split this file to 2 files of 109 bytes each with command like
-
[efg@fedori ~]$ split -b 109 secretfile
[efg@fedori ~]$ ls -l
If you do a 'ls' you will find two new files with perfix 'x' with 109 bytes each -
-rw-------. 1 efg efg 218 2011-05-08 12:27 secretfile
-rw-rw-r--. 1 efg efg 109 2011-05-16 10:04 xab
-rw-rw-r--. 1 efg efg 109 2011-05-16 10:04 xaa
Lets check out their contents
[efg@fedori ~]$ cat xaa
This is some random data for the file
This will go as second line.
Note that we used >> to append data to thesecond file content is -
[efg@fedori ~]$ cat xab
file.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..as you can see secretfile content is now placed in two files.This split command will be very useful when you w
ant to
transfer one big file from one machine to another.Anybody has questions ? No ..oh..yeah someone there thinking how to merge these contents again? something like
'merge' command?
hmm..good question..but wait..I just checked 'man merge',it won't fit into this.
the simple way is to use cat and append files , like
[efg@fedori ]$ cat xa* > newsecretfile
[efg@fedori ]$ cat newsecretfile
This is some random data for the file
This will go as second line.
Note that we used >> to append data to the file.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..
that's it.How to check for free disk fedori?
On windows , probably you mighti do somethin like 'right-click' on D: or C: and choose properties and there yo
u can see the size.
From linux command line is a matter of just two letters - yeah 'df'
[efg@fedori ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 140G 123G 9.6G 93% /
/dev/sda5 194M 100M 85M 55% /boot
As you can see ,I have used upto 93% of root partition and boot partition occupies 55%.
I have passed -h option to df command to make it more readable.In order to check for free fedori on RAM, do run "free" command.
Remember this !!!!
No one (no not even linus
) would remember all options of all commands.So try and make some guess for examp
le
-h may be human readable format.
-a may be run all
-r may be recursive
-i may be ...what ? hmm..install or interactive
-s may be size
-b may be block
-d may be directory
-l may be links
-v may be verbose or version
So If first-time interviewer asks you about 'how do you do that with command X' - Tell him bluntly, "First che
ck man commandname and then use it.No one can remember all options of commands."How long the machine is up and running?
uptime gives you the answer.
[efg@fedori ~]$ uptime
20:30:50 up 21 min, 3 users, load average: 0.24, 0.07, 0.09
First column denotes current time , next field shows its running for "21 minutes" (yeah...Just came back from
office and start the machine)
and the 3 users currently logged in. and approximate load average of the system in past 1,5,15 minutes.Who is it?
I like this song from MJ :D..no not that,I was thinking about "how to find out 3 users reported by uptime outp
ut."
and the command is ..who..whoo..who..who (I like that song too)
[efg@fedori ~]$ who
efg tty1 2011-05-17 20:10
efg pts/0 2011-05-17 20:11 (:0.0)
efg pts/1 2011-05-17 20:15 (:0.0)
It was me ,opened up different terminals.Not a intruders.
there is one more way for doing the same - use 'w'it combines the output for uptime and also lets us know what each user currently doing
[efg@fedori ~]$ w
20:41:18 up 31 min, 3 users, load average: 0.08, 0.16, 0.14
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
efg tty1 - 20:10 30:42 0.04s 0.01s /bin/sh /usr/bin/startx
efg pts/0 :0.0 20:11 3.00s 2.90s 7.81s gnome-terminal
efg pts/1 :0.0 20:15 0.00s 0.04s 0.00s w
What's your hostname and version
Okay,So far you have done all this on which machine and what OS or kernel version ?
Yeah..Its was pretty late, we didn't think about it till now , right? Anyway its better late than never.To find hostname
[efg@fedori ~]$ hostname
fedori
to find OS and kernel details run :
[efg@fedori ~]$ uname -a
Linux fedori 2.6.32.21-168.fc12.x86_64 #1 SMP Wed Sep 15 16:12:07 UTC 2010 x86_64 x86_64
x86_64 GNU/Linux
I'll let you to decode that uname output. Check man uname for any help.
So far we talked about file related stuffs mainly,next we will focus on process.
Posts: 1
Participants: 1