Friday, March 13, 2009
Wednesday, June 11, 2008
How to keep track of your Unix/Linux command execution and its output
Definition: The script command will keep a transcript(written record) of everything you say to the computer and everything that the computer responds to you.
Syntax: script [capturefilename]
*) To start the script command
eg: vjsujay$ script cmdlog
Note: cmdlog will be created in the current dir and also if you are not specifying the filename means, it will create a default filename 'typescript'
*) To stop capturing or script command,
eg: vjsujay$ exit
Different ways of running a Shell Script
vjsujay@linuxdemon$sample.sh
2. Executing the Shell Script file using specified Shell
vjsujay@linuxdemon$bash sample.sh
3. Executing the Shell Script file in the current Shell
vjsujay@linuxdemon$source sample.sh
or
vjsujay@linuxdemon$. sample.sh
4. Executing the Shell Script file using exec command
vjsujay@linuxdemon$ exec sample.sh
Wednesday, April 16, 2008
Copying longfilenames without retyping
-rw-r--r-- 1 Ajay None 13 Apr 17 03:22 thisismytestfile
Ajay@intel ~
$ cp thisismytestfile{,.bk}
Ajay@intel ~
$ ls -l thisismytest*
-rw-r--r-- 1 Ajay None 13 Apr 17 03:22 thisismytestfile
-rw-r--r-- 1 Ajay None 13 Apr 17 03:26 thisismytestfile.bk
Monday, June 25, 2007
How to Truncate a file in Unix/Linux
If the filename already exists,the file will be truncated to ZERO byte file else a new file with ZERO byte will be created.
Example:
[vjsujay@phenix vjsujay]$ ls -l jsk
-rw-r----- 1 vjsujay free 46 Jun 25 20:14 jsk
[vjsujay@phenix vjsujay]$ : > jsk
[vjsujay@phenix vjsujay]$ ls -l jsk
-rw-r----- 1 vjsujay free 0 Jun 25 20:15 jsk
One more example with 3 different methods:
volcano@volcano-laptop:~/test/trunc$ ls -ltr
total 12
-rw-r--r-- 1 volcano volcano 14 2011-02-23 11:56 rado
-rw-r--r-- 1 volcano volcano 14 2011-02-23 11:57 radoo
-rw-r--r-- 1 volcano volcano 14 2011-02-23 11:57 radooo
volcano@volcano-laptop:~/test/trunc$ > rado
volcano@volcano-laptop:~/test/trunc$ :>radoo
volcano@volcano-laptop:~/test/trunc$ true > radooo
volcano@volcano-laptop:~/test/trunc$ ls -ltr
total 0
-rw-r--r-- 1 volcano volcano 0 2011-02-23 11:57 rado
-rw-r--r-- 1 volcano volcano 0 2011-02-23 11:57 radoo
-rw-r--r-- 1 volcano volcano 0 2011-02-23 11:57 radooo
Tuesday, May 15, 2007
Commenting Multiple lines in a shell script
eg:
: << COMMENT
echo "Not in the Scope of debugging/execution"
a=5
b=3
let c=a+b
echo $c
COMMENT
The statements in between the anonymous here document will not be executed.
Tuesday, May 8, 2007
Length of a string in Linux
Example:
linuxdemon$lindem="linux demon"
linuxdemon$echo ${#lindem}
11
Explanation:
1.Assigning the string linux demon to the variable lindem.
2.Determining and printing the length of the string.
Wednesday, April 18, 2007
FTP Automation using Shell script
ftp -n servername/serverIP << FTP_AUTO
user giveusername givepassword
Any ftp command you can give here
bye
FTP_AUTO
Explanation:
1. -n is used to restrict the ftp from attempting auto login.
2. << here document concept applied here and hence the tag is FTP_AUTO
3. user is the ftp command
4. You can give any ftp command after the user command to do your job.
5.End the here document with the same tag FTP_AUTO
Monday, April 16, 2007
How to mount WIN partition in Linux?
Temporary Mounting: After the reboot of your system, you need to again execute the mount command to mount the win partition in Linux.
1.You need to create mount point(directory) for each win partition(drives) inside the /mnt directory.
eg: mkdir /mnt/winc (You can give any name instead of winc)
2. By using the mount command, we can now mount the windows partition
eg: mount -t vfat /dev/hda1 /mnt/winc (In linux, first partition is called as hda1)
3. Now get inside the /mnt/winc directory and check the files.You will be able to see your files from C drive.
eg: cd /mnt/winc
Permanent Mounting: To do the permanent mounting, you need to follow these steps below.
1.Create mount points (mkdir /mnt/winc)
2.Edit the /etc/fstab file using any text editor like vi and insert the mount point and file system details at the end of the file with the proper spacing as previous lines.
eg: /dev/hda1 /mnt/winc vfat
3. After the reboot of your system, you will be able to access all C drive files.
How to rename group of files?
for filename in *.jsk
do
mv $filename `basename $filename jsk`txt
done
Explanation:
1.In the for statement, i am finding out all the .jsk extension files.
2.In the mv statement, i am using basename command to strip the jsk extension.