Linux & the Command Line

Creating, Copying, Moving and Deleting

30 دقيقة

Creating

mkdir notes                    # one folder
mkdir -p notes/2026/october    # nested folders, no error if they exist
touch todo.txt                 # empty file (or update its timestamp)
echo "buy milk" > todo.txt     # create or OVERWRITE with text
echo "call bank" >> todo.txt   # APPEND a line

> replaces the whole file. >> adds to the end. Mixing these up is a classic way to lose a file's contents.

Copying and moving

cp todo.txt todo.bak           # copy a file
cp -r notes notes-backup       # copy a folder (-r = recursive)
mv todo.bak archive/           # move into a folder
mv draft.txt final.txt         # rename (a move in the same folder)
cp -i a.txt b.txt              # ask before overwriting

cp and mv silently overwrite the destination by default. Add -i (interactive) when you are not sure, or -n to never overwrite.

Deleting: there is no recycle bin

rm old.txt                     # delete a file
rm -r old-folder               # delete a folder and everything in it
rmdir empty-folder             # delete only if empty (safe)
rm -i *.log                    # confirm each one

rm is permanent. Before running rm -r or anything with a wildcard, run ls with the exact same pattern first and read the list:

ls ~/tmp/*.log      # check what matches
rm ~/tmp/*.log      # then delete

Never run rm -rf / or rm -rf ~ style commands copied from the internet, and be very careful with variables: rm -rf "$DIR/" deletes / if $DIR is empty. Scripts should use set -u (covered in Automation & Scripting) to stop on empty variables.

Wildcards (globs)

Pattern Matches
* anything, including nothing
? exactly one character
[abc] one of a, b or c
*.{jpg,png} brace expansion: *.jpg and *.png
ls *.txt
cp report-202?.pdf archive/
mkdir -p project/{src,tests,docs}

Links

ln -s /var/www/app/current app   # a shortcut (symbolic link) called app
ls -l app                        # app -> /var/www/app/current

Deployment tools use symlinks so that switching versions is one atomic change of the link.

Practice

Complete these missions in the Practice Terminal: Make folders, Write a file, Copy and move and Clean up.

Check yourself

  • What is the difference between > and >>?
  • How do you rename a.txt to b.txt?
  • What should you always do before rm -r with a wildcard?

يُحفظ تقدّمك في هذا المتصفح فقط.

إرسال للمراجعة (10 نقطة)