Shell Syntax & Core Features
1. Command Syntax Structure
The basic structure of a Linux command is like a sentence:
command [options] [arguments]
- Command: The action (e.g.,
ls) - Options: Modifies how it runs (e.g.,
-l) - Arguments: The target (e.g.,
/home/shad)
2. Case Sensitivity
Linux is strictly case-sensitive. Commands, options, and file names must match the exact case.
ls /home/shad # Works
ls /HOME/Shad # Fails
3. Tab Completion
Type the first few letters and press Tab to auto-complete commands and paths. Press twice to see all possibilities.
4. Command Chaining
Run multiple commands sequentially by separating them with a semicolon (;).
ls -l file1; ls -l file2
5. Piping, Paging, and Root Privileges
The pipe (|) passes output to another command. Use more or less to page long outputs.
ls -l /etc | more
Prepend your command with sudo to invoke root (administrative) privileges temporarily.
sudo mkdir /shad
System & Basic Info
pwd
Prints the current working directory path.
whoami
Prints the current effective username.
uname -a
Displays system and kernel information; does not list users.
date
Displays or sets the system date and time.
hostname
Displays the system's hostname.
uptime
Displays how long the system has been running.
who / w
Displays who is logged on and what they are doing.
last
Shows a listing of last logged in users.
Date Command Formatting
You can format the output of the date command using characters prefixed with %.
| Format | Description |
|---|---|
| %d / %D | Day of month (01-31) / Date as mm/dd/yy. |
| %m / %M | Month of year (01-12) / Minute (00-59). |
| %y / %Y | Year (last two digits) / Year (four digits). |
| %H / %l | Hour (24-hour) / Hour (12-hour). |
| %r / %R | Time (12-hour format) / Time (24-hour hh:mm). |
| %S / %T | Seconds (00-60) / Time (24-hour hh:mm:ss). |
Comparing User Commands
- whoami (Who am I?) Prints only your current effective username.
- who (Who is here?) Shows a simple list of all users currently logged into the system, their terminal name, and what time they logged in.
-
w (Who is here AND what are they doing?)
Similar to
who, but more detailed. Shows system uptime, CPU usage, and the exact process each user is running. -
last (Who was here?)
Shows a historical log of logins, logouts, and system reboots by reading from
/var/log/wtmp.
System Shutdown
The shutdown command is used to turn off a system safely. It warns all users, closes files, and performs necessary tasks.
Syntax: shutdown [-t seconds] [-options] time [warning message]
-r
Reboot the system after shutdown.
-h
Halt the system after shutdown (safely turn off power).
-c
Cancel a shutdown in progress.
-k
Send warning messages to everyone, but don't actually shutdown.
-f / -F
Skip (-f) or force (-F) filesystem check on reboot.
-t seconds
Wait specified seconds before changing runlevel.
Navigation & File Operations
Navigation
cd
Changes the current directory (returns to user's home directory by default).
cd ..
Moves to the parent directory.
cd /
Moves to the filesystem root directory.
Directories
-
mkdir {dir}Creates a new directory. -
rmdir {dir}Removes a directory (must be empty). -
rm -r {dir}Recursively removes a directory and all its contents.
Moving & Copying
-
mv {src} {dest}Moves or renames files and directories. -
cp {src} {dest}Copies a file to a new location. -
cp -r {src} {dest}Recursively copies a directory and its contents.
Listing Files: ls
The ls command lists directory contents. It has several powerful options:
-l
Long list format (permissions, owner, size, date).
-a
Shows all files, including hidden files (start with .).
-R
Recursive list of all subdirectories and files.
-F
Displays the nature of a file (e.g., / for directories, * for executable).
touch
Creates an empty file if it doesn't exist, or updates the access and modification timestamps of an existing file to the current time.
touch some.txt
stat
Displays detailed file status, including exact dates and times for when the file was last accessed, modified, or changed.
stat some.txt
Verbose Output: You can add the -v flag to many file operation commands (like mkdir, rm, mv, cp) to make the shell print a message detailing exactly what the command did.
Package Management (APT)
-
apt updateSyncs local system catalog with the remote repository. -
apt upgradeDownloads and installs the newest versions of all installed packages. -
apt searchSearches the repository for packages matching a keyword. -
apt installDownloads and installs a specific package. -
apt removeUninstalls the software binary but leaves configuration files. -
apt purgeCompletely removes the software and all of its configuration files. -
apt autoremoveSafely removes orphaned dependencies.
Key Differences in Removal
- remove vs purge: Use
removeif you plan to reinstall the software later and want to keep your custom configuration files. Usepurgewhen you want a completely clean slate with no leftover configs. - autoremove: This doesn't remove software you explicitly installed. Instead, it cleans up "orphaned" dependency packages that were automatically installed to support another program which has since been removed.
Service Management (systemctl)
-
systemctl start <svc>Starts a service immediately. -
systemctl stop <svc>Stops a currently running service. -
systemctl restart <svc>Restarts a service (useful after config changes). -
systemctl enable <svc>Configures a service to start automatically on boot. -
systemctl disable <svc>Disables automatic boot startup for a service. -
systemctl status <svc>Checks if a service is running, shows PID and recent logs.
Documentation / Help
Linux documentation provides information on various commands. Available from sources like manual pages, online resources, HOWTOs, and more.
Which Help Command Should I Use?
- "I know the command, but want a quick one-line summary:" Use
whatis <command>. - "I know what I want to do, but don't know the command:" Use
apropos <keyword>(e.g.,apropos partition). - "I need the full technical manual:" Use
man <command>. - "I need a detailed, hyperlinked guide (best for GNU tools):" Use
info <command>.
man command
Syntax: man {topic}
| Option | Description |
|---|---|
| -a | Finds all entries matching the query. |
| -D | Displays debugging information |
| -f | Displays a short description. |
| -h | Displays help options. |
| -k | Lists all manual pages containing the keyword. |
| -t | Formats the man pages to enable printing. |
Man Page Navigation & Sections
Sections are identified by the number beside the command (e.g., fsck (8)).
- 1: General commands
- 2: System calls
- 3: C library functions
- 4: Special files
- 5: File formats
- 6: Games
- 7: Miscellaneous
- 8: System admin
Keys:
- Home/End: Move to top/bottom
- PgUp/PgDn: Scroll
- /: Search
- n/p: Next/Prev search
- q: Quit
Other Help Options
-
apropos {keyword}Searches whatis database and returns matching man pages. -
whatis {command}Displays short description matching exact command. -
info {command}Displays info pages (links have *, move cursor & press Enter). -
command --helpDisplays quick summary of usage and arguments.
More Resources
- /usr/share/doc Contains installed documents describing Linux config/usage.
- HOWTOs Comprehensive docs, often under /usr/share/doc or online. (HTML, PDF, txt).
- Online Help Internet forums, official distribution websites, release notes, etc.
Search & Find Commands
whereis Command
The whereis command is used to view various details and files associated with a command. It locates the binary, source, and manual page files for a command.
Syntax: whereis [options] {command}
-b
Searches only for binaries.
-m
Searches only for manual pages.
-s
Searches only for source files.
-u
Searches for unusual entries.
grep Command
The grep command is a powerful search tool used to find specific text patterns or keywords inside files, or to filter the output of other commands via a pipe (|).
Searching File Contents
Search for a keyword inside files directly.
grep "pattern" filename
grep -r "pattern" /path/
Use -r to recursively search inside all files in a directory.
Filtering Output
Pipe output of another command (like ls) into grep to filter results.
ls -lR | grep "Q4"
Lists all files recursively but filters to only show those containing "Q4".
find Command
The find command enables you to search a specific location for files and directories that adhere to some search criteria recursively.
Syntax: find [options] {search locations} {search criteria} [actions]
Actions
-
-printDisplays the location of the files found. -
-execExecutes the command that follows. (e.g.-exec grep -H "text" {} \;) -
-okExecutes the command that follows interactively (prompts first). -
-deleteDeletes files found. -
-fprintStores results in the target file.
Logical Operators
By default, all criteria are treated as AND (all conditions must be met).
-
-orMatches if either the preceding or following condition is met. -
-notNegates the condition that follows it.
Numeric Arguments
n: Exactlyn(e.g. exactly 5 days ago)+n: Greater thann(e.g. more than 5 days ago)-n: Less thann(e.g. less than 5 days ago)
Common Search Conditions
-name
Matches by name. Wildcards allowed.
-iname
Matches by name, ignoring case.
-type
Matches by file type (e.g. f for file, d for dir).
-user / -group
Matches by owner username/UID or group name/GID.
-size
Matches by size.
-perm
Matches by symbolic or octal permissions.
-mtime
Matches by modification time in days.
-atime
Matches by access time in days.
-newer
Matches files modified later than a given reference file.
Archive & Backup Commands
tar Command
The tar command (derived from Tape ARchive) allows you to create archives of data. You can extract files, store additional files, update files, and list contents.
Syntax: tar [options] {files | directories}
Creating & Restoring Archives
tar cf filename.tar files*
Creates a new archive. Note: tar is one of the few commands that accepts options without a hyphen for historical reasons.
tar -xvf filename.tar
Restores the entire contents of the source file or directory structure. To restore a portion, provide the exact path and file name.
tar -wxvf [destination] [source]
Makes restoring files interactive.
Compression: Archives made with tar are frequently compressed with gzip (resulting in .tar.gz) or bzip2 (resulting in .tar.bz2).
gzip Command
GNU zip (gzip) is a compression utility that reduces the size of selected files (ignoring symbolic links). Files compressed with gzip frequently have the .gz extension.
Syntax: gzip [options] {file name}
Common Options
-d
Decompresses the file (same as gunzip).
-f
Forces compression/decompression even if file exists or has links.
-r
Descends into directories to compress files recursively.
-v
Displays the name and percentage reduction.
-t
Checks the compressed file for integrity.
-q
Suppresses all warnings.
-n
Omits saving original name & time stamp.
-N
Saves original file name & time stamp.
-h
Displays help. (-L for license).
Decompression Utilities: Compressed files can be restored to their original form using gzip -d, gunzip, or zcat.
unzip Command
The unzip command is used to list, test, and extract compressed files in a ZIP archive. By default, it extracts all files from the specified ZIP archive into the current working directory.
Syntax: unzip [options] {file name} -d [directory]
Common Options
-c
Extracts files to standard output.
-t
Tests files before extraction.
-f
Extracts new and freshens existing files.
-z
Displays the archive comment.
-v
Extracts and lists files verbosely.
Most Linux distributions also include a zip command which may be used to compress files into the ZIP archive format.
dump Command
The dump command "dumps" all files in a filesystem into a tape or another file. It can also be used to dump files modified after a specified date.
Syntax: dump {-level #} -f {file} {filesystem/file/directory}
Common Options
-0
Make a full backup.
-1 to 9
Make incremental or partial backups.
-b
Specify the number of kilobytes per dump record (maximum block size).
-f
Specify the location of the target file.
-z
Specify the compression level in the range 1 to 9.
restore Command
The restore command enables you to restore files or filesystems from backups made using the dump command. This command can be used across networks to restore data.
Common Options
-C
Compare the backup file with the source file.
-i
Run in restore mode to restore backups partially interactively.
-r
Perform a complete recovery of the backed up files.
-f
Specify the location of the backup file.
Multi-Volume Restoration: While making backups of large files on removable storage devices (like tape drives), the total size of the files will be split into smaller volumes (each identified with a specific volume number). When you want to restore the backup made on multiple volumes, specify the volume number starting from the last volume number to the first. Hard disks always have volume number 1.