Essential Linux File Operations and Commands
Core File Operations in Linux
File Naming Conventions
- Use descriptive names for files and directories
- Separate words with underscores (e.g.,
my_website.html) - Ensure unique names within the same directory (Linux is case-sensitive)
Example:
touch 'descriptive_name.txt'
touch project_backup.tar.gz
Directory Operations
- Create directories:
mkdir dir_name - Create nested directories:
mkdir -p path/to/nested/dir - Remove empty directories:
rmdir dir_name
Example:
mkdir -p projects/2023/linux/config
File Operations
- Create files:
touch filename - Delete files:
rm filename - Force delete:
rm -f filename - Recursive delete:
rm -rf directory
Example:
touch {report1,report2,report3}.log
rm -f obsolete_file.txt
Copy and Move Operations
- Copy files:
cp source destination - Copy directories:
cp -r source_dir destination - Move/rename:
mv old_name new_name
Example:
cp -r /var/log/nginx /backups/
mv old_config.txt new_config.cfg
Compression and Archiving
- Create tar archive:
tar -cvf archive.tar files/ - Compress with gzip:
tar -czvf archive.tar.gz files/ - Extract archive:
tar -xvf archive.tar
Example:
tar -czvf web_backup.tar.gz /var/www/html
File Viewing Commands
- View entire file:
cat filename - View with line numbers:
cat -n filename - View end of file:
tail -n 20 filename - Follow log changes:
tail -f /var/log/syslog
Example:
tail -f /var/log/nginx/access.log
Search Operations
- Find files by name:
find / -name "*.conf" - Find files containing text:
grep -r "search_term" /path - Count lines:
wc -l filename
Example:
find /etc -name "*.conf" | xargs grep -l "timeout"
Process and System Monitoring
- View processes:
ps aux - Filter processes:
ps aux | grep nginx - Check ports:
netstat -tulnp
Example:
ps aux | grep java
netstat -tulnp | grep :80
Text Editing with Vim
- Basic commands:
- Insert mode:
i - Save and quit:
:wq - Quit without saving:
:q! - Copy line:
yy - Delete line:
dd - Go to start:
gg - Go to end:
G - Undo:
u
- Insert mode: