Working with files
By the end of this lesson you can create, copy, move, rename and delete files and folders, read them without opening an editor, select many at once with wildcards, and find a file by its name or by a word inside it.
You already know how to move around with cd and look with ls from Shell basics. Make a practice folder now and work inside it, so nothing you try touches real files:
mkdir ~/practice
cd ~/practiceMake folders and files
| Command | Example | What it does |
|---|---|---|
mkdir | mkdir notes | Makes a folder |
mkdir -p | mkdir -p projects/website/css | Makes a folder and any missing folders above it |
touch | touch todo.txt | Makes an empty file, or updates the date of an existing one |
echo … > | echo "Buy milk" > shopping.txt | Writes a line of text into a new file |
The > sends a command's output into a file instead of the screen. Lesson 5 covers it properly; for now it is a quick way to make a file with something in it.
sam@laptop:~/practice$ mkdir -p projects/website/css
sam@laptop:~/practice$ echo "Buy milk" > shopping.txt
sam@laptop:~/practice$ ls
projects shopping.txtCopy, move and rename
| Command | Example | What it does |
|---|---|---|
cp | cp shopping.txt backup.txt | Copies a file |
mv | mv backup.txt projects/ | Moves a file into a folder |
mv | mv shopping.txt list.txt | Renames a file. Moving and renaming are the same thing |
cp -r | cp -r projects projects-copy | Copies a folder and everything in it |
The pattern is always source first, destination last. If the destination is an existing folder, the file goes inside it with the same name.
Run the first three examples in order. You end with list.txt in the practice folder and backup.txt inside projects.
cp refuses to copy a folder unless you say you mean the whole tree with -r (recursive):
sam@laptop:~/practice$ cp projects projects-copy
cp: -r not specified; omitting directory 'projects'
sam@laptop:~/practice$ cp -r projects projects-copyWARNING
cp and mv overwrite a file of the same name at the destination without asking. Add -i (interactive), as in cp -i a.txt b.txt, to be asked first.
Delete: rm is for ever
| Command | Example | What it does |
|---|---|---|
rm | rm list.txt | Deletes a file |
rm -i | rm -i list.txt | Asks before each deletion |
rmdir | rmdir notes | Deletes a folder, only if it is empty |
rm -r | rm -r projects-copy | Deletes a folder and everything inside it |
rm does not delete a folder on its own:
sam@laptop:~/practice$ rm projects-copy
rm: cannot remove 'projects-copy': Is a directory
sam@laptop:~/practice$ rm -r projects-copyThere is no recycle bin on the command line. The file manager on your desktop moves deleted files to the Trash, but rm removes them at once. There is no undo, and no "are you sure?" unless you add -i. You will often see rm -rf: the -f (force) also silences every warning and error, which makes a mistake even quieter.
One character can make all the difference:
rm -r ~/old-build deletes the folder old-build in your home folder
rm -r ~ /old-build one stray space: deletes your whole home folderLook inside files
| Command | What it shows | Good for |
|---|---|---|
cat file | The whole file at once | Short files |
less file | One screen at a time. Space pages, /word searches, q quits | Long files |
head -n 3 file | The first 3 lines (10 if you leave out -n) | Seeing what a file looks like |
tail -n 3 file | The last 3 lines | The newest lines of a log |
seq 12 prints the numbers 1 to 12, one per line, which makes a handy test file:
sam@laptop:~/practice$ seq 12 > numbers.txt
sam@laptop:~/practice$ head -n 3 numbers.txt
1
2
3
sam@laptop:~/practice$ tail -n 2 numbers.txt
11
12Use cat and less on text files only. A program or a picture prints as garbage and can scramble the terminal; if that happens, type reset and press Enter.
Wildcards: many files at once
A wildcard is a pattern the shell turns into a list of matching names before the command runs.
| Pattern | Matches | Example |
|---|---|---|
* | Any characters, any number of them | *.txt matches every name ending in .txt |
? | Exactly one character | photo?.jpg matches photo1.jpg, not photo10.jpg |
[abc] | One of the characters listed | report-[jf]* matches report-jan and report-feb |
Make a few empty files to try them on (touch accepts several names at once):
sam@laptop:~/practice$ touch photo1.jpg photo2.jpg photo10.jpg report-jan.txt report-feb.txt report-mar.csv
sam@laptop:~/practice$ ls *.txt
list.txt numbers.txt report-feb.txt report-jan.txt
sam@laptop:~/practice$ ls report-*
report-feb.txt report-jan.txt report-mar.csv
sam@laptop:~/practice$ ls photo?.jpg
photo1.jpg photo2.jpgBecause the shell expands the pattern, the command never sees *; ls *.txt really runs ls list.txt numbers.txt report-feb.txt report-jan.txt.
DANGER
Before you delete with a wildcard, run the same pattern with ls first and read the list. rm *.txt and rm * .txt differ by one space, and the second deletes every file in the folder.
Find files
Two questions come up all the time: "where is the file called…?" and "which file mentions…?".
First, give the practice folder a small log file to find:
echo "ERROR disk full" > projects/app.log
echo "error lowercase" >> projects/app.log>> adds a line to the end of the file instead of replacing it.
By name, with find. Give it where to start and what to match:
sam@laptop:~/practice$ find . -name "*.log"
./projects/app.log
sam@laptop:~/practice$ find ~ -name "cv.pdf"
/home/sam/Documents/cv.pdf. means "start here". Quote the pattern, so that find sees the * and not the shell. Useful extras: -type d finds only folders, -type f only files, and -iname ignores upper and lower case.
By content, with grep -r. grep prints lines that contain a word; -r (recursive) searches every file in a folder and below:
sam@laptop:~/practice$ grep -r "ERROR" projects
projects/app.log:ERROR disk full
sam@laptop:~/practice$ grep -ri "error" projects
projects/app.log:ERROR disk full
projects/app.log:error lowercase
sam@laptop:~/practice$ grep -rl "error" projects
projects/app.logEach line starts with the file name. -i ignores case, -l lists only the names of matching files, and -n adds line numbers. Lesson 5 does much more with grep.
Stay safe
- Practise in a throwaway folder, as in this lesson, never in your home folder or a project you care about.
lsbeforerm. Run the exact path or pattern withlsfirst. If the list is what you expect, press ↑, changelstorm, and run it.- Avoid
rm -rfwith a variable or a wildcard that you have not checked. An empty variable can turnrm -rf "$DIR/"intorm -rf /. - Keep a backup. Since
rmcannot be undone, the only way back from a mistake is a copy somewhere else. cpandmvoverwrite silently. Use-iwhen the destination might already exist.
Try it in a Dhiva Labs app
Ubuntu Terminal on Windows runs these same commands, including find, grep, head, tail and less. It has the same warning: rm does not use the Windows Recycle Bin. SecureShell Go adds a file browser for a remote server, where deleting a file is also permanent.
Check yourself
Question 1 of 6
Which command copies the folder projects with everything inside it?
What you learned
mkdir,touch,cp,mvandrmcreate, copy, move and delete. Source first, destination last.- Folders need
-rto copy or delete.rmhas no recycle bin and no undo. cat,less,headandtailshow what is inside a file.*,?and[…]are wildcards the shell expands before the command runs.findsearches by name;grep -rsearches by content.