Essential Linux commands every user should know
The Linux command line looks intimidating at first, but a handful of commands cover most of what you’ll do day to day: finding files, searching text, checking what’s running, and editing files over SSH. This guide walks through those commands with practical examples you can try right away.
Image is captured on Linux Mint 19.2 Xfce Edition with cmatrix command running
1. Navigating and listing files
pwd- print the current working directoryls -la- list all files (including hidden ones) with details like permissions, size, and modified datecd <path>- change directory (cd ..goes up one level,cd ~goes home)
$ pwd
/home/user/projects
$ ls -la
drwxr-xr-x 4 user user 4096 Aug 3 10:00 .
-rw-r--r-- 1 user user 220 Aug 3 10:00 notes.txt
2. Finding a file
The easiest way to locate a file by name is the find command.
$ find [directory] -name "file name pattern"
$ find [directory] -iregex "pattern"
For example, to find every .log file under /var/log:
$ find /var/log -name "*.log"
find also accepts flags that control whether it follows symbolic links while searching:
-P- never follow symbolic links (default behavior)-L- follow symbolic links-H- never follow symbolic links, except for the directories/files given directly on the command line
3. Searching for text inside files
The grep command finds a piece of text inside one or more files. By default, grep prints each matching line.
$ grep "text" "file name pattern"
To search recursively through every file in a directory, add -r:
$ grep -r "text" "directory name"
Example: find every file that mentions “TODO” inside the current project, with line numbers.
$ grep -rn "TODO" .
./src/app.js:42:// TODO: handle empty response
Useful flags for grep:
-G- use basic regular expressions (the default)-i- ignore case-v- invert match, show lines that do not match-c- print a count of matching lines instead of the lines themselves-H- print the filename alongside each match-n- show the line number of each match
4. Checking running processes
ps aux- list all running processestoporhtop- live view of processes sorted by CPU/memory usagekill <pid>- stop a process by its process IDkill -9 <pid>- force-kill a process that won’t respond to a normalkill
$ ps aux | grep firefox
user 2345 2.3 4.1 ... firefox
5. File permissions
Linux permissions are shown as a 10-character string, e.g. -rwxr-xr--, representing owner/group/other read, write, and execute access.
chmod 755 file- set owner to read/write/execute, group and others to read/executechmod +x file- make a file executablechown user:group file- change the file’s owner and group
6. Redirection and pipes
command > file- write command output to a file, overwriting itcommand >> file- append command output to a filecommand1 | command2- send the output ofcommand1as input tocommand2
$ ls -la | grep ".conf"
7. VI editor commands
VI (or its modern successor vim) is the text editor most likely to be preinstalled on any Linux server. If you’re looking for a more modern terminal app to run these commands in, Tabby is worth a look. It has two modes: insert mode, for typing text, and normal mode, for issuing commands like the ones below. Press Esc to leave insert mode and return to normal mode before using these shortcuts.
a- append text after cursori- insert text before cursorcw- delete from the cursor to the end of the word and switch to insert modecc- delete the current line and switch to insert modedd- delete the current line:w- save:q- exit:q!- exit without saving:wq- save and exit/string- search forward forstring?string- search backward forstringn- jump to the next search matchN- jump to the previous search matchG- go to the last linenG- go to linenj- move cursor downk- move cursor up^- jump to the beginning of the line$- jump to the end of the linew- jump one word forwardb- jump one word backwardx- delete the character under the cursoru- undo the last changeyy- copy (yank) the current linep- paste the copied text after the cursor:s/old_text/new_text- find and replace on the current line
This list covers the commands you’ll reach for most often, but it isn’t exhaustive. If there’s a command you use regularly that isn’t listed here, let us know in the comments and we’ll add it.