Skip to content

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:

bash
mkdir ~/practice
cd ~/practice

Make folders and files ​

CommandExampleWhat it does
mkdirmkdir notesMakes a folder
mkdir -pmkdir -p projects/website/cssMakes a folder and any missing folders above it
touchtouch todo.txtMakes an empty file, or updates the date of an existing one
echo … >echo "Buy milk" > shopping.txtWrites 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.

text
sam@laptop:~/practice$ mkdir -p projects/website/css
sam@laptop:~/practice$ echo "Buy milk" > shopping.txt
sam@laptop:~/practice$ ls
projects  shopping.txt

Copy, move and rename ​

CommandExampleWhat it does
cpcp shopping.txt backup.txtCopies a file
mvmv backup.txt projects/Moves a file into a folder
mvmv shopping.txt list.txtRenames a file. Moving and renaming are the same thing
cp -rcp -r projects projects-copyCopies 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):

text
sam@laptop:~/practice$ cp projects projects-copy
cp: -r not specified; omitting directory 'projects'
sam@laptop:~/practice$ cp -r projects projects-copy

WARNING

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 ​

CommandExampleWhat it does
rmrm list.txtDeletes a file
rm -irm -i list.txtAsks before each deletion
rmdirrmdir notesDeletes a folder, only if it is empty
rm -rrm -r projects-copyDeletes a folder and everything inside it

rm does not delete a folder on its own:

text
sam@laptop:~/practice$ rm projects-copy
rm: cannot remove 'projects-copy': Is a directory
sam@laptop:~/practice$ rm -r projects-copy

There 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:

text
rm -r ~/old-build      deletes the folder old-build in your home folder
rm -r ~ /old-build     one stray space: deletes your whole home folder

Look inside files ​

CommandWhat it showsGood for
cat fileThe whole file at onceShort files
less fileOne screen at a time. Space pages, /word searches, q quitsLong files
head -n 3 fileThe first 3 lines (10 if you leave out -n)Seeing what a file looks like
tail -n 3 fileThe last 3 linesThe newest lines of a log

seq 12 prints the numbers 1 to 12, one per line, which makes a handy test file:

text
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
12

Use 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.

PatternMatchesExample
*Any characters, any number of them*.txt matches every name ending in .txt
?Exactly one characterphoto?.jpg matches photo1.jpg, not photo10.jpg
[abc]One of the characters listedreport-[jf]* matches report-jan and report-feb

Make a few empty files to try them on (touch accepts several names at once):

text
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.jpg

Because 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:

bash
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:

text
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:

text
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.log

Each 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.
  • ls before rm. Run the exact path or pattern with ls first. If the list is what you expect, press ↑, change ls to rm, and run it.
  • Avoid rm -rf with a variable or a wildcard that you have not checked. An empty variable can turn rm -rf "$DIR/" into rm -rf /.
  • Keep a backup. Since rm cannot be undone, the only way back from a mistake is a copy somewhere else.
  • cp and mv overwrite silently. Use -i when 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 ​

QuizWorking with files

Question 1 of 6

Which command copies the folder projects with everything inside it?

Score: 0

What you learned ​

  • mkdir, touch, cp, mv and rm create, copy, move and delete. Source first, destination last.
  • Folders need -r to copy or delete. rm has no recycle bin and no undo.
  • cat, less, head and tail show what is inside a file.
  • *, ? and […] are wildcards the shell expands before the command runs.
  • find searches by name; grep -r searches by content.

Next steps ​