Skip to main content
Back to blog

A practical guide to the Linux command line

·3 min readLinux

The command line intimidated me for a long time. A blank terminal with a blinking cursor does not exactly scream "user friendly." But once I learned the 20 or so commands I actually needed, it became the fastest way to do almost everything.

# Where am I?
pwd
 
# List files (with details)
ls -la
 
# Change directory
cd /path/to/directory
 
# Go up one level
cd ..
 
# Go to home directory
cd ~
 
# Go to previous directory
cd -

ls -la shows hidden files (those starting with .) and file permissions. I use it more than plain ls.

File operations

# Create a file
touch newfile.txt
 
# Create a directory
mkdir my-project
 
# Create nested directories
mkdir -p projects/my-app/src
 
# Copy a file
cp source.txt destination.txt
 
# Copy a directory
cp -r source-dir/ destination-dir/
 
# Move or rename
mv oldname.txt newname.txt
 
# Delete a file
rm unwanted-file.txt
 
# Delete a directory and its contents
rm -rf old-project/

Be careful with rm -rf. There is no recycle bin. Deleted files are gone.

Reading files

# Show entire file
cat config.yaml
 
# Show first 20 lines
head -20 logfile.txt
 
# Show last 20 lines
tail -20 logfile.txt
 
# Follow a file as it updates (great for logs)
tail -f /var/log/syslog
 
# Search inside a file
grep "error" logfile.txt
 
# Search recursively in a directory
grep -r "TODO" ./src/

tail -f is invaluable for watching log files in real time. I use it constantly when debugging services.

Permissions

# Make a script executable
chmod +x script.sh
 
# Set specific permissions (owner: read/write/execute, others: read/execute)
chmod 755 script.sh
 
# Change file owner
chown user:group file.txt

Process management

# Show running processes
ps aux
 
# Find a specific process
ps aux | grep nginx
 
# Kill a process by PID
kill 12345
 
# Kill a process by name
pkill nginx
 
# Show system resource usage
htop

Piping and redirection

This is where the command line gets powerful. You can chain commands together:

# Find all TypeScript files containing "useState"
grep -r "useState" ./src/ | grep ".tsx"
 
# Count the number of files in a directory
ls | wc -l
 
# Sort a file and remove duplicates
sort names.txt | uniq
 
# Save command output to a file
ls -la > file-list.txt
 
# Append to a file
echo "new line" >> file.txt

The pipe (|) sends the output of one command as input to the next. Once this clicks, you start solving problems by chaining simple commands together.

The commands I use most

After two years of daily terminal use, these are the ones I reach for constantly:

  • cd, ls, pwd for navigation
  • grep -r for searching code
  • tail -f for watching logs
  • chmod for fixing permission issues
  • cat for quickly reading config files
  • | pipes for chaining commands
  • Ctrl+R for searching command history

Everything else I look up as needed. You do not need to memorize hundreds of commands. The 20 or so basics cover 90% of daily use.

Sources

Enjoying the blog? Subscribe via RSS to get new posts in your reader.

Subscribe via RSS