Skip to content
KALANBLON
Show contents

Chapter 2 of 7 · 20 min

Copy, move, delete, find

By the end of this chapter, you'll be able to

  • Copy, move, rename and delete files and folders
  • Refer to several files at once with the * and ? wildcards
  • Display the start or end of a file and count its lines
  • Find files by name with find

Copy, move, rename

Let's prepare a few files. printf writes text like echo, but understands \n (a line break): handy for creating a multi-line file in one go.

mkdir -p photos archives
touch photos/beach.jpg photos/market.jpg photos/party.mp4
printf "bread\nmilk\nmangoes\nonions\nrice\n" > shopping.txt
cp shopping.txt archives/
mv photos/party.mp4 archives/
ls -1 photos archives
archives:
party.mp4
shopping.txt

photos:
beach.jpg
market.jpg
  • cp source destination copies: the original stays in place. If the destination is a folder, the copy keeps its name there;
  • mv source destination moves; it is also used to rename: mv shopping.txt list.txt;
  • to copy a whole folder, you need cp -r (recursive: the folder and everything in it).

Wildcards

The shell replaces some characters with the list of matching names, before running the command:

  • * matches any sequence of characters (even an empty one);
  • ? matches exactly one character.
touch report1.txt report2.txt report10.txt
echo report?.txt
echo *.txt
report1.txt report2.txt
report1.txt report10.txt report2.txt shopping.txt

echo is a good way to check what a wildcard matches before using it with a command that changes something. Note the order: report10 comes before report2, because names are sorted character by character, and 1 comes before 2.

A single command, however many photos there are.

Step 5 of 5
Text description of the animation
  1. You write a pattern: all names ending in .jpg.
  2. The shell scans the current folder and keeps the matching names.
  3. It replaces the pattern with the sorted list. cp never sees the star.
  4. cp copies each file into the folder, the last argument.
  5. A single command, however many photos there are.

Looking inside a file

A log file can have millions of lines: no point displaying everything. head shows the start, tail the end (10 lines by default, -n to choose), and wc -l counts the lines:

head -n 2 shopping.txt
tail -n 1 shopping.txt
wc -l shopping.txt
bread
milk
rice
5 shopping.txt

Deleting and finding

rm deletes files; rm -r deletes a folder and all its contents. rmdir only deletes an empty folder. To find files by name, anywhere under a folder, use find:

find . -name "*.jpg" | sort
./photos/beach.jpg
./photos/market.jpg

The quotes around "*.jpg" matter: without them, the shell would expand the wildcard before running find, in the current folder only. We add | sort because find lists files in the order the system stores them, which can vary from one machine to another; you will see | in detail in the next chapter.

Your turn

Mini-lab: tidying up a messy folderTerminal simulator

Terminal

Click “Run”: the commands run in a fresh terminal, in your home folder /home/awa. Type “help” for the list of commands.

Teaching simulator: it reproduces the essential Linux commands in your browser, with nothing to install · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Worked exercise

Worked exercise: the backup before breaking everything

In a site folder, there are index.html, contact.html and style.css. Before changing them, create a backup folder containing a copy of the two HTML files only, then rename style.css to theme.css. Finish by listing the files in backup, one per line.

mkdir site
touch site/index.html site/contact.html site/style.css
Hint 1
A single cp with a wildcard is enough for both HTML files.
Hint 2
To rename, you “move” to a new name, in the same folder.
Show the solution and common mistakes
mkdir backup
cp site/*.html backup/
mv site/style.css site/theme.css
ls -1 backup
contact.html
index.html

Common mistakes:

  • Writing mv site/style.css theme.css: the file is renamed and moved into the current folder, out of site.
  • Copying with cp site/* backup/: the wildcard also picks up style.css, which does not match the instructions.

Summary

  1. 1cp copies, mv moves or renames, cp -r copies a whole folder.
  2. 2The * and ? wildcards are expanded by the shell before the command: check them with echo.
  3. 3head, tail and wc -l let you explore a file without displaying it all.
  4. 4find . -name "pattern" finds files across a whole tree.
  5. 5rm deletes permanently, with no recycle bin: always reread the command before pressing Enter.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.