Linux Command Line: File Management

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

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?

  • `ls -l`
  • `ls -r`
  • `ls -t` (correct)
  • `ls -u`

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?

  • `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?

<p><code>dir1</code> will be moved into <code>dir2</code> as a subdirectory. (A)</p> Signup and view all the answers

Which keyboard shortcut moves the cursor to the beginning of the current line in the terminal?

<p>Ctrl+A (D)</p> Signup and view all the answers

What does the command command1 | command2 do in Linux?

<p>Runs <code>command1</code> and uses the output of <code>command1</code> as the input for <code>command2</code>. (D)</p> Signup and view all the answers

What does the > operator do in the command ls -l > file.txt?

<p>Redirects the standard output of <code>ls -l</code> to <code>file.txt</code>, overwriting the file if it exists. (C)</p> Signup and view all the answers

What does the command grep -i "text" file.txt do?

<p>Finds all lines in <code>file.txt</code> that contain <code>text</code>, ignoring case. (C)</p> Signup and view all the answers

How can you use grep to display only the names of files that contain a certain word, without showing the matching lines?

<p><code>grep -l &quot;word&quot; ./*</code> (D)</p> Signup and view all the answers

You have created a tar archive named archive.tar of a directory named myDir. Which command would compress this archive using gzip?

<p><code>gzip archive.tar</code> (A)</p> Signup and view all the answers

Flashcards

Home directory in Linux

A special directory assigned to your user account, usually /home/username/.

Asterisk (*) in Linux wildcards

Represents a string of any length consisting of any characters, including the empty string.

Ctrl-P shortcut in Linux

Rolls back to the previous command in the Linux terminal.

Ctrl-A shortcut in Linux

Moves the cursor to the beginning of the current line in the terminal.

Signup and view all the flashcards

Ctrl-U shortcut in Linux

Erases everything from the current cursor position back to the beginning of the line.

Signup and view all the flashcards

Piping in Linux

Allows one program to receive as input the output of another program.

Signup and view all the flashcards

grep command

A command-line utility for finding lines that match a search string in one or more files.

Signup and view all the flashcards

File archiving (e.g., using tar)

Combines multiple files or directories into a single archive file for easier storage and transfer.

Signup and view all the flashcards

File compression

Reduces the size of a file, making it take up less disk space.

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, and rmdir

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 the touchcommand
  • Write three lines of text into file1.txt using vi and save the file
  • Create a subdirectory dir1 and copy file1.txt into it
  • Rename file1.txt in dir1 to dlfile1.txt
  • Create a second subdirectory dir2 in the lab2 directory
  • Create two files and a subdirectory in dir2

Copying and Removing Directories

  • Copy the directory dir2 (including its content) into the directory dir1, 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 the ls 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 directory dir2 directory, and the dir2 directory already exists it will copy dir1 inside of dir2
  • cp file1 file2 file3 directory/ copies file1, file2, and file3 into the specified directory
  • The syntax of mv is mv file1 file2
  • file1 is renamed to file2, if file2 already exists, it will be overwritten
  • If you have a directory at ~/Rapport/docs/ and a file report.tex at home the current directory is ~/Rapport/docs/
  • mv report.tex docs moves report.tex into docs
  • To return the file to its original location use the following mv report.tex ../../
  • To rename dir1 to dir2 use the command mv dir1 dir2, if the dir2 directory already exists, dir1 will be moved inside dir2
  • 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 and rm file?

Useful Keyboard Shortcuts

  • Ctrl-P rolls back to the previous command at the Linux prompt, typing it twice rolls back by two commands
  • Ctrl-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 line
  • Ctrl-E moves to the end of a line
  • Ctrl-U erases everything from where you are in a line back to the beginning
  • Ctrl-K erases everything from where you are to the end of the line
  • Ctrl-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 to test2.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 to test2.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 and vi
  • 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 then mkdir myDir2 then cp 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. The du -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.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser