3 Working with files
3.1 Touching (Creating an empty file)
The command touch creates an empty file and also updates the timestamp of an existing file without changing its contents:
cd ~/training
touch notes.txt
lsEmpty files might seem useless, but touch is handy for quickly creating placeholder files, or for testing commands before applying them to real data.
3.2 Copying (Making a duplicate)
The command cp (copy) duplicates a file. The syntax is:
cp source destinationFor example:
cp notes.txt notes_backup.txt
lsTo copy a whole directory and everything inside it, add the -r flag (recursive):
cp -r data data_backup3.3 Moving (Relocating or renaming)
The command mv (move) moves a file to a new location and also serves as the way to rename files in Unix, since there is no separate rename command:
# rename a file
mv notes.txt readme.txt
# move it into a subdirectory
mv readme.txt data/readme.txtmv will silently overwrite the destination if a file with that name already exists there. Always double-check before moving.
3.4 Removing (Deleting permanently)
The command rm (remove) deletes files. Unlike moving something to the Trash, there is no undo:
rm notes_backup.txtTo remove a directory and all its contents:
rm -r data_backupBe very careful with rm -r. A common safety habit is to use rm -ri instead, which asks for confirmation before deleting each item:
rm -ri data_backup3.5 Concatenating (Printing a whole file)
The command cat (catenate) prints the entire contents of a file to the terminal:
cat data/readme.txtcat is fast and simple, but prints everything at once not ideal for large files.
3.6 Head and tail (Peeking at the beginning or end)
head shows the first 10 lines of a file by default:
head data/readme.txtUse -n to choose a different number of lines:
head -n 5 data/readme.txttail shows the last 10 lines:
tail data/readme.txtThese are especially useful for large biological data files you can check the format without loading the whole thing.
3.7 Less (Scrolling through a large file)
For files too large to comfortably cat, less opens them in an interactive scrollable viewer:
less data/readme.txt| Key | Action |
|---|---|
Space |
Page down |
b |
Page up |
↓ / ↑ |
One line at a time |
/pattern |
Search for pattern |
n |
Next search result |
q |
Quit |
You will use less constantly when working with FASTQ, FASTA, and other large genomics files. It never loads the whole file into memory, so it works even on files that are gigabytes in size.