Intro to Linux Command-Line: View Find and Search Files

Introduction: Introduce how to work with, view, find and search the content of text files. The workshop is aimed at beginners with basic command-line experience of the Linux file system and will focus on hands-on exercises.

Course Goals

  • View the content of text-based files.

  • Search a file for a string. 

  • Search for a file/folder by name.

  • Redirect output from commands and pipe commands together. 

  • Be very exercise based to allow practice of commands and concepts.



01 View/Search a File

01.01 Setting Up

Question:

  • There is a folder called intro_to_linux within the /project/arccatrain/ folder.

  • How would you copy this folder into your home folder?


01.02 Setting Up: Answer(s)

Answer: There are a number of ways…

# Move to your home folder and copy int this location. # The “.” marks the current working directory. [arccanetrain]$ cd [~]$ cp -r /project/arccanetrain/intro_to_linux/ . cp: cannot open 'intro_to_linux/workshop_me.txt' for reading: Permission denied [~]$ ls Desktop Documents Downloads intro_to_linux # Move into the /project/arccatrain/ folder and copy from there into your home. # The “~” is short for your home folder. [~]$ cd /project/arccanetrain/ [arccanetrain]$ cp -r intro_to_linux/ ~ cp: cannot open 'intro_to_linux/workshop_me.txt' for reading: Permission denied [arccanetrain]$ ls ~ Desktop Documents Downloads intro_to_linux # Why do we see the cp related permission denied? # -rw------- 1 arcc-t05 arccanetrain 23 Oct 5 07:20 workshop_me.txt # What happens to this file? It does not get copied.

01.03a View the content of files.

Command

Description

cat

Usage: cat [OPTION]... [FILE]... Concatenate FILE(s) to standard output.  -n, --number               number all output lines

more

more [options] <file>... A file perusal filter for CRT viewing. more  is a filter for paging through text one screenful at a time.

head


01.03b View the content of files.

Command

Description

tail


01.04 Exercises


01.05 Search for a string within a text file (grep) 

Command

Description

grep


01.06 Examples: Search a file:


01.07 Examples: Search folders and files:


01.08 Exercises

Questions:

  1. Which named applications are related to the words “bayes”?

  2. Which files contain reference to IPA?


01.09 Answers


02 Search for a File


02.01a Searching for Files: find

Let us look at a folder with many subfolders and files.


02.01b Searching for Files: find


02.02 Searching for Files: find

Command

Description

find


02.03 Examples


02.04 Examples


02.05 Exercises

Questions:

  1. What do we notice about some of the find command options?

  2. Find any files that contain the string “hello”, regardless of case, within their filename.

  3. Find any folders or files that contain the string “feb” regardless of case.

    1. Can you list only the folders?

  4. Find any files that have the postfix “tx” – must be lowercase.


02.06 Answers

1: What do we notice about some of the find command options?

  • That some of the single dash options (-name) are similar to long-names and not single letters.

2: Find any files that contain the string “hello”, regardless of case, within their filename.


02.07 Answers

3: Find any folders or files that contain the string “feb” regardless of case.

  • Can you list only the folders?


02.08a Answers

4: Find any files that have the postfix “tx” – must be lowercase.


02.08a Answers


03 Output Redirection and Pipes


03 Output Redirection and Pipes

  • Redirection of output: > vs >>

    • redirect sends a channel of output to a file.

    • You can redirect a file as input to a command using < and << (not looked at).

  • Using pipe “|’

    • A pipe passes standard output as the standard input to another command

  • Examples of the form: 

    • View a text file and pipe to grep.

    • Cat a list and sort by line.

    • Sort and then find unique items.

    • View folder contents and look for a specifically named name.


03.01 Redirection of output: > vs >>


03.02 Example: Using pipe “|” from a file.


03.03 Example continued:


03.04 Example continued:


03.05 Example continued:


03.06 Example continued 


03.07 Example: Pipe from ls command


03.08 Exercises

  1. How does the wc command work? What are its options?

  2. How does the sort command work? What are its options?

  3. How does the uniq command work? What are its options?

  4. How many unique varieties of beans are there in the vegetables.txt file?


03.09 Answers

4: How many unique varieties of beans ae there in the vegetables.txt file?

  • How do you deal with “soy beans” vs “Soy Beans”?

  • What options does the uniq command provide?


04 More Intermediate Features, Next Steps, Suggestions and Summary


04.01a More Intermediate Features

  • Environment Variables: Define the behavior of the environment: Try:

    • echo $HOME

    • echo $USER

    • echo $SHELL

    • echo $PATH

  • File searching/manipulation

    • sed: stream editor for filtering and transforming text

    • gawk: pattern scanning and processing language

  • Ability to update file permission and ownership: chmod/chown

    • User-case of sharing files/folders.


04.01b More Intermediate Features

  • Aliases in .bashrc.

    • Create short-cuts of popular/frequently used commands.

  • Text editors: vi/vim/nano

    • vimtutor

    • touch

  • Remote access with ssh.


04.02 Next Steps, Suggestions


04.03 Further Trainings: UWYO LinkedIn

  • Introduction to Linux

  • Learning Linux Command Line

  • Linux: Files and Permissions

  • Linux: Over and Installation

  • Learning Linux Shell Scripting


04.04 Request an Account with ARCC

image-20240522-200611.png

04.05 Summary

In this workshop we have:

  • How to search for a string within a file.

  • How to find a file.

  • How to redirect the output of a command into a file.

  • How to use pipes to direct the output of one command as the input into another command.