SkillSprout
Chapter 1 · Lesson 3
Beginner
1 min read

Create, Read, Copy and Move Files

Overview

Linux file commands separate creating directories, reading data, copying entries and removing entries. The key safety rule is to confirm the exact source and destination before an operation.

Detailed Notes

Create directories and files

bash
mkdir -p ~/linux-practice/files
touch ~/linux-practice/files/notes.txt
ls -l ~/linux-practice/files

touch creates an empty file if it does not exist. It updates timestamps if the file already exists.

Write and read text

bash
printf 'First line
Second line
' > ~/linux-practice/files/notes.txt
cat ~/linux-practice/files/notes.txt
head -n 1 ~/linux-practice/files/notes.txt
tail -n 1 ~/linux-practice/files/notes.txt
  • > replaces the destination file.
  • >> appends to the destination.
  • cat prints the complete file.
  • less provides interactive paging.
  • head prints the beginning.
  • tail prints the end.

Copy and move

bash
cp notes.txt notes-copy.txt
cp -r folder-1 folder-2
mv notes-copy.txt ../notes-copy.txt
  • cp copies a file.
  • cp -r recursively copies a directory.
  • mv moves or renames.

Remove carefully

bash
rm notes.txt
rmdir empty-folder
rm -r unwanted-folder

Before removing:

  1. Print the absolute path with pwd.
  2. List the target with ls -ld TARGET.
  3. For rm -r, verify the directory contents first.
  4. Prefer moving uncertain files into a backup location.

Some commands support -i for confirmation. rm -i FILE asks before removal.

Common Interview Questions

Finished this lesson?

0 of 25 lessons done · 0% of the course