How to create simple interactive folder directory bash script
When I started using Linux, I always have the problem of navigating into my working folder using the terminal.The case sensitive nature of Linux make it more frustrating and often time get me demotivate and lost in my own system!!.I still remember it took me 3 minute to find the right folder and just type "cd " and "ls" along the way . I don't have strong understanding on Linux architecture and how file was arrange back then.
In this post, I'm going to share a simple script that we can run directly from the terminal to our respective working folder. This is a simple script that use if,else,elif function .
Below is the breakdown of this script and how to use it .
1 . The script start off like how a normal bash file look like.Since this is an easy script Next I'm declaring the variable that going to be use in the script. It is better to write all at once for the directory for future maintenance.
#!/bin/bash
#Title : Lazy bum directory link
#Author : shark Attack
#REV:1.0
fold_prog='/home/shark_attack/Programming'
fold_proj='/home/shark_attack/Videos/PROJECT'
fold_mus='/home/shark_attack/Music'
fold_doc='/home/shark_attack/Documents'
fold_hack='/home/shark_attack/hack
#Title : Lazy bum directory link
#Author : shark Attack
#REV:1.0
fold_prog='/home/shark_attack/Programming'
fold_proj='/home/shark_attack/Videos/PROJECT'
fold_mus='/home/shark_attack/Music'
fold_doc='/home/shark_attack/Documents'
fold_hack='/home/shark_attack/hack
2.This part of the script will print on terminal requesting input from user
echo "Please type folder selection that you want to go"
echo "1.programming"
echo "2.project"
echo "3.music"
echo "4.documents"
echo "5.hack"
echo "1.programming"
echo "2.project"
echo "3.music"
echo "4.documents"
echo "5.hack"
3. input from the user wil be save in variable call "gugu"
read gugu
4.I used "gugu" as a loop control in this script .Basic idea of this script is when the word type in the terminal is similar with the selection above, the system will navigate it to respective folder.system will then list or open a graphical user interface depending on what user want to do .
if [ "$gugu" == "project" ]; then
cd "$fold_proj" #this will change the current folder directory to desired location
ls -a #listing your directory
#nautilus "$PWD"
elif [ "$gugu" == "programming" ]; then
cd "$fold_prog"
ls
# nautilus "$PWD"
elif [ "$gugu" == "documents" ]; then
cd "$fold_doc"
ls
nautilus "$PWD" #nautilus will open a GUI from your terminal
elif [ "$gugu" == "music" ]; then
cd "$fold_mus"
ls
elif [ "$gugu" == "hack" ]; then
cd "$fold_hack"
ls
cd "$fold_proj" #this will change the current folder directory to desired location
ls -a #listing your directory
#nautilus "$PWD"
elif [ "$gugu" == "programming" ]; then
cd "$fold_prog"
ls
# nautilus "$PWD"
elif [ "$gugu" == "documents" ]; then
cd "$fold_doc"
ls
nautilus "$PWD" #nautilus will open a GUI from your terminal
elif [ "$gugu" == "music" ]; then
cd "$fold_mus"
ls
elif [ "$gugu" == "hack" ]; then
cd "$fold_hack"
ls
5. exit loop
else \
echo "folder is not in selection.please try run the command again .."
fi
echo "folder is not in selection.please try run the command again .."
fi
6. save the script in the location that was include in your system path. you can type 'echo $PATH' on your terminal . Please refer to my previous post on this .
7. Open the terminal and change the file type
shark_attack@Positive-Space:~$ chmod 754 /home/shark_attack/Programming/bash/desktop
8. Run the command. You can run the command straight from your terminal. Note that there is a different when running as "filename" or ". filename" with and without . (dot) . . (dot) is a longer version of "source"
When a script is run using "source" it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as "source.sh"
, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.please refer here for more details9. Run the command from terminal and adjust the script to what is suitable for your usage
shark_attack@Positive-Space:~$ . desktop
or
shark_attack@Positive-Space:~$ source desktop
That's it for now.Please find the script here for download and subscribe my page and youtube channel for more tips.
Read Previous :How to do progress indicator using Bash script