Example 02: The Shell
We start in the home directory for this seminar’s website:
pwd
ls
Make a temporary directory:
mkdir tmp
touch
and cat
Use touch
to create an empty file:
touch tmp/my-file.txt
List it:
ls -l tmp
Echo some text into some files:
echo Hello > tmp/01-my-file.txt
echo There > tmp/02-my-file.txt
ls -l tmp
Cat the files
cat tmp/01-my-file.txt tmp/02-my-file.txt
Cat the files into a new file:
cat tmp/01-my-file.txt tmp/02-my-file.txt > tmp/03-my-file.txt
cat tmp/03-my-file.txt
Append to a file with >>
instead of >
:
cat tmp/01-my-file.txt tmp/02-my-file.txt >> tmp/03-my-file.txt
Result:
cat tmp/03-my-file.txt
Brace expansion
You can do this:
#| class: scrollout
touch tmp/{a..z}.txt
ls tmp/
Or this:
#| class: scrollout
touch tmp/{A..Z}{0..10}.txt
ls tmp/
A loop
You can write loops in the shell as well:
for f in {a..z}{0..10}
do
echo hello > "tmp/$f.txt"
done
#| class: scrollout
ls tmp/
cat tmp/u3.txt
Using cut
We can cut files or STDIN
. Here we get a long listing of the files in a directory, cut the output using whitespace as the field delimiter (-w
), select fields 6 and 7 (-f6-7
) which are the modification dates of the file and then get the unique values with a count of their frequency.
ls -lt assets/01-file-system/ | cut -w -f6-7 | uniq -c
Clean up
rm
command is dangerous!
The rm
command deletes the named files. rm -f
forces the deletion without any further confirmation. The -r
switch deletes files and directories recursively starting from the top level directory provided and descending into every directory below. The combination of the -r
and -f
switches makes it easy to accidentally delete way too much.
## Be VERY CAREFUL with the syntax of this command
rm -rf tmp/