Essential Linux Commands: Files, Directories, Viewing, and Searching
The handful of commands you'll reach for right after logging into your server: move around, manage files, read contents, and find things fast.
The first time you SSH into your server, all you get is a blank terminal. Don't let it intimidate you. Get comfortable with the dozen or so commands below and you'll cover most day-to-day server work. Examples assume Ubuntu/Debian.
Getting Around
Start with "where am I, what's here, how do I get elsewhere?"
pwd # print the full path of the current directory
ls # list what's in the current directory
ls -lh # long format with human-readable file sizes
ls -a # include hidden files (those starting with .)
cd /var/www # change into a directory (absolute path)
cd .. # go up one level
cd # jump straight to your home directory
Tip: press Tab while typing a path to auto-complete it. Less typing, fewer typos.
Working with Files and Directories
Creating, copying, moving, and deleting are your bread and butter.
mkdir logs # create a directory
mkdir -p a/b/c # create nested directories in one go
touch app.log # create an empty file
cp src.txt dst.txt # copy a file
cp -r dir1 dir2 # copy a directory recursively
mv old.txt new.txt # rename or move a file
rm file.txt # delete a file
rm -r olddir # delete a directory recursively
There is no recycle bin for rm — once it's gone, it's gone. Double-check the path with ls before deleting, and treat rm -rf with respect, especially near / or your home directory.
Reading File Contents
You don't always need an editor. These are quicker for a glance.
cat config.yaml # dump the whole file to the screen
less big.log # page through a file; arrow keys scroll, q quits
head -n 20 app.log # show the first 20 lines
tail -n 50 app.log # show the last 50 lines
tail -f app.log # follow new log lines as they're written
Reach for less on large files — cat floods your screen all at once. tail -f is a favorite for watching logs roll in while you troubleshoot.
Searching and Finding
Use grep to search inside files, and find to locate files by name or attributes.
grep "error" app.log # find lines containing "error"
grep -i "error" app.log # case-insensitive match
grep -rn "timeout" /etc/nginx # search a directory, show line numbers
find . -name "*.conf" # find files by name under the current dir
find /var/log -mtime -1 # files modified within the last day
Pipes and Redirection
Chaining commands is where the shell really earns its keep.
ps aux | grep nginx # feed one command's output into the next
ls -l | less # page long output instead of scrolling past it
echo "hello" > a.txt # write to a file, overwriting its contents
echo "world" >> a.txt # append to the end of a file
| passes output from the left command to the right; > overwrites while >> appends — two ways to save results.
Summary
Find your footing with pwd/ls/cd, manage files with mkdir/cp/mv/rm, read them with cat/less/tail, and track things down with grep/find. Then glue it all together with | and >. When memory fails, command --help or man command is always a keystroke away. A little practice and the terminal quickly becomes second nature.