File Operations
Basic File Manipulation Commands
Creating Files and Directories
## Create an empty file
touch newfile.txt
## Create a directory
mkdir new_directory
## Create nested directories
mkdir -p /path/to/nested/directory
File Copying and Moving
## Copy a file
cp source.txt destination.txt
## Copy directory recursively
cp -r source_directory destination_directory
## Move/Rename files
mv oldname.txt newname.txt
## Move file to another directory
mv file.txt /path/to/directory/
File Deletion
## Remove a file
rm file.txt
## Remove directory recursively
rm -r directory
## Force remove with confirmation
rm -i file.txt
File Permissions and Ownership
## Change file permissions
chmod 755 file.txt
## Change file ownership
chown user:group file.txt
File Operation Command Reference
Command |
Options |
Purpose |
cp |
-r |
Copy recursively |
mv |
- |
Move/Rename |
rm |
-i |
Interactive removal |
chmod |
- |
Change permissions |
File Inspection Commands
## View file contents
cat file.txt
## View first/last lines
head file.txt
tail file.txt
## Count lines, words, characters
wc file.txt
File Compression
## Compress files
tar -czvf archive.tar.gz directory
## Extract compressed files
tar -xzvf archive.tar.gz
File Search Techniques
## Find files by name
find / -name "filename.txt"
## Search file contents
grep "search_term" file.txt
File Operation Workflow
graph TD
A[Create] --> B[Copy/Move]
B --> C[Modify]
C --> D[Backup]
D --> E[Delete]
LabEx Tip
LabEx provides a safe environment to practice file operations without risking your primary system.
Advanced File Handling
Linking Files
## Create symbolic link
ln -s /path/to/original /path/to/symlink
## Create hard link
ln /path/to/original /path/to/hardlink
Best Practices
- Always use
-i
for critical deletions
- Backup important files before operations
- Understand permission implications
- Use absolute paths for clarity