Create files from redirecting content, edit, inspect and modify content of a file.
Make a file
$ touch myfile.txt
Edit a file
$ nano myfile.txt
Generate project structure for use in a readme
# append
$ tree >> out.txt
# new file
$ tree > out.txt
Another common pattern is generating a requirements file for a Python project
$ pip freeze > requirements.txt
You can also put the redirection at the start
$ > out.txt tree
Make a file with a single line tdd
$ echo tdd > newfile.txt
Using the states file as example:
# print all file contents
$ cat states.txt
# print first n rows
$ head -n 3 states.txt
# print last n rows
$ tail -n 3 states.txt
# paging over the file
$ less states.txt
# word count: number of lines, number of words, number of characters of file
$ wc states.txt
grep
when using wildcards:
$ grep "x" states.txt
egrep
when using regular expressions:
$ egrep "s*as" states.txt
Let's create a file to run some more examples:
$ touch small.txt
$ echo "abcdefghijklmnopqrstuvwxyz" >> small.txt
$ echo "ABCDEFGHIJKLMNOPQRSTUVWXYZ" >> small.txt
$ echo "0123456789" >> small.txt
$ echo "aa bb cc" >> small.txt
$ echo "rhythms" >> small.txt
$ echo "xyz" >> small.txt
$ echo "abc" >> small.txt
$ echo "tragedy + time = humor" >> small.txt
$ echo "http://www.jhsph.edu/" >> small.txt
$ echo "#%&-=***=-&%#" >> small.txt
Using our created small.txt
file as example:
# list all “word” characters
$ egrep "\w" small.txt
# list all “number” characters
$ egrep "\d" small.txt
# invert match
$ egrep -v "\w" small.txt