10 KiB
Lesson 2.1.1
Guided Exercises
- Split the lines below into the components of command, option(s)/parameter(s) and argument(s):
cat -n /etc/passwd
ls -l /etc
ls -l -a
cd /home/user
- Find what type the following commands are: (extenal commands or builtin)
pwd
mv
cd
cat
exit
- Resolve the following commands that use quotes:
echo "$HOME is my home directory"
touch "$USER"
touch 'touch'
Explorational Exercises
- With one command and using brace expansion in Bash (review the man page for Bash), create 5 files numbered 1 to 5 with the prefix game (game1, game2, ...).
- Delete all 5 files that you just created with just one command, using a different special character (review Pathname Expansion in the Bash man pages).
- Is there any other way to make two commands interact with each other? What are those?
Lesson 2.1.2
Guided Exercises
- Create a local variable
number
. - Create an environment variable
ORDER
, using one of the two above methods. - Display both the variable names and their contents.
- What are the scopes of the previously created variables?
Explorational Exercises
- Create a local variable
nr_files
and assign the number of lines found in the/etc/passwd
file. Hint: Look into the commandwc
and command substitution and don’t forget about quotation marks. - Create an environment variable
ME
. Assign theUSERNAME
variable’s value to it. - Append the
HOME
variable’s value toME
, having the : delimiter. Display the contents of theME
variable. - Using the date example above, create a variable called
today
and assign the date for one of the time zones. - Create another variable called
today1
and assign the system’s date to it.
Lesson 2.2
Guided Exercises
- Use the
man
command to find out what each command does:ls
cat
cut
cd
cp
mv
mkdir
touch
wc
passwd
rm
rmdir
more
less
whereis
head
tail
sort
tr
chmod
grep
- Open the
ls
info page and identify the MENU.- What options do you have?
- Find the option that allows you to sort the output by modification time.
- Display the path to the first 3 README files. Use the
man
command to identify the correct option for locate. - Create a file called test in your home directory. Find its absolute path with the
locate
command. - Did you find it immediately? What did you have to do in order for locate to find it?
- Search for the test file that you previously created, using the
find
command. What syntax did you use and what is the absolute path ?
Explorational Exercises
-
There is one command in the table above that doesn’t have a man page. Which one is it and why do you think that the command doesn’t have a man page?
-
Using the commands in the table above, create the following file tree. The names that start with a capital are Directories and the ones in lower case are files.
User ├── Documents | ├──Hello | | ├──hey2 | | ├──helloa | | └──ola5 | └──World | └──earth9 ├── Downloads | ├──Music | └──Songs | ├──collection1 | └──collection2 ├── Test | └── passa └── test
-
Display on the screen the present working directory, including the subfolders.
-
Search within the tree for all files that end with a number.
-
Remove the entire directory tree with a single command.
Lesson 2.3.1
Guided Exercises
-
For each of the following paths, identify whether it is absolute or relative:
- /home/user/Downloads
- ../Reports
- /var
- docs
- /
-
Observe the following file structure. Note: Directories end with a slash (/) when tree is invoked with the -F option. You will need elevated privileges in order to run the tree command on the root (/) directory. The following is example output and is not indicative of a full directory structure. Use it to answer the following questions:
$ sudo tree -F / / ├── etc/ │ ├── network/ │ │ └── interfaces │ ├── systemd/ │ │ ├── resolved.conf │ │ ├── system/ │ │ ├── system.conf │ │ ├── user/ │ │ └── user.conf │ └── udev/ │ ├── rules.d/ │ └── udev.conf └── home/ ├── lost+found/ └── user/ └── Documents/ 12 directories, 5 files
- A user enters the following commands:
$ cd /etc/udev $ ls -a
- What will be the output of the
ls -a
command?
-
Enter the shortest possible command for each of the following:
- Your current location is root (/). Enter the command to navigate to lost+found within the home directory (example):
- Your current location is root (/). Enter the command to navigate to the directory named /etc/network/.
- Your current location is /home/user/Documents/. Navigate to the directory named /etc/.
- Your current location is /etc/systemd/system/. Navigate to the directory named /home/user/.
-
Consider the following commands:
$ pwd /etc/udev/rules.d $ cd ../../systemd/user $ cd .. $ pwd
- What is the output of the final
pwd
command?
- What is the output of the final
Explorational Exercises
-
Suppose a user has entered the following commands:
$ mkdir "this is a test" $ ls this is a test
- What cd command would allow you to enter this directory?
-
Try this again, but after typing in cd this, press the TAB key. What is now displayed on the prompt?
- This is an example of autocompletion, which is an invaluable tool not only for saving time, but for preventing spelling errors.
-
Try to create a directory whose name contains a
\
character. Display the directory’s name with ls and delete the directory.
Lesson 2.3.2
Guided Exercises
-
Use the following file structure to answer the following three questions:
/ ├── etc/ │ ├── network/ │ │ └── interfaces/ │ ├── systemd/ │ │ ├── resolved.conf │ │ ├── system/ │ │ ├── system.conf │ │ ├── user/ │ │ └── user.conf │ └── udev/ │ ├── rules.d │ └── udev.conf └── home/ ├── lost+found/ ├── user/ │ └── Documents/ └── michael/ └── Music/
- What command will navigate into the network directory regardless of your current location?
- What command can user enter to navigate into their Documents directory from
/etc/udev
? Use the shortest possible path. - What command can user enter to navigate into michael 's Music directory? Use the shortest possible path.
-
Consider the following output of
ls -lh
to answer the next two questions. Note that directories are indicated with ad
at the beginning of the line.drwxrwxrwx 5 eric eric 4.0K Apr 26 2011 China/ -rwxrwxrwx 1 eric eric 1.5M Jul 18 2011 img_0066.jpg -rwxrwxrwx 1 eric eric 1.5M Jul 18 2011 img_0067.jpg -rwxrwxrwx 1 eric eric 1.6M Jul 18 2011 img_0074.jpg -rwxrwxrwx 1 eric eric 1.8M Jul 18 2011 img_0075.jpg -rwxrwxrwx 1 eric eric 46K Jul 18 2011 scary.jpg -rwxrwxrwx 1 eric eric 469K Jan 29 2018 Screenshot from 2017-08-13 21-22-24.png -rwxrwxrwx 1 eric eric 498K Jan 29 2018 Screenshot from 2017-08-14 21-18-07.png -rwxrwxrwx 1 eric eric 211K Jan 29 2018 Screenshot from 2018-01-06 23-29-30.png -rwxrwxrwx 1 eric eric 150K Jul 18 2011 tobermory.jpg drwxrwxrwx 6 eric eric 4.0K Apr 26 2011 Tokyo/ -rwxrwxrwx 1 eric eric 1.4M Jul 18 2011 Toronto 081.jpg -rwxrwxrwx 1 eric eric 1.4M Jul 18 2011 Toronto 085.jpg -rwxrwxrwx 1 eric eric 944K Jul 18 2011 Toronto 152.jpg -rwxrwxrwx 1 eric eric 728K Jul 18 2011 Toronto 173.jpg drwxrwxrwx 2 eric eric 4.0K Jun 5 2016 Wallpapers/
- When you run the command
ls -lrS
, what file will be at the beginning? - Please describe what you expect to see as the output for
ls -ad */
.
- When you run the command
Explorational Exercises
- Run the
ls -lh
command in a directory that contains subdirectories. Note the listed size of these directories. Do these file sizes seem correct to you? Do they accurately represent the contents of all files inside that directory? - Here is a new command to try:
du -h
. Run this command and describe the output that it gives you. - On many Linux systems, you can type in
ll
and get the same output as you would if you typedls -l
. Please note however thatll
is not a command. For example,man ll
will give you the message that no manual entry exists for it. This is an example of analias
. Why might aliases be useful to a user?
Lesson 2.4
Guided Exercises
-
Given the following, select the directories that would be created by the command
mkdir -p /tmp/outfiles/text/today /tmp/infiles/text/today
.$ pwd /tmp $ find . ./outfiles ./outfiles/text
- /tmp
- /tmp/outfiles
- /tmp/outfiles/text
- /tmp/outfiles/text/today
- /tmp/infiles
- /tmp/infiles/text
- /tmp/infiles/text/today
-
What does
-v
do formkdir
,rm
, andcp
? -
What happens if you accidentally attempt to copy three files on the same command line to a file that already exists instead of a directory?
-
What happens when you use
mv
to move a directory into itself? -
How would you delete all files in your current directory that start with old?
-
Which of the following files would
log_[a-z]_201?_*_01.txt
match?log\_3\_2017_Jan_01.txt
log_+_2017_Feb_01.txt
log_b_2007_Mar_01.txt
log_f_201A_Wednesday_01.txt
-
Create a few globs to match the following list of file names:
- doc100
- doc200
- doc301
- doc401
Explorational Exercises
- Use the
cp
man page to find out how to make a copy of a file and have the permissions and modification time match the original. - What does the
rmdir -p
command do? Experiment with it and explain how it differs fromrm -r
. - DO NOT ACTUALLY EXECUTE THIS COMMAND: What do you think
rm -ri /*
will do? (HONESTLY, DO NOT ATTEMPT TO DO THIS!) - Other than using
-i
, is it possible to preventmv
from overwriting destination files? - Explain the command
cp -u
.