appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

Working with files and folders

Lesson 2 of 510 minBeginner

Create, copy, move, delete and read files, and find what you need.

Create

mkdir projects            # new folder
mkdir -p a/b/c            # create nested folders in one go
touch notes.txt           # empty file (or update its timestamp)

Copy, move and rename

cp a.txt b.txt            # copy a file
cp -r folder/ backup/     # copy a folder and its contents
mv a.txt docs/            # move
mv old.txt new.txt        # rename (a move to a new name)

Delete

rm file.txt               # delete a file
rm -r folder/             # delete a folder and everything in it

CarefulThere is no recycle bin. rm deletes immediately and for good. Double-check the path, never run rm -r with a path you did not read, and be extremely careful with wildcards like *.

Read files

cat notes.txt             # print the whole file
less app.log              # scroll through a long file (q to quit, / to search)
head -n 5 app.log         # first 5 lines
tail -n 20 app.log        # last 20 lines
tail -f app.log           # follow new lines as they are written (Ctrl+C to stop)

Wildcards and finding things

ls *.txt                          # every file ending in .txt
cp *.jpg photos/                  # copy all JPEGs
find /var/log -name "*.log"       # search by name
find . -type f -mtime -1          # files changed in the last day

The shell expands * before the command runs, so ls *.txt really runs ls with every matching name. That is why a stray * in an rm command is dangerous.

Test yourself

Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.

HintThe command is "make directory".
Hintcp source destination
Hinttail shows the end of a file.
4. What does rm do to a file?

Key terms