Useful Shell Commands

File and Directory Management

List files and directories

ls -la  # Long format with hidden files

Change directory

cd /path/to/directory
cd ..  # Go up one level
cd ~   # Go to home directory
pwd

Create directory

mkdir new_directory
mkdir -p path/to/nested/directory  # Create nested directories

Copy files/directories

cp source destination
cp -r source_directory destination_directory  # Recursive copy

Move/rename files/directories

mv source destination

Remove files/directories

rm file
rm -rf directory  # Recursive and force

Display file contents

cat file
less file  # Paginated view
head -n 10 file  # First 10 lines
tail -n 10 file  # Last 10 lines
tail -f file     # Follow file (for logs)

Find files

find /path -name "*.txt"  # Find files by name
find /path -type f -mtime -7  # Files modified in last 7 days

Search in files

grep "pattern" file
grep -r "pattern" /path  # Recursive search

Archive and compress

tar -czf archive.tar.gz directory  # Create gzip tarball
tar -xzf archive.tar.gz  # Extract gzip tarball

Disk usage

df -h  # Disk free space
du -sh directory  # Directory size

Process Management

List processes

ps aux  # All processes
top     # Interactive process viewer
htop    # Enhanced top (if installed)

Kill processes

kill PID
kill -9 PID  # Force kill
pkill process_name

Background jobs

command &  # Run in background
jobs       # List background jobs
fg %1      # Bring job to foreground
bg %1      # Resume in background

Network and Downloads

Download files

wget https://example.com/file
curl -O https://example.com/file

Check network connectivity

ping example.com
curl -I https://example.com  # HTTP headers

SSH connection

ssh user@hostname
scp file user@hostname:/path  # Secure copy

System Information

System info

uname -a  # Kernel info
whoami    # Current user
id        # User ID and groups

Environment variables

echo $PATH
export VARIABLE=value  # Set variable

History and aliases

history  # Command history
alias ll='ls -la'  # Create alias

Permissions

Change permissions

chmod 755 file  # rwxr-xr-x
chmod +x file   # Add execute permission

Change ownership

chown user:group file

Tree Command

Display directory tree structure:

tree -L 1 src  # Limit to 1 level

Example output:

src
├── api
├── dao
├── index.js
├── migrations
└── server.js

Additional Tips

  • Use man command for detailed help.
  • Combine commands with pipes: ls | grep txt
  • Use tab completion for efficiency.
  • For complex tasks, consider scripting with bash.

References