Intro1,2,Wildcards
Shell101
Enter the name of the day in the box below. For Monday enter "Mo", Tuesday is "Tu", Wednesday is "We", etc. What day was the 31th of December 2002
Tu
cal can also work with 1 parameter, which is the year. When given a year, it displays a calander for the whole year. Use this feature but redirect the output of cal so that the calander for 2005 is saved to a file called "yearfile" in the home directory of demo.
cal 2005 > yearfile
Use the ls command to see the hidden and normal files you have in the home directory for "demo". Count all the files and directories shown and enter the number below. Include in the counting "." and "..".
-bash-4.2$ ls -al ./
total 4
drwxr-xr-x. 4 demo tutorial 48 Aug 2 00:37 .
drwxr-xr-x. 4 root root 29 Aug 1 18:02 ..
drwxr-xr-x. 3 demo tutorial 17 Aug 2 00:37 .cache
drwxr-xr-x. 3 demo tutorial 17 Aug 2 00:37 .config
-rw-r--r--. 1 demo tutorial 1946 Aug 2 00:39 yearfile
-bash-4.2$ ls -al ../
total 4
drwxr-xr-x. 4 root root 29 Aug 1 18:02 .
dr-xr-xr-x. 17 root root 4096 Mar 9 2017 ..
drwx------. 3 alice alice 74 Sep 8 2014 alice
drwxr-xr-x. 4 demo tutorial 48 Aug 2 00:37 demo
5
Which file in /home/demo (including hidden files) has the smallest size? Enter the size below:
# I put the correct answer but still wrong:(
Use redirection to create a file called "thismonth", containing only this month's current calendar. Then use the date command to append the current date to the end of the same file.
# not updated
cal 2 2021 > thismonth
-date >> thismonthcal
Copy the file yearfile to yearfile2. Copy the file yearfile to yearfile3.
cp yearfile yearfile2
cp yearfile yearfile3
Change the name of file yearfile3 to thisyear.
mv yearfile3 thisyear
Delete the file yearfile.
rm -rf yearfile
Concatinate the files "thismonth", "yearfile2" and "thisyear" together to form a single big file called "bigfile". Check that your command worked using more.
cat * > bigfile
more bigfile
Directories
The top directory of the structure (demo) corresponds to /home/demo, your HOME directory and therefore the directory you start in when you log into your machine using the demo username. You DO NOT create a "demo" directory yourself to answer this question, as it already exists are you are already it in.
mkdir work letters scripts
cd work/
mkdir progs tutorial misc
Copy the files /etc/group and /etc/vimrc into your misc directory.
cd work/
cp /etc/group misc/
cp /etc/vimrc misc/
Make misc your current directory. Move the file vimrc to the progs directory. Use ls to verify that this worked.
cd misc/
mv vimrc ../progs/
ls ../progs/
While still in the misc directory using a relative path name, copy the file 'bigfile' from your home directory to the tutorial directory and rename it 'bigfile2'.
cp /home/demo/bigfile ../tutorial/bigfile2
Make the directory work your current directory and using an absolute path name, copy the file 'bigfile2' in the tutorial directory to the scripts directory.
cp ../tutorial/bigfile2 ../../scripts/bigfile2
We may use ~root for the home directory of the user root. Use cd and pwd to find the home directory of the user mysql
/var/lib/mysql
We can have spaces in our file and directory names (like Windows). File names and directory names are case sensitive (unlike Windows). Create directories with the following names in your home directories, use ls to check they are there.
mkdir 'My Documents' gordon Gordon
Wildcards
In the demo account, cd to the home directory (/home/demo). From there copy all files using the * wildcard that contain the word file in the filename into the work directory. Confirm this using ls.
cd /home/demo
cp *file* /home/demo/work/
Copy the file thismonth to the letters directory and rename it let1.doc. Change the current working directory to the letters directory and copy let1.doc to let2.doc. Copy once more let1.doc to let3.doc. Confirm this yourself using the ls command.
cp thismonth letters/let1.doc
cd letters/
cp let1.doc let2.doc
cp let1.doc let3.doc
ls
let1.doc let2.doc let3.doc
Copy using the ? wildcard the three files let1.doc, let2.doc, and let3.doc from the letters directory to the work/misc directory. Once copied, rename the new files (the ones in the misc directory) into rpt1.doc, rpt2.doc, and rpt3.doc. When renaming, you have to do this explicitly and not try to use a wildcard. It would appear at first that a wildcard could make the renaming easier, but it turns out to be ridiculously hard to use a wildcard in this case.
cp * ../work/misc/
cd ../work/misc
cp let1.doc rpt1.doc
cp let2.doc rpt2.doc
cp let3.doc rpt3.doc
Using square brackets, move all the files from the misc directory that contain the numbers 2 or 3 into the scripts directory.
mv *{2,3}.doc /home/demo/work/
Remove all files beginning with the letter "r" from the scripts directory. Use the -i option to see what you are about to delete before then agreeing to delete the files involved.
rm -rf r* /home/demo/scripts/
Create a hard link in progs called biglink, which is a hard link to the bigfile file in your home directory. In "ls -l bigfile" the hard link count should be 2. If you make mistakes and create links with the wrong name or in the wrong place you will be marked wrong until you fix the problem.
ln bigfile work/progs/biglink
Create a soft link in progs called mylink, which is a relative soft link to the thismonth file in your home directory.
ln -srf thismonth work/progs/mylink
Create a soft link in progs called mylink2, which is a absolute soft link to the thismonth file in your home directory.
cd /work/progs
ln -sf ~/thismonth "$(pwd)"/mylink2
Last updated
Was this helpful?