Podcast
Questions and Answers
Which command is used to display the contents of a directory, but sorts the output by modification time with the most recently modified files listed first?
Which command is used to display the contents of a directory, but sorts the output by modification time with the most recently modified files listed first?
- `ls -l`
- `ls -r`
- `ls -t` (correct)
- `ls -u`
What is the function of the wildcard character ?
in Linux?
What is the function of the wildcard character ?
in Linux?
- Matches one character. (correct)
- Matches zero or more occurrences of the preceding character.
- Matches any digit.
- Matches any string of characters.
After using the command cp file1 file2 directory1/
, what will be the state of file1
?
After using the command cp file1 file2 directory1/
, what will be the state of file1
?
- `file1` will be copied into `directory1`, and remain in the current directory. (correct)
- The command will fail if `directory1` does not exist.
- `file1` will be renamed to `file2` in the current directory.
- `file1` will be moved into `directory1` and no longer exist in the current directory.
What happens if you try to rename a directory using mv dir1 dir2
and dir2
already exists?
What happens if you try to rename a directory using mv dir1 dir2
and dir2
already exists?
Which keyboard shortcut moves the cursor to the beginning of the current line in the terminal?
Which keyboard shortcut moves the cursor to the beginning of the current line in the terminal?
What does the command command1 | command2
do in Linux?
What does the command command1 | command2
do in Linux?
What does the >
operator do in the command ls -l > file.txt
?
What does the >
operator do in the command ls -l > file.txt
?
What does the command grep -i "text" file.txt
do?
What does the command grep -i "text" file.txt
do?
How can you use grep
to display only the names of files that contain a certain word, without showing the matching lines?
How can you use grep
to display only the names of files that contain a certain word, without showing the matching lines?
You have created a tar
archive named archive.tar
of a directory named myDir
. Which command would compress this archive using gzip?
You have created a tar
archive named archive.tar
of a directory named myDir
. Which command would compress this archive using gzip?
Flashcards
Home directory in Linux
Home directory in Linux
A special directory assigned to your user account, usually /home/username/.
Asterisk (*) in Linux wildcards
Asterisk (*) in Linux wildcards
Represents a string of any length consisting of any characters, including the empty string.
Ctrl-P shortcut in Linux
Ctrl-P shortcut in Linux
Rolls back to the previous command in the Linux terminal.
Ctrl-A shortcut in Linux
Ctrl-A shortcut in Linux
Signup and view all the flashcards
Ctrl-U shortcut in Linux
Ctrl-U shortcut in Linux
Signup and view all the flashcards
Piping in Linux
Piping in Linux
Signup and view all the flashcards
grep command
grep command
Signup and view all the flashcards
File archiving (e.g., using tar)
File archiving (e.g., using tar)
Signup and view all the flashcards
File compression
File compression
Signup and view all the flashcards
Study Notes
- This lab uses "LibreOffice" to create lab reports
- Completed reports are submitted in PDF form
- The submission deadline is the end of the second lab session
- Answers should include commands, outputs and screenshots
- Existing text files can be used or create your own
Lab Objectives
- Practice Linux commands
- Become familiar with keyboard shortcuts and wildcards
- Learn about redirection and pipes
- Manipulate text files, including searching
- Compress files
- Produce a lab report
File/Directory Management
- The terminal starts in your home directory,
/home/username/
- This directory is assigned to your user account
- Commands you have used include
ls
,pwd
,cd
,mkdir
, andrmdir
Creating a Subdirectory and Files
- Use the commands above to create a subdirectory
lab2
, which is where files will be created and processed - Create a file named
file1.txt
using thetouch
command - Write three lines of text into
file1.txt
usingvi
and save the file - Create a subdirectory
dir1
and copyfile1.txt
into it - Rename
file1.txt
indir1
todlfile1.txt
- Create a second subdirectory
dir2
in thelab2
directory - Create two files and a subdirectory in
dir2
Copying and Removing Directories
- Copy the directory
dir2
(including its content) into the directorydir1
, you will need to explain the required option - Remove
dir2
, you will need to explain the required option
Command ls
- Display the contents of
/usr/local/
- Find the size of your
.bashrc
- The
-R
option of thels
command lists subdirectories recursively - By default, files and directories are arranged alphabetically
- Options can be used to modify the classification criterion
- Display the contents of your current directory so that most recently modified files appear first
Directory Management
- It is possible to copy a directory and all of its subdirectories in a single command line
- If you want to copy a directory
dir1
to a directorydir2
directory, and thedir2
directory already exists it will copydir1
inside ofdir2
cp file1 file2 file3 directory/
copies file1, file2, and file3 into the specified directory- The syntax of
mv
ismv file1 file2
file1
is renamed tofile2
, iffile2
already exists, it will be overwritten- If you have a directory at
~/Rapport/docs/
and a filereport.tex
at home the current directory is~/Rapport/docs/
mv report.tex docs
movesreport.tex
intodocs
- To return the file to its original location use the following
mv report.tex ../../
- To rename
dir1
todir2
use the commandmv dir1 dir2
, if thedir2
directory already exists,dir1
will be moved insidedir2
- To both move a file and change its name simultaneously use
mv file1 destination/newname
, if a file with that name already exists in the destination directory, it will be overwritten
Wild Cards
- An asterisk can represent a string of any length consisting of any characters - including the empty string
- Another character that can be used to replace any character is
?
- Examples of usage can be
ls a?.txt
andrm file?
Useful Keyboard Shortcuts
Ctrl-P
rolls back to the previous command at the Linux prompt, typing it twice rolls back by two commandsCtrl-N
moves forward if you have rolled back to many commands- Arrow will move up for previous (backward), down for next (forward)
Ctrl-A
moves to the beginning of a lineCtrl-E
moves to the end of a lineCtrl-U
erases everything from where you are in a line back to the beginningCtrl-K
erases everything from where you are to the end of the lineCtrl-L
clears the text from the current terminal
Running Commands Sequentially, Redirection, Piping
- To execute two or more commands sequentially (in a single command line) use
&&
- An example of running commands sequentially is
pwd && ls && date
Redirection
$ touch test0.txt
$ touch test1.txt
$ ls > test2.txt
$ cat test2.txt
$ ls -l > test2.txt
$ cat test2.txt
- The above commands create two empty files, list the files in the directory and save the output to
test2.txt
, then list the files with detailed information and save the output totest2.txt
(overwriting the previous content) >
is called redirection
$ ls > test2.txt
$ cat test2.txt
$ ls -l >> test2.txt
$ cat test2.txt
- The above commands list the files in the directory and save the output to
test2.txt
, then list the files with detailed information and append the output totest2.txt
- What you notice is that the files are appended to the end
$ wc -c < test2.txt
- The command counts the number of characters of the file test2.txt
- Piping allows one program to receive as input the output of another program
program1 | program2
$ man -k "directory" | grep "create"
- The above command searches the man pages for the word "directory" and then filters the results to show only the lines containing the word "create"
Basic Operations on Text Files
- Create a text file “numtext.txt” that contains at least 50 lines of text and each line is numbered. Combining several commands can solve this task
- Use the command less, print and navigate within the content of the file
- Briefly describe the options of command less with your own words
- Display the 10 first lines of the file using less
- Display the 15 last lines of the file using less
- Display line number 38 using less
- Save the 25 first lines of “numtext.txt” to another file (fist25lines.txt)
- Save the lines from 13 to 37 of “numtext.txt” to another file (lines13-37.txt)
- Count and display the number of characters in “lines13-37.txt” using
wc -c
- Count and display the number of words in “lines13-37.txt” using
wc -w
- Count and display the number of lines in “lines13-37.txt” using
wc -l
- Search for a specific string in “numtext.txt” and display the number of lines where that string appears by searching the output of the command
grep "specific string" numtext.txt | wc -l
Searching Files, grep Command
- The
grep
command searches for a pattern in a file and its main options can be found using man grep - The grep options that allow you to obtain context lines (which precede and/or follow the line where the searched word appears) are
-C
,-A
, and-B
- How to display the number of the line where the searched word appears? What happens when we also ask for context lines, can be achieved with
grep -n
- The
-c
option displays the number of occurrences of the searched word - The
-i
option makes grep ignore character case (difference between upper and lower case) in its search - The
-l
option displays the file names where the word appears, but not the lines - Display lines where the search word does not appear using the
-v
option
More Grep
- The
-L
option displays the names of files that do not contain the search word - To make grep only search lines where the word appears as is (and not its variants), use the
-w
options - To search for several words at once by displaying the line numbers, use the
-e
option with-n
ie.grep -n -e "word1" -e "word2"
- Find all lines starting with “a” or “A” with
grep "^[aA]"
- Find all lines ending with “rs” with
grep "rs$"
- Find all lines containing at least one digit with
grep "[0-9]"
- Find all lines starting with a capital letter with
grep "^[[:upper:]]"
- Search for all lines starting with “B”, “E” or “Q” with
grep "^[BEQ]"
- Find all lines ending with an exclamation point using
grep "!$"
- Find all lines that do not end with a punctuation mark (period, comma, semicolon, colon, question mark, exclamation point) using
grep -v "[.,:;?!]$"
- Search for all words containing an “r” preceded by any upper or lower case letter with
grep "[a-zA-Z]r"
- Find all words with an "r" as the second letter using
grep "\br[a-zA-Z]"
File Compression
- All of the following actions must be done in command line
- Create 3 text files and add text to these files using
touch file1.txt file2.txt file3.txt
andvi
- Copy a text file whose size is greater than 10 k bytes from your system using
cp sourcefile destination
- Download 2 images using
wget URL
- Store all of these files into one directory named “myDir” under your home by creating the directory then using the
mv
command to move the files - Compute the total size of files in this directory using
du -sh myDir
- Put all the content of the directory in a single uncompressed file (“file1.tar”) and copy it to a subdirectory (“myDir2”) using
tar -cvf file1.tar myDir
thenmkdir myDir2
thencp file1.tar myDir2
- Compute the size of the “file1.tar” directory this directory will be large because it is uncompressed. Use
du -sh file1.tar
to display its size - Compress "file1.tar” and compute its size
- To compress use
gzip file1.tar
. Thedu -sh file1.tar.gz
will be smaller. The.gz
indicates that it is compressed - Decompress the resulting file by using the command
gzip -d file1.tar.gz
. Check the produced files and their size. Notice that you now have a.tar
from the.tar.gz
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.