Pages

Monday, July 16, 2012

Shell script to create and change the directory

Script to create and change the directory



This is a small script which will create a directory provided by you and change the directory to it.

#!/bin/bash
read -p "Give the directory to create" VAR1
mkdir -p $VAR1 && cd $VAR1
echo "Present working directory is `pwd`"

Save the file and change the permissions cdmd.sh

chmod +x cdmd.sh


execute the cdmd.sh as below.

bash cdmd.sh

Create this one as function and call this function as a command

mdcd () { mkdir -p $1 && cd $1 } 

 Keep this function in ~/.bashrc file and save this file.

From now on words we can just use this command to make directory and change the directory.

How about changing  the existing directory and list the conent?

cdls () { cd $1 && ls $1; } 


change the directory and then list the folder content. Keep this function in the ~/.bashrc file for using it as a command.




0 comments:

Post a Comment