How to know your machine is 32 or 64 bit?

Here is an easy way to find this, in command line type

$ getconf LONG_BIT

Synopsis:

getconf [-v specification] system_var

the getconf utility shall write to the standard output the value of the variable specified by the system_var operand.

So it prints the value of ‘LONG_BIT’.

Read linux manual for getconf: http://linux.die.net/man/1/getconf

 

Shell command for killing all the processes together, that we found using grep command

Sometime when we found a lot of process associates with a software or something that may hurt our memory or CPU, and it would be nice to kill that all process and be our system in a fine position. But when there is so many process we can’t kill all the process by hand using the command

 $ kill -9 PID

where ‘PID’ is the process id.

Shell commands will help us to do this. See below

First you create a .sh file file named ‘killAllProcess.sh’.

#!/bin/bash
echo "Usage : ./killAllProcess.sh "
for i in `ps ax | grep $1 | grep -v grep | sed 's/ *//' | sed 's/[^0-9].*//'`
do
  kill -9 $i
done
echo "Killed All the process which has name with $1"

And make this file executable

$ chmod +x killAllProcess.sh 

Then run it

 $ ./killAllProcess.sh 

And see any process with that name is there

$ ps aux | grep PROCESS_NAME

All vanishes..!!

Cron and Crontab in Linux

  1. Cron is a daemon that executes scheduled commands in linux.
  2. Also known as clock daemon.
  3. This daemon execute so many jobs known as cron jobs.
  4. This cron jobs are described in crontab files in UNIX systems.

How to add a cron job in crontab files?

    • There are almost 7 categories. That are listed below.
      1. Minute : Add the minute here (eg : 12, 45)
      2. Hour : Add hour here (eg: 6, 17)
      3. Day Of month : Add day of month (eg : 10, 25)
      4. Month : Add month here (eg : 5, Dec, jan)
      5. Day Of Week : Add day of week (eg : 0=> sunday, 5 => friday)
      6. User : Add username
      7. Command : Add Your command here.
    An Example

Open the terminal and type
$ vim /etc/crontab
and add the following line (you can see the time by typing the date command)
50 15 * * * abhi sh /home/abhi/Project/my_project_no1/my_first_cron_job.sh
my my_first_cron_job.sh file contains:
cd /home/abhi/Projects/myProject/ && touch file1.txt file2.txt
Note : Don’t forget to give executable permission to my_first_cron_job.sh file
Here my my_first_cron_job.sh file executes at 15hr 50 min all days and creates two files named file1 and file2 in my /home/abhi/Projects/myProject folder.

# Example of job definition:
# .—————- minute (0 – 59)
#  |   .————- hour (0 – 23)
#  |   |  .———- day of month (1 – 31)
#  |   |  |   .——- month (1 – 12) OR jan,feb,mar,apr …
#  |   |  |   |   .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
#  |   |  |   |   |
# * * * * * user-name command to be executed

Note : You can also use * mark that denotes ‘all’. For example If you give 3 * * in day of month, month, day of week respectively ,that means it executes all month 3rd day whatever cron job you given.

Programming in UNIX Environment : Using the shell

A greater than sign ‘>’ followed by a name makes a file of that name. the -n option of echo command is for omiting the newline. the ‘-l’ option for the ‘who’ command shows the details of users. The shell is a command like all unix commands. It is represented by ‘sh’.

$ > file //creates a file named file.
$ echo -n you are lucky
$ echo /* //prints the all the folders in root (not recursively)
$ echo \* //prints a ‘*’ character
$ who -l

Programming in UNIX Environment : Commands – sort, tail, cmp, diff

The command ‘sort’ is for sorting the line of the text. ‘tail’ prints the last ten lines of the file. ‘cmp’ for showing the difference between the files. It shows only the first difference. But ‘diff’ shows all the difference between two files.

sort filename1 file2
sort -r // reverse normal order.
sort -n // numeric order.
sort -f // fold upper and lower case together.
sort -b // Ignore the leading blanks.

tail filename // prints last 10 lines.
tail -3 filename // print last 3 lines.

cmp filename1 filename2 // prints only the 1st difference.

diff filen1 filen2 // prints all differences.

pwd – For printing current working directory.
cp – For copying a file.

cp /home/abhi/Pclass/num.c num.c

ed /home/abhi/Pclass/num.c

Programming in UNIX Environment : Commands – mv, rm, wc, grep

The ‘mv’ command is using for renaming. And ‘rm’ command for removing files. ‘wc’ for counting lines,words,characters. The ‘grep’ command is for searching a word. ‘-v’ for searching all words except the indicating word in a line.

mv filename newname

rm filename1 filename2

wc filename1 filename2 // count lines,words,characters.
wc -l //only counts the new lines.
wc -w //print the word count.
wc -m //print the character counts.
wc -c //printf the byte counts.

grep int filename // for searching the word int.
grep -v int filename // all except int.

Programming in UNIX Environment : Commands – kill, ps, nohup, nice

The ampersand at the end of the command says that run this command and also take another from the terminal without stopping the first. It prints the process id. For stopping a process we use ‘kill’ command followed by process id. ‘ps’ command prints what that user running. Using ‘nohup’, the command will run even if the user logged out. ‘nice’ is for using another system to run our job.

$wc a* >wc.out &

$kill 6578

$ps

$ps -ag // all process that are currently running.

$nohup command & // No hangup for this command even if user logged out.

$nice expensive-command & // if your command take a lot of processor resources.

The ‘at’ command followed by time is to run our job at whatever time we want.

$ at 2340 <file

There is a file ‘.profile’ in the user’s login directory, this file contains commands which the user want to run when the user logged in. A user can set things in it.

a=/long/directory/name

$cd $a

We can put ‘a’ as given above in our profile. So we can refer that directory by the value of shell variable.

Unix file system : Devices

/dev/rp00 and /dev/rp01 are named after the DEC RP06 disc drive attached to the system. We cannot make a link to a file in another subsystem. One reason is that the inode numbers are not unique in different file systems.
$ df
$ tty
$ mesg n
$ mesg y

The ‘df’ command tells the available space in the mounted file subsystem. The ‘tty’ command tells which terminal we are using. So we can send files and informations to that terminal and it appears in the corresponding terminal. ‘mesg n’ turn off the messages. /dev/tty is a synonym for our terminal.

If we want to run a program and its output files are not importent we can redirect it to ‘/dev/null’ causes its output to be thrown away.

Unix file system : The directory hierarchy

When the system starts the file /boot is read, it then reads /unix (it is the program for the UNIX kernal itself).

The following shows some directories which reside in root and its contents.

/bin : basic programs like who,ed reside.

/etc/rc : This is a file of shell commands which is executed after the system is bootstrapped.

/etc/group : lists the members of each group.

/lib : Contains the parts of the C compiler.

/tmp : Contains temporary (short-lived) files that are created in the execution of programs.

Unix file system : Inodes

The administrative infomation is stored in the inode. It contains information such as how long it is, where the contents of the file are stored on disc etc.
System’s internal name for a file is its i-number.

$ ls -lut
$ ls -lct
$ ls -i
$ ls -ict

‘rm’ command is for removing a file. ‘mv’ command is for moving and renaming the files.

$ rm filename1 filename2
$ mv filename1 filename2

The -ut lists the file in order of usage time. -ct lists the files in the order of inode change time. The ‘-i’ option is for listing the i-numbers of the directories contents. ‘-ict’ lists inode number in order of the modifications in the inode (most recently first).