Linux: Basic Command
1. Shell basics
| Command | What it does |
|---|---|
chsh | Change your login shell (prompts if no shell is given) |
pwd | Print current working directory |
file <name> | Identify the type of a file |
less <file> | Page through a file (q to quit, / to search) |
head -n N / tail -n N | Show first / last N lines |
grep -i -v | Search text: -i ignore case, -v invert match |
diff a b | Show differences between two files |
find <dir> -name <file> -print | Search directory tree by name |
locate <name> | Fast search using a prebuilt index |
man -k <keyword> | Search manual pages by keyword |
passwd | Change your password |
Manual page sections
The manual is split into numbered sections, and the same name can exist in several of them — passwd(1) is the command, passwd(5) is the file format of /etc/passwd. The number in parentheses after a name (ls(1), open(2)) tells you which section it comes from.
| Section | Contents | Example |
|---|---|---|
| 1 | User commands | man 1 ls |
| 2 | System calls (kernel functions) | man 2 open |
| 3 | Library functions (mostly C library) | man 3 printf |
| 4 | Special files and devices (in /dev) | man 4 null |
| 5 | File formats and configuration files | man 5 passwd, man 5 fstab |
| 6 | Games | man 6 fortune |
| 7 | Miscellaneous: conventions, protocols, standards | man 7 signal, man 7 hier |
| 8 | System administration commands (usually root) | man 8 mount |
| 9 | Kernel routines (not always present) |
man passwd # first match — section 1 (the command)
man 5 passwd # the /etc/passwd file format
man -a passwd # show every section that has a page, one after another
man -f passwd # list sections where the name exists (same as whatis)
man -k <keyword> # search descriptions (same as apropos)
The default search order is roughly 1, 8, 2, 3, 5, … (see man man), so man with no number usually gives you the command first.
Environment variables
VARIABLE=value # shell variable (current shell only)
export VARIABLE # make it an environment variable (visible to child processes)
$PATH — colon-separated list of directories the shell searches when locating a command.
2. Special symbols
| Symbol | Name | Meaning in Linux |
|---|---|---|
Esc | Escape | Leaves modes (e.g. insert mode in vi/vim) |
~ | Tilde | Home directory: cd ~ |
` | Backtick | Command substitution (old style): `date` |
! | Bang | History: !! last command, !$ its last argument |
@ | At | user@hostname in prompts and SSH |
# | Hash | Comment marker; root’s prompt symbol |
$ | Dollar | Variables ($HOME, $1); regular user’s prompt |
% | Percent | Job control (%1); modulo in arithmetic |
^ | Caret | Start of line in regex: grep '^root' /etc/passwd |
& | Ampersand | Run in background: ./script.sh & |
* | Star | Wildcard, matches anything: rm *.log (dangerous!) |
( ) | Parentheses | Grouping / subshells |
_ | Underscore | No special meaning — safe in filenames |
- | Dash | Command options: ls -l |
{ } | Curly braces | Brace expansion: mkdir dir{1,2,3} |
[ ] | Square brackets | Character sets (file[12]); test conditions in scripts |
: | Colon | vi commands (:wq); separator in $PATH, /etc/passwd |
; | Semicolon | Chain commands: cd /tmp; ls |
" " | Double quotes | One argument, variables still expand |
' ' | Single quotes | One argument, nothing expands |
| | Pipe | Send output of one command to input of another |
\ | Backslash | Escape next character (Windows path separator, not Linux) |
< | Less-than | Input redirection |
> | Greater-than | Output redirection (overwrites) |
>> | Double greater-than | Output redirection (appends) |
. .. | Dot, dot-dot | Current / parent directory; leading dot hides a file |
? | Question mark | Wildcard for exactly one character: file?.txt |
/ | Forward slash | Path separator and filesystem root |
3. Redirection and pipes
File descriptors: 0 = stdin, 1 = stdout, 2 = stderr.
cmd > file # stdout to file (overwrite)
cmd >> file # stdout to file (append)
cmd 2> file # stderr to file
cmd > file 2>&1 # stdout and stderr to the same file
cmd < file # file as stdin
cmd1 | cmd2 # stdout of cmd1 -> stdin of cmd2
4. Processes
ps x # all your processes
ps ax # all processes on the system
ps u # more detailed information
ps w # full command names
ps u $$ # info about the current shell ($$ = its PID)
| Command | Effect |
|---|---|
kill <pid> | Terminate (TERM) |
kill -STOP <pid> | Freeze |
kill -CONT <pid> | Resume |
kill -KILL <pid> | Force kill (last resort) |
kill -l | List signal numbers and names |
jobs | Show suspended / background jobs |
cmd & | Run command in background |
nohup cmd & | Keep running after logout |
5. File permissions
-rw-r--r-- 1 linuxize users 12288 Apr 8 20:51 filename.txt
|[-][-][-]- [------] [---]
| | | | | | |
| | | | | | +------------> 7. Group
| | | | | +--------------------> 6. Owner
| | | | +--------------------------> 5. Alternate access method
| | | +----------------------------> 4. Others permissions
| | +-------------------------------> 3. Group permissions
| +----------------------------------> 2. Owner permissions
+------------------------------------> 1. File type
File types (first character of ls -l)
| Symbol | Type | Example |
|---|---|---|
- | Regular file | /etc/passwd |
d | Directory | /home |
l | Symbolic link | /bin -> usr/bin |
b | Block device (data in blocks, e.g. disks) | /dev/sda |
c | Character device (data as a stream, e.g. terminals) | /dev/tty, /dev/null |
p | Named pipe (FIFO) — one process writes, another reads | mkfifo mypipe |
s | Socket — local inter-process communication | /run/docker.sock |
chmod — numeric values
| Permission | Value |
|---|---|
r read | 4 |
w write | 2 |
x execute | 1 |
| none | 0 |
Example 754: owner rwx = 7, group r-x = 5, others r-- = 4.
umask— default permissions mask for newly created files
Links: hard vs symbolic
ln <target> <linkname> # hard link
ln -s <target> <linkname> # symbolic (soft) link
The difference is what the link points to. A hard link is another name for the same inode — the actual data on disk. A symbolic link is a separate small file that stores the path of the target.
| Hard link | Symbolic link | |
|---|---|---|
| Points to | Inode (the data itself) | Path name of the target |
ls -l shows | Regular file (-), link count > 1 | l, e.g. link -> target |
| Target deleted | Data survives as long as one name remains | Link breaks (“dangling”) |
| Target moved/renamed | Still works | Breaks |
| Across filesystems | No (same filesystem only) | Yes |
| Link to a directory | No | Yes |
| Own permissions | Same as the file (it is the file) | Ignored — target’s permissions apply |
Check with ls -li (the -i shows the inode number): hard links to the same file share one inode number, and the second column is the link count.
Sources: CBT Nuggets, GeeksforGeeks
6. Archives and compression
gzip file # -> file.gz
gunzip file.gz
zcat file.gz # same as gunzip -dc (print to stdout)
tar cvf archive.tar file1 file2 # create
tar xvf archive.tar # extract
tar tvf archive.tar # list contents
tar ztvf file.tar.gz # list a gzipped archive
| Flag | Meaning |
|---|---|
c | create |
x | extract |
t | test / list |
v | verbose |
f | next argument is the file |
p | preserve permissions |
z | gzip compression |
7. Filesystem Hierarchy Standard
/
├── bin/ ready-to-run commands (essential binaries)
├── sbin/ system executables (for root)
├── boot/ kernel and boot loader files
├── dev/ device files
├── etc/ core system configuration (passwd, boot, network, ...)
├── home/ personal directories of regular users
├── lib/ shared and static libraries used by executables
├── media/ mount point for removable media (USB drives, ...)
├── opt/ additional third-party software
├── proc/ info about running processes and kernel parameters
├── run/ runtime data: PIDs, sockets, status records, logging
├── sys/ device and system interface (similar to /proc)
├── tmp/ small temporary files
├── var/ variable data: logs, caches, user tracking
└── usr/ user programs and data
├── include/ header files for the C compiler
├── local/ software installed by the administrator
├── man/ manual pages
└── share/ architecture-independent files
Kernel location: /vmlinuz or /boot/vmlinuz.
8. Root and sudo
sudo <cmd> # run command as root
visudo # safely edit /etc/sudoers (who may use sudo)
journalctl SYSLOG_IDENTIFIER=sudo # view sudo logs