Lab 5 Exploring the Bash Shell

Sequence 1: Directory and file organization

  1. Log in as user student with the password student.

[student@stationX ~]$ pwd

/home/student

[student@stationX ~]$ touch {report,memo,graph}_{sep,oct,nov,dec}_{a,b,c}_{1,2,3}

  1. Use the ls command to examine the results of the last command. You should find that it created 108 new, empty files

[student@stationX ~]$ mkdir a_reports

[student@stationX ~]$ mkdir september october november december

Again, use ls to examine your work.

[student@stationX ~]$ cd a_reports

[student@stationX a_reports]$ mkdir one two three

ls

[student@stationX a_reports]$ cd

[student@stationX ~]$ ls -l *dec_b_?

[student@stationX ~]$ mv graph_dec_b_1 december/

[student@stationX ~]$ mv *dec_b_? december/

[student@stationX ~]$ ls -l december/

[student@stationX ~]$ mv *nov_b_? november/

[student@stationX ~]$ mv *oct_b_? october/

[student@stationX ~]$ mv *sep_b_? september/

[student@stationX ~]$ cd a_reports

[student@stationX a_reports]$ mv ~/*_a_1 one/

[student@stationX a_reports]$ cd one

[student@stationX one]$ ls *sep*

[student@stationX one]$ rm *sep*

[student@stationX one]$ ls

[student@stationX one]$ pwd

/home/student/a_reports/one

[student@stationX one]$ ls /home/student/*a_2*

[student@stationX one]$ mv /home/student/*a_2* /home/student/a_reports/two/

[student@stationX one]$ ls ../../*a_3*

[student@stationX one]$ mv ../../*a_3* ../three/

[student@stationX one]$ cd

ls

[student@stationX ~]$ mkdir /tmp/archive

[student@stationX ~]$ cp report*[12] /tmp/archive/

[student@stationX ~]$ cp -i report_dec* /tmp/archive/

cp: overwrite `/tmp/archive/report_dec_c_1′? n

cp: overwrite `/tmp/archive/report_dec_c_2′? n

[student@stationX ~]$ ls *c*

[student@stationX ~]$ ls -Fd *c*

[student@stationX ~]$ ls *c_[1-3]

[student@stationX ~]$ rm *c_[1-3]

[student@stationX ~]$ ls

Sequence 2: Automating tasks with shell scripts

  1. Consider the command:

# cp -av /etc/sysconfig ~/backups/sysconfig-yyyymmdd

# man date

/format

date ‘+%Y%m%d’

[root@stationX ~]# mkdir ~/bin/

  1. Use nano or vi, ~/bin/backupsysconfig.sh

#!/bin/bash

# This script creates a backup of /etc/sysconfig

# into a datestamped subdirectory of ~/backups/

  1. add a line

cp -av /etc/sysconfig ~/backups/sysconfig-$(date ‘+%Y%m%d’)

  1. Finally, add a line

echo “Backup of /etc/sysconfig completed at: $(date)”

  1. Save the file.

#!/bin/bash

# This script creates a backup of /etc/sysconfig

# into a datestamped subdirectory of ~/backups/

cp -av /etc/sysconfig ~/backups/sysconfig-$(date ‘+%Y%m%d’)

echo “Backup of /etc/sysconfig completed at: $(date)”

  1. remove today’s datestamp

[root@stationX ~]# rm -rf ~/backups/sysconfig-$(date ‘+%Y%m%d’)

[root@stationX ~]# chmod u+x ~/bin/backup-sysconfig.sh

[root@stationX ~]# ~/bin/backup-sysconfig.sh

  1. If you have problems, double-check your script and try using bash -x in your shbang for diagnostic output.

 

By |2015-01-01T01:39:45+00:00January 1st, 2015|Linux and Unix, Linux/Unix Basics|0 Comments

About the Author:

Leave A Comment

Show Buttons
Hide Buttons