Previous | Home | Terminology | Next
"Hey buddy, I sure hope you enjoyed reading chapter 4 of TLCL. Let's see how attentive you were while reading."
Chapter 4 - Manipulating Files and Directories
Commands:
cp – Copy files and directories
mv – Move/rename files and directories
mkdir – Create directories
rm – Remove files and directories
ln – Create hard and symbolic links
echo - displays line of text
touch - change file timestamps (if file does not exist, it creates one)
Sometimes we want to display or manipulate only a handful of files and the concept of wildcards are used to match on a chosen number of filenames. Sometimes they are referred to a filename expansion metacharacters. Wildcards are similar to language abbreviations such as "Mr." when read out aloud will turn into "Mister", "Dr." will become "Doctor".
Here they are:
So, I hope that reading chapter 4, helped you grasp the idea of their applications. They are really cool because they allow you to do things such as file/folder removal or display in a way GUI is hopeless most of the time.
Let's see few examples:
d?g
will match any three-character filename starting with d and ending with g (dig, dog, dug, d_g, d1g, etc.)
m*n
will match any filenames regarding its lenght but starting with m and ending with n (mn, moon, man, m456n, etc.)
will match any filenames regarding its lenght but starting with m and ending with n (mn, moon, man, m456n, etc.)
b[ie]g
will match any filename starting with b followed by either i or e and ending with g (big, beg but not bieg)
will match any filename starting with b followed by either i or e and ending with g (big, beg but not bieg)
[a-z]*.c or [[:lower:]]*.c
will match any filename in lower regardless of their length but ending with .c
So, let me ask you a few questions regarding the chapter you read. See if you can answer those without looking at the answers.
Question 4-1
Symbolic link 'myfile' points to a sample.txt file. What happens if the user writes to 'myfile'?
The sample.txt will receive what was written to 'myfile.
Question 4-2
What happens if symbolic link 'myfile' is removed?
The sample.txt is not removed. Only symbolic link 'myfile' is removed.
Question 4-3
What are the two limitations of hard links that soft links don't have?
A hard link cannot reference file that is not on the same partition as the link itself. A hard link cannot reference a directory (only a file).
Question 4-4
How to distinguish between a hard link and a file it points to?
They have the same i-node number (here: 7215139)
Question 4-5
How can you create symlink using GUI?
Hold CTRL+SHIFT while dragging the file.
Lab 1
Type the following command and try to figure out what they do.
$ echo "PI's Corner!" (error)
$ echo "PI's Corner"!
$ echo "PI's Corner"'!'
$ echo {1..9}
$ echo "PI's Corner"'!'
$ echo {1..9}
$ echo {a..z}
$ echo {A..Z}
$ cd Documents; ls -l
NOTICE!
You can use many commands on the same line as long as you separate them with semicolon ';' character.
- Create a tmp directory in your home directory and change current working directory to tmp. Use only one line to accomplish this.
- Change current working directory to tmp. Using touch command create 5 files: pr1.html, pr2.html, pr3.html, pr4.html, pr5.html. Use the least number of commands possible.
- Create 5 directories named: ST1, ST2, ST3, ST4, ST5. Use the least number of commands possible.
- Create the following files: dogma, digger7, dig, dog, s1, s2.
- Devise a pattern using wildcards to match the following:
- All two-character files/directories.
- All three-characters files/directories ending with a number.
- All three-character files/directories starting with d ending with g with either i or o as the middle character.
- All filenames consisting of two characters: first lowercase letter and second a digit.
- All filenames ending with .html.
- All filenames beginning with d and ending with digit.
- All filenames beginning with p and having number somewhere.
Well done! Now you can remove the whole tmp directory.
This evening, read the chapter 5 of TLCL.
Create a tmp directory in your home directory and change current working directory to tmp. Use only one line to accomplish this.
Lab 1 Solution
Create a tmp directory in your home directory and change current working directory to tmp. Use only one line to accomplish this.
pi@raspberrypi ~ $ mkdir tmp
pi@raspberrypi ~ $
Change current working directory to tmp. Using touch command create 5 files: pr1.html, pr2.html, pr3.html, pr4.html, pr5.html. Use the least number of commands possible.
pi@raspberrypi ~/tmp $ touch pr{1..5}.html
pi@raspberrypi ~/tmp $ ls -l
total 0
-rw-r--r-- 1 pi pi 0 Apr 10 12:04 pr1.html
-rw-r--r-- 1 pi pi 0 Apr 10 12:04 pr2.html
-rw-r--r-- 1 pi pi 0 Apr 10 12:04 pr3.html
-rw-r--r-- 1 pi pi 0 Apr 10 12:04 pr4.html
-rw-r--r-- 1 pi pi 0 Apr 10 12:04 pr5.html
pi@raspberrypi ~/tmp $
Create 5 directories named: ST1, ST2, ST3, ST4, ST5. Use the least number of commands possible.
pi@raspberrypi ~/tmp $ mkdir ST{1..5}
pi@raspberrypi ~/tmp $ ls -l
total 20
-rw-r--r-- 1 pi pi 0 Apr 10 12:04 pr1.html
-rw-r--r-- 1 pi pi 0 Apr 10 12:04 pr2.html
-rw-r--r-- 1 pi pi 0 Apr 10 12:04 pr3.html
-rw-r--r-- 1 pi pi 0 Apr 10 12:04 pr4.html
-rw-r--r-- 1 pi pi 0 Apr 10 12:04 pr5.html
drwxr-xr-x 2 pi pi 4096 Apr 10 12:06 ST1
drwxr-xr-x 2 pi pi 4096 Apr 10 12:06 ST2
drwxr-xr-x 2 pi pi 4096 Apr 10 12:06 ST3
drwxr-xr-x 2 pi pi 4096 Apr 10 12:06 ST4
drwxr-xr-x 2 pi pi 4096 Apr 10 12:06 ST5
pi@raspberrypi ~/tmp $
Create the following files: dogma, digger7, dig, dog, s1, s2.
pi@raspberrypi ~/tmp $ touch dogma digger7 dig dog s1 s2
pi@raspberrypi ~/tmp $ ls -l d* s[1-2]
-rw-r--r-- 1 pi pi 0 Apr 10 12:12 dig
-rw-r--r-- 1 pi pi 0 Apr 10 12:12 digger7
-rw-r--r-- 1 pi pi 0 Apr 10 12:12 dog
-rw-r--r-- 1 pi pi 0 Apr 10 12:12 dogma
-rw-r--r-- 1 pi pi 0 Apr 10 12:12 s1
-rw-r--r-- 1 pi pi 0 Apr 10 12:12 s2
pi@raspberrypi ~/tmp $
Devise a pattern using wildcards to match the following:
- All two-character files/directories.
pi@raspberrypi ~/tmp $ ls ??
s1 s2
pi@raspberrypi ~/tmp $
- All three-characters files/directories ending with a number.
pi@raspberrypi ~/tmp $ ls ??[0-9]
ST1:
ST2:
ST3:
ST4:
ST5:
- All three-character files/directories starting with d ending with g with either i or o as the middle character.
pi@raspberrypi ~/tmp $ ls d[io]g
dig dog
pi@raspberrypi ~/tmp $
- All filenames consisting of two characters: first lowercase letter and second a digit.
pi@raspberrypi ~/tmp $ ls [a-z][0-9]
s1 s2
pi@raspberrypi ~/tmp $
- All filenames ending with .html.
pi@raspberrypi ~/tmp $ ls *.html
pr1.html pr2.html pr3.html pr4.html pr5.html
pi@raspberrypi ~/tmp $
- All filenames beginning with d and ending with digit.
pi@raspberrypi ~/tmp $ ls d*[0-9]
digger7
pi@raspberrypi ~/tmp $
- All filenames beginning with p and having number somewhere.
pi@raspberrypi ~/tmp $ ls p*[0-9]*
pr1.html pr2.html pr3.html pr4.html pr5.html
pi@raspberrypi ~/tmp $
- Remove the whole tmp directory.
pi@raspberrypi ~/tmp $ cd ..
pi@raspberrypi ~ $ rm -r tmp
pi@raspberrypi ~ $
(PS. This post has been created on the train from Tralee to home in Dublin ;). Everything for you my kids.).
Previous | Home | Terminology | Next