A practical guide to the Linux command line
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.
Navigation
# 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.txtProcess 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
htopPiping 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.txtThe 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,pwdfor navigationgrep -rfor searching codetail -ffor watching logschmodfor fixing permission issuescatfor quickly reading config files|pipes for chaining commandsCtrl+Rfor 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
Related posts
Setting up a productive dev environment on Linux
The actual tools, terminal setup, and configuration I use for web development on Linux.
Why I use Linux for web development
My case for using Linux as a web development environment, and the practical advantages it has over Windows.
Why Pop!_OS is my Linux distro of choice
What makes Pop!_OS stand out as a Linux distribution for developers, and why I chose it over Ubuntu, Fedora, and Arch.
Enjoying the blog? Subscribe via RSS to get new posts in your reader.
Subscribe via RSS