Linux Commands

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 / %DDay of month (01-31) / Date as mm/dd/yy.
%m / %MMonth of year (01-12) / Minute (00-59).
%y / %YYear (last two digits) / Year (four digits).
%H / %lHour (24-hour) / Hour (12-hour).
%r / %RTime (12-hour format) / Time (24-hour hh:mm).
%S / %TSeconds (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 update Syncs local system catalog with the remote repository.
  • apt upgrade Downloads and installs the newest versions of all installed packages.
  • apt search Searches the repository for packages matching a keyword.
  • apt install Downloads and installs a specific package.
  • apt remove Uninstalls the software binary but leaves configuration files.
  • apt purge Completely removes the software and all of its configuration files.
  • apt autoremove Safely removes orphaned dependencies.

Key Differences in Removal

  • remove vs purge: Use remove if you plan to reinstall the software later and want to keep your custom configuration files. Use purge when 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}

OptionDescription
-aFinds all entries matching the query.
-DDisplays debugging information
-fDisplays a short description.
-hDisplays help options.
-kLists all manual pages containing the keyword.
-tFormats 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 --help Displays 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.

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.