Skip to content

Latest commit

 

History

History
135 lines (89 loc) · 1.93 KB

File metadata and controls

135 lines (89 loc) · 1.93 KB

File Operations Patterns

This note covers common patterns for creating, copying, moving, renaming, organizing, and deleting files from the shell.

Create Directories

Create one directory:

mkdir reports

Create nested directories:

mkdir -p project/{src,docs,tests}
mkdir -p project/logs/archive

Create a directory with a specific mode:

mkdir -m 700 private

Useful options:

  • -p creates missing parent directories and does not fail if the directory already exists.
  • -m MODE sets the directory permission mode at creation time.
  • -v prints each directory created.

Copy Files

Copy one file to a new file:

cp report.txt report.backup.txt

Copy files into a directory:

cp *.txt text-files/

Copy a directory tree:

cp -r site site.backup

Preserve basic metadata:

cp -p original.txt preserved-copy.txt

Prompt before overwriting:

cp -i draft.txt final.txt

Move and Rename

Rename a file:

mv draft.txt final.txt

Move a file into a directory:

mv final.txt reports/

Move several files:

mv *.log logs/

Prompt before overwriting:

mv -i new.conf app.conf

Delete Conservatively

Remove one file:

rm old-report.txt

Prompt before removal:

rm -i old-report.txt

Remove an empty directory:

rmdir empty-dir

Remove a directory tree only when you have verified the target:

pwd
ls -la old-build
rm -r old-build

Avoid using rm -rf as a default habit. It is useful in automation only when the target path is explicitly controlled and verified.

Organizing a Small Project

One compact pattern:

mkdir -p project/{bin,docs,logs,src,tests}
touch project/README.md

Then inspect:

find project -maxdepth 2 -type d | sort

The useful lesson is the sequence: create structure, create minimal marker files, then inspect the result.