7. Working with Files and Directories: mkdir, touch, cp, mv
Managing files is essential. These commands allow you to create, copy, and move data.
1. Creating Directories (mkdir)
mkdir stands for Make Directory. It creates a new folder.
bash
Create a folder named 'scripts'
$ mkdir scripts
Create nested folders (requires -p flag: parent)
$ mkdir -p projects/python/scripts
2. Creating Empty Files (touch)
touch is used to create an empty file or update the timestamp of an existing file.
bash
Create an empty text file
$ touch notes.txt
Create a simple bash script file
$ touch my_first_script.sh $ ls notes.txt my_first_script.sh scripts
3. Copying Files and Directories (cp)
cp stands for Copy. Syntax: cp [source] [destination].
bash
Copy notes.txt to the scripts folder
$ cp notes.txt scripts/notes_backup.txt
To copy an entire directory, you must use the -r (recursive) flag
$ cp -r scripts/ projects/backups
4. Moving and Renaming (mv)
mv stands for Move. It is used both to move a file/directory and to rename it.
bash
Rename notes.txt to important_notes.txt
$ mv notes.txt important_notes.txt
Move the renamed file into the projects directory
$ mv important_notes.txt projects/python/.