Theory
एक File की Life
एक file create होती है, देखी जाती है, copy होती है, move या rename होती है, और आख़िर में delete होती है। Linux आपको इनमें से हर एक के लिए एक छोटा command देता है, और साथ मिलकर ये वे tools हैं जो आप server पर अपना work हर दिन manage करने के लिए इस्तेमाल करेंगे।
पाँच हैं touch, cat, cp, mv, और rm। ज़्यादातर gentle हैं। एक, rm, genuinely dangerous है, क्योंकि Linux for real delete करता है, बचाने के लिए कोई recycle bin नहीं। यह lesson family introduce करता है और exactly flag करता है कहाँ careful रहना है।
At a glance
| Command | यह क्या करता है |
|---|---|
| touch file | एक empty file create करता है (या एक existing file की timestamp update करता है) |
| cat file | एक file के contents screen पर दिखाता है (files को साथ भी join करता है) |
| cp src dst | src को dst में copy करता है (पूरी directory copy करने के लिए cp -r) |
| mv old new | एक file move करता है, या इसे rename करता है (same command दोनों करता है) |
| rm file | एक file permanently delete करता है (directory और इसके contents के लिए rm -r) |
Practical
एक File-Management Session
$ echo "hello" > a.txt # create a.txt containing 'hello'
$ cat a.txt
hello
$ cp a.txt b.txt # copy: now a.txt AND b.txt exist
$ mv b.txt c.txt # rename b.txt to c.txt
$ ls
a.txt c.txt
$ rm a.txt # delete a.txt (permanently!)
$ ls
c.txtWatch out
rm Permanent है, और rm -r Classic Disaster है
Linux shell में कोई recycle bin नहीं है। जब आप एक file rm करते हैं, यह चली जाती है, trash में move नहीं होती, gone। सावधानी से type कीजिए।
Famous trap है `rm -r`, जो एक directory AND इसके अंदर सब कुछ delete करता है, recursively। इसे wrong directory पर aim कीजिए और आप एक instant में work की huge amounts erase कर सकते हैं। दो habits help करती हैं: हर delete से पहले prompted होने के लिए `rm -i` इस्तेमाल कीजिए, और Enter press करने से पहले हमेशा path double-check कीजिए। rm को respect कीजिए; यह default रूप से दो बार नहीं पूछता।
Quiz
आप चलाते हैं: cp a.txt b.txt और फिर mv b.txt c.txt। बाद में कौन सी files exist करती हैं (मानते हुए a.txt शुरुआत में exist करता था)?
- सिर्फ़ c.txt, क्योंकि copying और moving originals delete कर देते हैं
- a.txt और c.txt (cp ने a.txt को जगह पर छोड़ा और b.txt बनाया; mv ने फिर b.txt को c.txt में rename किया)
- a.txt, b.txt, और c.txt, तीनों
- सिर्फ़ a.txt, क्योंकि बाकी temporary थीं
Show the answer
a.txt और c.txt (cp ने a.txt को जगह पर छोड़ा और b.txt बनाया; mv ने फिर b.txt को c.txt में rename किया)
इसे trace कीजिए: cp a.txt b.txt COPY करता है, तो a.txt रहता है और एक नई b.txt appear होती है (दो files)। फिर mv b.txt c.txt, b.txt को c.txt में RENAME करता है, तो b.txt, c.txt बन जाती है। आपके पास a.txt और c.txt बचती हैं। Option A ग़लत है: cp original delete नहीं करता (copying का पूरा point यही है), और mv ने सिर्फ़ b.txt rename किया, a.txt नहीं। Option C ग़लत है: mv के इसे c.txt में rename करने के बाद b.txt अब exist नहीं करता। Option D ग़लत है: यहाँ कुछ भी a.txt या c.txt delete नहीं करता। Key distinction: cp duplicate करता है (original रहता है), mv relocate या rename करता है (original name चला जाता है)।
Think first
'Move' और 'Rename' Same Command क्यों है?
mv एक file को दूसरी directory में move भी करता है AND इसे in place rename भी करता है। एक command दो jobs क्यों कर रहा है? फिर tap कीजिए।
Show the answer
क्योंकि Linux के लिए, moving और renaming SAME underlying operation हैं: एक file का path बदलना। एक file का name असल में सिर्फ़ directory tree में इसकी location है, तो 'इस file को एक नया नाम दो' और 'इस file को एक नई जगह रखो' दोनों बस 'यह entry कहाँ point करता है यह बदलो' हैं। जब आप same directory में mv old.txt new.txt चलाते हैं, आप path का name part बदल रहे हैं, एक rename। जब आप mv report.txt /home/riya/archive/ चलाते हैं, आप path का directory part बदल रहे हैं, एक move। Same action, path बदलना, तो एक command, mv, दोनों handle करता है, और यह इन्हें साथ में कर सकता है (mv old.txt archive/new.txt एक साथ move AND rename करता है)। यह दो अलग commands रखने से ज़्यादा elegant है, और यह उस underlying truth को reflect करता है कि एक filename बस एक location पर एक label है। Practical caution नोट कीजिए: क्योंकि mv path बदलता है, यह एक ऐसी file को overwrite कर सकता है जिसका destination name पहले से है, silently, तो unsure होने पर prompted होने के लिए mv -i इस्तेमाल कीजिए। एक command, क्योंकि moving और renaming एक idea हैं।
Summary
Key takeaways
- touch एक empty file create करता है (या इसकी timestamp update करता है); cat एक file के contents दिखाता है।
- cp src dst एक file copy करता है (original रहता है); cp -r पूरी directory copy करता है।
- mv old new एक file move OR rename करता है, same command, क्योंकि दोनों बस file का path बदलते हैं।
- rm एक file permanently delete करता है; rm -r एक directory और इसके अंदर सब कुछ delete करता है।
- कोई recycle bin नहीं है: rm irreversible है, और wrong directory पर rm -r classic disaster है।
- cp और mv silently एक existing destination को overwrite कर सकते हैं; prompted होने के लिए -i इस्तेमाल कीजिए, और Enter से पहले paths check कीजिए।
- Memory hook: touch बनाओ, cat दिखाओ, cp copy, mv move/rename, rm हमेशा-के-लिए-delete।