Files and command syntax
Goal
Create, move, inspect, and search files. Explain each part of a command.
Understand the command
In grep -in 'four' example.txt, grep is the program, -in supplies options, and the remaining arguments specify the pattern and file.
Options are case-sensitive. -i ignores letter case and -n prints line numbers. Without a file argument, grep can wait for text from standard input.
An absolute path begins at /. A relative path begins at your current directory. ~ refers to your home directory. Use pwd when unsure where you are.
> creates or truncates a file. >> appends. The shell handles these redirections, not echo.
Worked example
Use a new practice directory. If it already exists, choose another name to avoid overwriting earlier work.
mkdir ~/interview-practice
cd ~/interview-practice
echo 'This is line one' > example.txt
echo 'This is line four' >> example.txt
cat example.txt
grep -in 'four' example.txt
mkdir archive
mv example.txt archive/
cat archive/example.txt
The search should print 2:This is line four. Its line number is 2 because this file contains two lines.
mv moves or renames a file. cp copies it. Both can overwrite an existing destination, so inspect paths first.
Independent exercise
Create a new folder and a three-line file. Move the file into another folder. Append a fourth line. Search for a word and show its line number.
Run ls -lah and explain the file size, owner, and permissions columns. Use ls --help when needed.
Explain aloud
- How do relative paths change after
cd? - What happens if you use
>instead of>>? - Why can grep appear to wait when you omit the filename?
Completion check
Repeat with different names without command hints. Show that all four lines remain after moving and appending.