Andrey's Blog

Linux: basic command

1. Shell basics

CommandWhat it does
chshChange your login shell (prompts if no shell is given)
pwdPrint 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 NShow first / last N lines
grep -i -vSearch text: -i ignore case, -v invert match
diff a bShow differences between two files
find <dir> -name <file> -printSearch directory tree by name
locate <name>Fast search using a prebuilt index
man -k <keyword>Search manual pages by keyword
passwdChange your password

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

SymbolNameMeaning in Linux
EscEscapeLeaves modes (e.g. insert mode in vi/vim)
~TildeHome directory: cd ~
`BacktickCommand substitution (old style): `date`
!BangHistory: !! last command, !$ its last argument
@Atuser@hostname in prompts and SSH
#HashComment marker; root’s prompt symbol
$DollarVariables ($HOME, $1); regular user’s prompt
%PercentJob control (%1); modulo in arithmetic
^CaretStart of line in regex: grep '^root' /etc/passwd
&AmpersandRun in background: ./script.sh &
*StarWildcard, matches anything: rm *.log (dangerous!)
( )ParenthesesGrouping / subshells
_UnderscoreNo special meaning — safe in filenames
-DashCommand options: ls -l
{ }Curly bracesBrace expansion: mkdir dir{1,2,3}
[ ]Square bracketsCharacter sets (file[12]); test conditions in scripts
:Colonvi commands (:wq); separator in $PATH, /etc/passwd
;SemicolonChain commands: cd /tmp; ls
" "Double quotesOne argument, variables still expand
' 'Single quotesOne argument, nothing expands
|PipeSend output of one command to input of another
\BackslashEscape next character (Windows path separator, not Linux)
<Less-thanInput redirection
>Greater-thanOutput redirection (overwrites)
>>Double greater-thanOutput redirection (appends)
. ..Dot, dot-dotCurrent / parent directory; leading dot hides a file
?Question markWildcard for exactly one character: file?.txt
/Forward slashPath 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)
CommandEffect
kill <pid>Terminate (TERM)
kill -STOP <pid>Freeze
kill -CONT <pid>Resume
kill -KILL <pid>Force kill (last resort)
kill -lList signal numbers and names
jobsShow 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 type: - regular file, d directory, l symbolic link.

chmod — numeric values

PermissionValue
r read4
w write2
x execute1
none0

Example 754: owner rwx = 7, group r-x = 5, others r-- = 4.

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
FlagMeaning
ccreate
xextract
ttest / list
vverbose
fnext argument is the file
ppreserve permissions
zgzip 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