Working With Files
touch
Create a new file or change the modified date of an existing file→.
- think - record the last time you’ve touched the file.
- takes one argument, the path of the file name that you’re modifying/creating
- if the file already exists touch changes the modified date →
- if the file does not exist it creates it →
cp
Copy file to a path→
- think: copy
- requires two arguments - pathnames for both source and destination
- can take a flag, -r, to copy recursively
cp filename1.txt filename2.txt
# use -r to copy recursively (for example, all nested directories and files)
cp -r dir1 dir2
mv
Move file to a different location, or rename. →
- think: move.
- requires two arguments - pathnames for both source and destination
# renaming
mv filename1.txt filename2.txt
# moving
mv filename.txt dir
less
Show the contents of a file with pagination. →
- requires a single argument, the path to the file
less filename1.txt
# use <SPACE> to page down
# use <b> to page up
# use <UP>,<DOWN> to navigate by line
# use <q> to quit
# use </> to search
more
Show the contents of a file with pagination. →
- requires a single argument, the path to the file
cat
Show all of the contents of a file →
- requires a single argument, the path to the file
head
Show the contents of the start of a file. →
- think head is the beginning / top of the file
- requires a single argument, the path to the file
tail
Show the contents of the end of a file. →
- think tail is the end of the file
- requires a single argument, the path to the file
- can be used with flags: -f (realtime) and -n (how many lines should be displayed?)
tail filename1.txt
# show changes realtime!
tail -f filename.txt
# display last 5 lines
tail -n5 filename.txt
rm
Remove a file. →
- think: remove
- requires a single argument, the path to the file
- has an optional flag, -r, for removing recursively
- has an optional flag, -f, for skipping confirmation
rm filename1.txt
# also... to remove everything recursively (and ignore
# confirmation)... use this (be careful with it!)
rm -rf filename1.txt
Wildcard Matching
- you can work on a set of files by using wildcard matching
- the * (asterisk) represents anything
- so, *.gif represents all files with a .gif extension
- you can use wildcard matching with some of the commands that we’ve seen
# copy all gifs into the archive folder
cp *.gif archive
# move all gifs into the archive folder
mv *.gif archive
wc
Count the number of lines, words or bytes in a file. →
- think: word count
- requires a single argument, the path to the file
- can take flags (e.g. -l, -w, -c)
- displays lines, words, bytes
# count the number of lines, words, and bytes in a file
wc filename.txt
# count the number of lines in a file
wc -l filename.txt
Activity: Drills!
Entering commands flash cards x 3 (use set 3)
We’ll do this together, then try downloading it yourself:
- Download drills.py to the home directory
- Type python drills.py
- When prompted for a number, enter 3
- CTRL-C quits
Lab
Working With Files
- Type each command (with arguments and flags) exactly
- Only press <ENTER> when instructed…
- <TAB>, <ENTER>, <UP>, <DOWN> are the actual keys!
- Paste answer or output below the dashed line (————)
Activity: Drills!
Entering commands flash cards x 3 (use set 123)
We’ll do this together, then try downloading it yourself:
- Download drills.py to the home directory
- Type python drills.py
- When prompted for a number, enter 123
- CTRL-C quits