Introduction To Unix-And-Linux-Redirecting-Output-And-Finding-Files
Introduction To Unix-And-Linux-Redirecting-Output-And-Finding-Files
Type:
You should see Hello World! printed out under where you typed the
command since printing to the terminal is the standard output.
The above sends the output of the echo command to myfile.txt rather than
the console.
Type in the following two commands to see your file and list
it out:
ls
cat myfile.txt
Concatenating files
On the last page we learned that the > character redirects the stdout to a
file. It will create the file if it does not exist and overwrite it if it does.
We’ll create another file with some text. We can still use the >> operator
even though the file doesn’t exist; it will create the file.
The file anotherfile.txt is unchanged - you can list it out and check:
cat anotherfile.txt
Finding files
The find command looks for a file or directory based on the parameters
you set.
First take a look at the man pages for the find command:
man find
Type the following commands to get a sense of the versatility of the find
command.
Solution
Find files in the home directory that are greater in size than 10 bytes.
Solution
Generally, you won’t look for files based on bytes. You may specify k for
kilobytes, M for megabytes next to the size (e.g. find ~/ -size +10k).
If you are looking for a range, you can specify both a minimum and
maximum file size. For example, to look for files between 10 and 50
bytes you would use find ~/ -size +10 -size -50
Find files or directories in the code directory in your workspace that
have been modified in the last year
Solution
First take a look at the man pages for the stat command:
man stat
stat code/src/average.c
challenge
stat -f code/src/average.c
The -f parameter
When you use the -f parameter you get system information rather
than file information
man file
file code/src/average.c
challenge
Try:
file code/bin/average
This file is an executable, you can see which system it is built for and
information about the linker among other things.
Finding file content
The grep utility searches a file for a pattern or string and outputs all lines
that match to stdout.
First take a look at the man pages for grep to get a sense on
how it would be used:
man grep
The general syntax is pretty simple (grep PATTERN) but take a look at some
of the helpful OPTIONS under Matching Control (e.g. -w and -i).
Solution
Solution
Output the file names of files that contain concat, search in code/docs.
Solution