Thursday, June 07, 2007

Shell Scripts

1/
Use command time to start the watch


$ time cat


Use Ctrl + D to stop the watch. You cannot see the progress.


2/
There is a way to put current working directory into stack and use it later.
This is really helpful when working in real projects to save time.

You can use command pushd . to put current working directory and use popd to move to directory in the stack.

3/
There is a way to add multiple users (in batch) to fedora server.
Adding number of users to the system using useradd and passwd command takes time and bit boaring.

There is a way to automate this.


You can use newusers command, which update and create new users in batch.

This command reads a file of user name and clear-text password pairs and uses this information to update a group of existing users or to create new users. Each line is in the same format as the standard password file.


Since username and passwords are stored in clear text format make sure only root can read/write the file. Use chmod command:


# touch /root/batch-user-add.txt
# chmod 0600 /root/batch-user-add.txt


Create a user list as follows. Open file:

# vi /root/batch-user-add.txt



Append username and password:

user1:password:1001:513:Student Account:/home/user1:/bin/bash
user2:password:1002:513:Sales user:/home/user2:/bin/bash
user100:password:1100:513:Sales user:/home/user100:/bin/bash
tom:password:1110:501:Guest Account:/home/guest:/bin/menu
jerry:password:1120:501:Guest Account:/home/guest:/bin/menu



Now create users in batch:

# newusers /root/batch-user-add.txt

4/
Way to find the hard disk usage by each users during maintenance.

Following simple command can be used.

du --max-depth=1 /home/ | sort -n -r


If you need to see the information in human readable format.


du -H --max-depth=1 /home/user

5/
Replace a directory path in number of files to a given directory path.
All the files has .metadat extension.


Can use sed to do it.

1) Create a shell script as given below.

for fl in *.metadata; do
echo $f1
mv $fl $fl.old
#sed 's_/home/buddhini_/home/test_g' $fl.old > $fl
sed 's_$src_$dest_g' $fl.old > $fl
done


Note : Replace
/home/buddhini with your directory path to find in the files
/home/test with replacing directory path

"_" is used as delimeter in the above file

2) Place the in the directory where you have *.metadata files

3) Make the file executable

$ chmod +x


4) Execute the shell script
Orginal files are saved with *.old extension.

6/
Change the file's created date property

Can use the touch command to change the file access and modification time.


$ touch -t

No comments: