CSCI 344
Quiz 2
Wednesday October 8
You may use any non-living reference. In other words, you can use
books and the web but you cannot communicate with anyone.
Write the answers in an ascii text file. Copy your file to:
/user/projects/csci344/quizN/USERNAME/quizN
where N is the quiz number and USERNAME is your ecst username.
Write your name at the top of your answer file.
1) Write a bash script that recursively finds all files with a given extention in the given directory:
$ mylocate /home/sam cpp
will print a list of all files in /home/sam that end with ".cpp".
This function will also consider all sub-directories of /home/sam
and the sub-directories of those directories, and so on. You may use any
Linux commands.
Hint: The find utility makes this script very easy to write.
2) Write a bash script that renames all the files in the current directory from their old extention to a new extension:
$ rename <old extension> <new extension>
For example:
$ rename cc cpp
will rename all the files in the current directory with the .cc extension to .cpp:
$ ls
a.cc
b.cc
c.cc
x.txt
$ rename cc cpp
$ ls
a.cpp
b.cpp
c.cpp
x.txt
$
Hint: Don't use ls to find all the files that need to be renamed, use bash's * to find them--it is easier.
3) Write a bash script that makes
a local backup copy of the given files. The first argument is the
name of the backup directory and all the subsequent arguments are the
files to be copied to the backup directory.
If the backup directory does not exist, create it. Then create a subdirectory in the backup directory with the given name:
./backup_dir/year.month.day-hour:minute:second
In the following example, mybackup is called twice (directories are shown by ls in blue):
$ ls
a.cpp
a.h
b.cpp
b.h
$ mybackup backup *.cpp *.h
$ ls
a.cpp
a.h
backup
b.cpp
b.h
$ ls backup
08.10.08-09:49:19
$ ls backup/08.10.08-09:49:19
a.cpp
a.h
b.cpp
b.h
$
two minutes pass
$ mybackup backup *.cpp
$ ls backup
08.10.08-09:49:19
08.10.08-09:51:14
$ ls backup/08.10.08-09:51:14
a.cpp
b.cpp
$
Hints: You can format the output of the date command to
exactly match the name of the subdirectory. Here is an example of
how to get date to format its output:
$ date '+The year is %y'
The year is 08
$
The bash shift command is helpful.