This method works perfectly in any shell.
NOTE: $$ holds the process id of current shell.
[penguin@cheetah:/home/penguin]#ps | grep -i `echo $$`
18530 0:00 ksh
[penguin@cheetah:/home/penguin]#bash
bash-3.00$ ps | grep -i `echo $$`
18574 pts/196 0:00 bash
bash-3.00$ csh
cheetah% ps | grep -i `echo $$`
18578 pts/196 0:00 csh
cheetah% tcsh
> ps | grep -i `echo $$`
18590 0:00 tcsh
Thursday, October 18, 2012
Monday, October 1, 2012
What is meant by Pipe and Filter in Unix or Linux?
PIPE:
Pipe is a way of passing data from standard output of one process to standard input of another process.
Filter:
It is a program that by default reads from standard input and writes to standard output.
Friday, September 28, 2012
How to list only hidden files in UNIX?
ls -1A | grep '^\.'
Eg:
[vjsujay@cheetah:/home/vjsujay]#ls -1A | grep '^\.'
.profile
.recently-used
.sh_history
.softwareupdate
.ssh
[vjsujay@cheetah:/home/vjsujay]#
FYI.
-a will list hidden files along with default directories single dot(.) and double-dot(..)
-A will list hidden files without single dot and double dot
Eg:
[vjsujay@cheetah:/home/vjsujay]#ls -1A | grep '^\.'
.profile
.recently-used
.sh_history
.softwareupdate
.ssh
[vjsujay@cheetah:/home/vjsujay]#
[vjsujay@cheetah:/home/vjsujay]#ls -1a | grep '^\.'
.
..
.profile
.recently-used
.sh_history
.softwareupdate
.ssh
[vjsujay@cheetah:/home/vjsujay]#
FYI.
-a will list hidden files along with default directories single dot(.) and double-dot(..)
-A will list hidden files without single dot and double dot
Thursday, September 27, 2012
How to update and replace files in a tar archive?
Question 1: How to update an existing tarball contents with newfile(s)?
Answer: We have to use the function ‘u’ to update newfile(s) to an
existing tarball.
Question 2: How to replace an existing tarball contents with newfile(s)?
Answer: We have to use the function ‘r’ to replace newfile(s) to an
existing tarball.
I am providing the definition of function update(u) and replace(c)
as per solaris man page.
Function letters Function
r
Replace. The named files are written at the end of the tarfile
u
Update. The named files are written at the end of the
tarfile if they are not already in the tarfile, or if they have
been modified since last written to that tar-file.
Example:
[st39422@dios8:/home/st39422]#ls -l fileABC.tar Aiszipped.zip
Biszipped.zip
-rw-r--r-- 1 st39422 users
7486342 Aug 8 14:19 Aiszipped.zip
-rw-r--r-- 1 st39422 users
11256018 Aug 8 14:19 Biszipped.zip
-rw-r--r-- 1 st39422
users 8192 Aug 6 12:55 fileABC.tar
Creation of a tarball: AB.tar
[st39422@dios8:/home/st39422]#tar -cvf AB.tar Aiszipped.zip
Biszipped.zip
a Aiszipped.zip 7311K
a Biszipped.zip 10993K
[st39422@dios8:/home/st39422]#
Contents of the created tarball: AB.tar
[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug 8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug 8 14:19 2012 Biszipped.zip
[st39422@dios8:/home/st39422]#
Updating a file(fileABC.tar) to the existing tar archive(AB.tar):
[st39422@dios8:/home/st39422]#tar -uvf AB.tar
fileABC.tar
a fileABC.tar 8K
[st39422@dios8:/home/st39422]#
[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug 8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug 8 14:19 2012
Biszipped.zip
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
[st39422@dios8:/home/st39422]#
If you try to update the already available file in the archive, it
will be added at the end of the archive; provided the file is modified.
Otherwise it will be ignored.
[st39422@dios8:/home/st39422]#tar -uvf AB.tar fileABC.tar
[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug 8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug 8 14:19 2012
Biszipped.zip
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
[st39422@dios8:/home/st39422]#
Replace an existing file will always add the mentioned file at the
end of the archive:
[st39422@dios8:/home/st39422]#tar -rvf AB.tar fileABC.tar
a fileABC.tar 8K
[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug 8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug 8 14:19 2012
Biszipped.zip
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
[st39422@dios8:/home/st39422]#
Replacing multiple files:
[st39422@dios8:/home/st39422]#tar -rvf AB.tar fileABC.tar
fileABC.tar
a fileABC.tar 8K
a fileABC.tar 8K
[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug 8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug 8 14:19 2012
Biszipped.zip
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
[st39422@dios8:/home/st39422]#
Updating multiple files:
[st39422@dios8:/home/st39422]#tar -uvf AB.tar fileA fileB
a fileA 1K
a fileB 1K
[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug 8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug 8 14:19 2012
Biszipped.zip
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
-rw-r--r-- 48153/63002 8192 Aug 6 12:55 2012
fileABC.tar
-rw-r--r-- 48153/63002 227 Aug 6 12:42 2012
fileA
-rw-r--r-- 48153/63002 227 Aug 6 12:43 2012
fileB
How to pass large number of files as an argument for tar archive creation?
Question: How to pass large number of files as an argument for tar archive
creation?
Answer: Using the option ‘I’
From Solaris Man page:
-I include-file Opens include-file containing a list
of files, one per line, and treats
it as if each file
appeared separately on the command line. Be
careful of trailing white spaces. Also beware of leading
white spaces,
since, for each line in the included file, the entire line
(apart from
the newline) will be used to match against the initial string
of files
to include
SYNTAX: tar –cvf archive.tar -I filename
archive.tar -> your desired archive name
filename -> pass the file name which has list of files to
be archived
Example:
Listing the available files:
[st39422@dios8:/home/st39422]#ls -ltr
total 64
-rw-r--r-- 1 st39422
users 227 Aug 6 12:42 fileA
-rw-r--r-- 1 st39422
users 227 Aug 6 12:43 fileB
-rw-r--r-- 1 st39422
users 19 Aug 6 12:47
fileD
-rw-r--r-- 1 st39422
users 19 Aug 6 12:49
fileC
Putting filenames in a file and checking the content:
[st39422@dios8:/home/st39422]#cat > files_to_be_archived
fileA
fileB
fileC
fileD
[st39422@dios8:/home/st39422]#cat files_to_be_archived
fileA
fileB
fileC
filed
Creating a tar archive:
[st39422@dios8:/home/st39422]#tar -cvf multiple_files.tar -I
files_to_be_archived
a fileA 1K
a fileB 1K
a fileC 1K
a fileD 1K
Listing tar file contents:
[st39422@dios8:/home/st39422]#tar -tvf multiple_files.tar
tar: blocksize = 10
-rw-r--r-- 48153/63002 227 Aug 6 12:42 2012
fileA
-rw-r--r-- 48153/63002 227 Aug 6 12:43 2012
fileB
-rw-r--r-- 48153/63002 19 Aug 6 12:49
2012 fileC
-rw-r--r-- 48153/63002 19 Aug 6 12:47
2012 fileD
[st39422@dios8:/home/st39422]#
NOTE: Depending on the type and version of tar utility, option
needs to be used ( -I or –L eg: tar -cvf abc.tar -L jskfile or tar
-cvf xyz.tar -I jskfile )
How to extract the desired file/directory from a tar archive in Unix or Linux or Solaris
Question: To extract the desired file/directory from a tarball
Answer: Pass the desired file/directory as an extra argument to the
normal tar extraction command
Syntax: tar –xvf filename.tar
desiredfilename/dirname
Example: To extract a file which is under two directories
1.
First we need to know the location of the file in the tarball
( tar –tvf filename.tar | grep filename)
2.
Secondly we need to apply that location as an additional
argument ( tar –xvf filename.tar “output of last command” )
Now the command part,
[vjsujay@cheetah:/home/vjsujay]#tar -tvf abcd.tar | grep FileD
tar: blocksize = 15
-rw-r--r-- 48153/63002 14 Aug 2 12:41
2012 ./c/d/FileD
[vjsujay@cheetah:/home/vjsujay]#tar -xvf abcd.tar ./c/d/FileD
tar: blocksize = 15
x ./c/d/FileD, 14 bytes, 1 tape blocks
How to find the default shell in UNIX or Linux or Solaris
Question: Default shell of the logged in User
Answer: There are many ways to find the default shell.
a)
By checking the last column in the /etc/passwd file ( grep –i
username /etc/passwd )
Eg:
[vjsujay@cheetah:/home/vjsujay]#grep -i vjsujay /etc/passwd
vjsujay:x:48153:63002:Sujay-kumar:/home/vjsujay:/bin/ksh
b)
By displaying the value of global environment variable SHELL
( echo $SHELL )
Eg: [vjsujay@cheetah:/home/vjsujay]#echo
$SHELL
/bin/ksh
NOTE: This will give
correct default shell name as output as long as your current shell is your
default shell
c)
By using the command echo $0
Eg: [vjsujay@cheetah:/home/st39422]#echo $0
-ksh
Wednesday, September 26, 2012
What is the flow in UNIX or UNIX like operating system for commands execution?
As soon as we press the enter key after the command, the below sequence will be followed.
1. Check alias and then execute the command(shell-built-in or external command) accordingly
2. Shell built-in command
3. External command
Note 1: If command is executed with absolute path then external command or script will run. Else above sequence will be followed.
Note 2: If PATH value is empty, then shell will presume that the command is available in the current directory and hence it will try to run it from current directory.
1. Check alias and then execute the command(shell-built-in or external command) accordingly
2. Shell built-in command
3. External command
Note 1: If command is executed with absolute path then external command or script will run. Else above sequence will be followed.
Note 2: If PATH value is empty, then shell will presume that the command is available in the current directory and hence it will try to run it from current directory.
Friday, September 14, 2012
Why we shouldn't give 777 to any directory in UNIX or LINUX?
Giving 777 to any directory in UNIX or Linux or UNIX based Operating System is a potential security risk. Because if a directory contains 777 permission, then any user can get inside the directory and remove other users files or directories. At times, we may need to permit all users to get in to the same directory and also need to allow them to create files and directories. In that case, we have to set 'sticky' bit for the particular directory to avoid the issue of removal of other users files intentionally.
Tuesday, September 11, 2012
How to find RAM size, CPU speed and CPU count in Solaris Sparc System
1.)Question: How to find the number of CPU, CPU Speed and RAM size?
Ans: prtdiag
Command Name: prtdiag
Purpose: To display the system diagnostics information
Ex:
[st39422@dios8:/home/st39422]#/usr/sbin/prtdiag -v
System Configuration: Sun Microsystems sun4u Sun Ultra 45
Workstation
System clock frequency: 200 MHZ
Memory size: 2GB
==================================== CPUs
====================================
E$
CPU
CPU
CPU Freq Size
Implementation Mask
Status Location
--- -------- ----------
--------------------- -----
------ --------
0 1600 MHz
1MB
SUNW,UltraSPARC-IIIi 3.4
on-line MB/0
============================ Memory Configuration
============================
Segment Table:
-----------------------------------------------------------------------
Base Address Size Interleave
Factor Contains
-----------------------------------------------------------------------
0x0
2GB
4 BankIDs 0,1,2,3
[st39422@dios8:/home/st39422]#
2.) Question: How to find the number of CPU?
Ans: psrinfo
Command Name: psrinfo
Purpose: To display information about processors
Ex:
[st39422@dios8:/home/st39422]#psrinfo -p
1
3.) Question: How to find the memory size(RAM)?
Ans: prtconf
Command Name: prtconf
Purpose: To print system configuration
Ex:
[st39422@dios8:/home/st39422]#prtconf -v | grep 'Memory'
Memory size: 2048 Megabytes
Sunday, April 10, 2011
How to find empty files in UNIX?
There are multiple ways to find whether a file is empty or not. Please see few examples below,
volcano@volcano-laptop:~/shellscript$ ls -ltr | grep '\<0\>'
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:41 sujay
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:42 jsk
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:45 a
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:46 b
-rw-r--r-- 1 volcano volcano 0 2011-04-10 22:30 emptyfile
volcano@volcano-laptop:~/shellscript$ ls -ltr | awk ' $5==0 {print}'
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:41 sujay
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:42 jsk
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:45 a
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:46 b
-rw-r--r-- 1 volcano volcano 0 2011-04-10 22:30 emptyfile
volcano@volcano-laptop:~/shellscript$ for fname in `ls`;do if [ ! -s $fname ];then ls -l $fname;fi;done
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:45 a
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:46 b
-rw-r--r-- 1 volcano volcano 0 2011-04-10 22:30 emptyfile
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:42 jsk
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:41 sujay
volcano@volcano-laptop:~/shellscript$ find . -maxdepth 1 -size 0 -ls
58438 0 -rw-r--r-- 1 volcano volcano 0 Mar 12 15:42 ./jsk
58434 0 -rw-r--r-- 1 volcano volcano 0 Mar 12 15:46 ./b
58437 0 -rw-r--r-- 1 volcano volcano 0 Mar 12 15:41 ./sujay
58211 0 -rw-r--r-- 1 volcano volcano 0 Mar 12 15:45 ./a
58234 0 -rw-r--r-- 1 volcano volcano 0 Apr 10 22:30 ./emptyfile
volcano@volcano-laptop:~/shellscript$ ls -ltr | grep '\<0\>'
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:41 sujay
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:42 jsk
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:45 a
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:46 b
-rw-r--r-- 1 volcano volcano 0 2011-04-10 22:30 emptyfile
volcano@volcano-laptop:~/shellscript$ ls -ltr | awk ' $5==0 {print}'
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:41 sujay
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:42 jsk
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:45 a
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:46 b
-rw-r--r-- 1 volcano volcano 0 2011-04-10 22:30 emptyfile
volcano@volcano-laptop:~/shellscript$ for fname in `ls`;do if [ ! -s $fname ];then ls -l $fname;fi;done
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:45 a
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:46 b
-rw-r--r-- 1 volcano volcano 0 2011-04-10 22:30 emptyfile
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:42 jsk
-rw-r--r-- 1 volcano volcano 0 2011-03-12 15:41 sujay
volcano@volcano-laptop:~/shellscript$ find . -maxdepth 1 -size 0 -ls
58438 0 -rw-r--r-- 1 volcano volcano 0 Mar 12 15:42 ./jsk
58434 0 -rw-r--r-- 1 volcano volcano 0 Mar 12 15:46 ./b
58437 0 -rw-r--r-- 1 volcano volcano 0 Mar 12 15:41 ./sujay
58211 0 -rw-r--r-- 1 volcano volcano 0 Mar 12 15:45 ./a
58234 0 -rw-r--r-- 1 volcano volcano 0 Apr 10 22:30 ./emptyfile
Saturday, April 2, 2011
How to remove empty lines from a file in UNIX or Linx
volcano@volcano-laptop:~/shellscript/empty$ cat testempty
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$ sed '/^$/d' testempty > testwithoutempty
volcano@volcano-laptop:~/shellscript/empty$ cat testwithoutempty
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$
volcano@volcano-laptop:~/shellscript/empty$ grep -v '^$' testempty > without
volcano@volcano-laptop:~/shellscript/empty$ cat without
hi first line
above line is empty
hi dude
above line is empty
One exception case is available for empty line removal. That is line will be empty but it will have space or tab characters. To handle that, we can use the below command format.
grep -v '^[]*]$' oldfile > newfile
volcano@volcano-laptop:~/shellscript/empty$ cat test
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$ grep -v '^[ ]*$' test > newtest
volcano@volcano-laptop:~/shellscript/empty$ cat newtest
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$
volcano@volcano-laptop:~/shellscript/empty$ sed '/^$/d' test
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$ sed '/^[ ]*$/d' test
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$ sed '/^$/d' testempty > testwithoutempty
volcano@volcano-laptop:~/shellscript/empty$ cat testwithoutempty
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$
volcano@volcano-laptop:~/shellscript/empty$ grep -v '^$' testempty > without
volcano@volcano-laptop:~/shellscript/empty$ cat without
hi first line
above line is empty
hi dude
above line is empty
One exception case is available for empty line removal. That is line will be empty but it will have space or tab characters. To handle that, we can use the below command format.
grep -v '^[
volcano@volcano-laptop:~/shellscript/empty$ cat test
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$ grep -v '^[ ]*$' test > newtest
volcano@volcano-laptop:~/shellscript/empty$ cat newtest
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$
volcano@volcano-laptop:~/shellscript/empty$ sed '/^$/d' test
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$ sed '/^[ ]*$/d' test
hi first line
above line is empty
hi dude
above line is empty
volcano@volcano-laptop:~/shellscript/empty$
How to remove CONTROL-M characters in UNIX or Linux
Control-m characters will get appended to a file when a file is transferred from windows to UNIX machine. There are multiple ways it can be removed.
Using vi editor:
:%s/^M//g
Using col command:
cat filename | col -b > newfilename
Using sed command:
sed 's/^M//g' filename > newfilename
Using dos2unix comand:
dos2unix filename newfilename
Examples:
volcano@volcano-laptop:~/shellscript/ctrl$ cat -v jsk
hi^M
jsk^M
volcano@volcano-laptop:~/shellscript/ctrl$ cat jsk | col -b > jsk.new
volcano@volcano-laptop:~/shellscript/ctrl$ cat -v jsk.new
hi
jsk
volcano@volcano-laptop:~/shellscript/ctrl$ sed 's/^M//g' jsk > jsk.new2
volcano@volcano-laptop:~/shellscript/ctrl$ cat -v jsk.new2
hi
jsk
Note: Hold the control key and then press v and m to get the control-m character
Using vi editor:
:%s/^M//g
Using col command:
cat filename | col -b > newfilename
Using sed command:
sed 's/^M//g' filename > newfilename
Using dos2unix comand:
dos2unix filename newfilename
Examples:
volcano@volcano-laptop:~/shellscript/ctrl$ cat -v jsk
hi^M
jsk^M
volcano@volcano-laptop:~/shellscript/ctrl$ cat jsk | col -b > jsk.new
volcano@volcano-laptop:~/shellscript/ctrl$ cat -v jsk.new
hi
jsk
volcano@volcano-laptop:~/shellscript/ctrl$ sed 's/^M//g' jsk > jsk.new2
volcano@volcano-laptop:~/shellscript/ctrl$ cat -v jsk.new2
hi
jsk
Note: Hold the control key and then press v and m to get the control-m character
Saturday, December 4, 2010
How to remove files starting with - minus in the filename
There might be several ways to remove files starting with first character as - minus. But i am providing 3 solutions below.
1. $ rm ./-filename
2. $ rm -- -filename
3. Find the inode number by using ls -i and then use the below command
find . -inum 45678 -exec rm {} \;
1. $ rm ./-filename
2. $ rm -- -filename
3. Find the inode number by using ls -i and then use the below command
find . -inum 45678 -exec rm {} \;
Monday, January 25, 2010
Difference between Unix Internal & External Commands
Internal Unix commands are built with the shell and hence it will run in the same shell.
eg: cd
External Unix commands will start a new shell and then it gets executed
eg: ls
Note: If you execute a shell script, it will create or fork a new shell and then execute the commands present in the script file. You can also execute the shell script in the same shell by using source or . infront of the shell script.
eg:
sujay@laptop:~/scripts$ cat test.sh
cd /
echo "Current dir is `pwd`"
sujay@laptop:~/scripts$ test.sh
Current dir is /
sujay@laptop:~/scripts$ pwd
/home/sujay/scripts
sujay@laptop:~/scripts$ source test.sh
Current dir is /
sujay@laptop:/$ pwd
/
eg: cd
External Unix commands will start a new shell and then it gets executed
eg: ls
Note: If you execute a shell script, it will create or fork a new shell and then execute the commands present in the script file. You can also execute the shell script in the same shell by using source or . infront of the shell script.
eg:
sujay@laptop:~/scripts$ cat test.sh
cd /
echo "Current dir is `pwd`"
sujay@laptop:~/scripts$ test.sh
Current dir is /
sujay@laptop:~/scripts$ pwd
/home/sujay/scripts
sujay@laptop:~/scripts$ source test.sh
Current dir is /
sujay@laptop:/$ pwd
/
Saturday, January 23, 2010
How to run a unix shell script from any directory
To run a script from anywhere inside UNIX directory, you need to include the script absolute directory in the PATH variable. After that without using the pathname we can execute the script successfully.
eg:
In .profile, update PATH=$PATH:/home/sujay/scripts and source the .profile
Script Loc: /home/sujay/scripts/
Script Name; anywhere
sujay@laptop:~$ anywhere
++ pwd
+ echo 'Executed from /home/sujay'
Executed from /home/sujay
eg:
In .profile, update PATH=$PATH:/home/sujay/scripts and source the .profile
Script Loc: /home/sujay/scripts/
Script Name; anywhere
sujay@laptop:~$ anywhere
++ pwd
+ echo 'Executed from /home/sujay'
Executed from /home/sujay
Saturday, October 10, 2009
Order of execution for /etc/profile .bash_profile .bash_login .profile /etc/bash.bashrc .bashrc
If interactive login-shell or non-interactive login shell with --Login option is started, it will start reading & executing in the order /etc/profile, .bash_profile, .bash_login and finally .profile
If interactive shell, that is a non-login shell is started, it will start reading & executing in the order /etc/bash.bashrc, .bashrc
If interactive shell, that is a non-login shell is started, it will start reading & executing in the order /etc/bash.bashrc, .bashrc
Labels:
.bash_profile,
.bashrc,
.profile,
/etc/bash.bashrc,
/etc/profile
Difference between .profile and .bashrc
.profile will be read, whenever login-shell is started
.bashrc will be read, whenever new interactive shell(not a login shell) is started
.bashrc will be read, whenever new interactive shell(not a login shell) is started
Monday, August 24, 2009
How to compress multiple files using tar and gzip
1. Put all the required filenames in a file
2. tar all the files
3. gzip the tar file
eg:
vjsujay@gmail.com$ ls *.html > filelist
vjsujay@gmail.com$ fname=filelist
vjsujay@gmail.com$ tar -cvf multihtml.tar -L $fname
vjsujay@gmail.com$ gzip multihtml.tar
Final file will be multihtml.tar.gz
Monday, July 20, 2009
To display only the user directories in Unix or Linux
vjsujay@home$ ls -lA "pipeline symbol" grep '^d'
drwxr-s--- 2 n0160429 ccipadm 512 Jul 21 00:17 .temp
drwxr-s--- 2 n0160429 ccipadm 512 Mar 15 04:58 cal
drwxr-s--- 2 n0160429 ccipadm 512 May 06 04:50 dump
drwxr-s--- 2 n0160429 ccipadm 512 May 05 23:14 script
drwxr-s--- 2 n0160429 ccipadm 512 Jul 18 12:28 temp
Note: If you use ls -la means, you will get . (dot) and .. (dot dot) directory also.
Subscribe to:
Posts (Atom)