Skip to content

Instantly share code, notes, and snippets.

@aw-junaid
Created January 30, 2026 17:28
Show Gist options
  • Select an option

  • Save aw-junaid/ef941bc44d116e665815e4e7509397d9 to your computer and use it in GitHub Desktop.

Select an option

Save aw-junaid/ef941bc44d116e665815e4e7509397d9 to your computer and use it in GitHub Desktop.
Essential Linux file commands for comparing, deleting securely, timestamps, disks/mounts, checksums, searching text/files, archiving/compressing, splitting/carving with dd, conversions, attributes, and useful misc ops like disabling history, compiling, rebooting, and URL extraction.

File & Misc Commands Cheat Sheet (Compare, Search, Archive, Hash, Convert)

Essential Linux file commands for comparing, deleting securely, timestamps, disks/mounts, checksums, searching text/files, archiving/compressing, splitting/carving with dd, conversions, attributes, and useful misc ops like disabling history, compiling, rebooting, and URL extraction.


Compare / diff

  • diff file file2
    Compare two text files line-by-line and show differences.

Delete / secure delete

  • rm -rf dir

    Force delete a directory recursively (dangerous: no prompt, removes everything under dir).

  • shred -f -u file

    Overwrites file contents to make recovery harder, then removes it (-u). -f forces permissions if possible.


Timestamps

  • touch -r ref_file file

    Sets file timestamps (atime/mtime) to match ref_file.

  • touch -t YYYYMMDDHHMM.SS file

    Sets timestamp explicitly (note the correct format includes minutes and optional .SS).
    Example:

    touch -t 202601301530.00 notes.txt

Disks / partitions / mounting

  • sudo fdisk -l

    Lists disks and partitions (connected drives). (-l is “list”; not -1.)

  • sudo mount /dev/sda# /mnt/usbkey

    Mounts a partition to a directory (replace # with partition number, e.g., /dev/sda1).
    Example:

    sudo mount /dev/sda1 /mnt/usbkey

Hashing / checksums

  • md5sum -t file

    Computes MD5 checksum. (-t is “text mode”; often unnecessary on Linux.)

  • echo -n "str" | md5sum

    Generates an MD5 hash of a string (-n avoids adding a newline).

  • sha1sum file

    Computes SHA1 checksum. (Your shalsum corrected to sha1sum.)


Sorting / unique lines

  • sort -u
    Sorts input and outputs unique lines (removes duplicates).
    Example:
    sort -u list.txt

Grep searching

  • grep -c 'str' file

    Counts matching lines containing str in file.

  • grep -Hnri word * | vim -

    Recursively searches for word in files under the current directory:

    • -H show filename, -n line number, -r recursive, -i ignore case
      Pipes results into vim for viewing (vim - reads stdin).
  • grep -rial word .

    Recursively lists filenames that contain word:

    • -r recursive, -i ignore case, -a treat binary as text, -l only names.

Tar archives (create/extract)

  • tar cf file.tar files

    Creates a .tar archive from files.

  • tar xf file.tar

    Extracts a .tar archive.

  • tar czf file.tar.gz files

    Creates a gzip-compressed tarball.

  • tar xzf file.tar.gz

    Extracts a gzip-compressed tarball.

  • tar cjf file.tar.bz2 files

    Creates a bzip2-compressed tarball.

  • tar xjf file.tar.bz2

    Extracts a bzip2-compressed tarball.


gzip compression

  • gzip file

    Compresses file into file.gz (original removed by default).

  • gzip -d file.gz

    Decompresses file.gz back to file. (file. gz corrected to file.gz.)


UPX packer

  • upx -9 -o out.exe orig.exe
    Compresses/“packs” an executable with maximum compression (-9) outputting out.exe.

Zip

  • zip -r zipname.zip Directory/
    Creates a zip archive recursively from a directory.

dd (extract/carve a portion of a file)

  • dd skip=1000 count=2000 bs=5 if=infile of=outfile
    Copies a slice of data from infile to outfile:
    • skips 1000 blocks, then copies 2000 blocks, block size 5 bytes
      Total extracted bytes = count * bs (here 10,000 bytes).

split (chunk files)

  • split -b 9K file prefix
    Splits file into 9 KB chunks named prefixaa, prefixab, etc.

Line ending conversion / text cleanup

  • awk '{ sub("\r$", ""); print }' unix.txt > win.txt

    Removes Windows CR (\r) at end-of-line, producing Unix-style output.
    (Your awk expression corrected and made functional.)

  • dos2unix file

    Converts DOS/Windows line endings (CRLF) to Unix (LF).


find (files, PDFs, permissions)

  • find / -iname '*.pdf'

    Searches case-insensitively for PDF files across /.

  • find / \( -perm -4000 -o -perm -2000 \) -type f -exec ls -ldb {} \;

    Finds setuid (4000) or setgid (2000) files and lists them in detailed form.


File type / metadata

  • file file
    Detects file type (text, ELF executable, PDF, image, etc.).

File attributes (immutable bit)

  • chattr +i file

    Makes a file immutable (cannot be modified/deleted even by root until removed).

  • chattr -i file

    Removes immutable attribute.


Infinite nested directory traversal

  • while [ $? -eq 0 ]; do cd flag/; done
    Keeps changing into flag/ repeatedly while the previous cd succeeded; useful when directories are nested the same way.

Miscellaneous commands

  • unset HISTFILE

    Disables writing shell history to the history file for the current session (does not erase existing history).

  • ssh user@ip "arecord -f cd -t raw" | aplay

    Records audio from the remote machine’s default microphone (arecord) and plays it locally (aplay).
    (Your original line corrected to a working form; audio devices and permissions must exist.)

  • gcc -o outfile myfile.c

    Compiles a C program (myfile.c) into an executable named outfile. (For C++ typically use g++.)

  • init 6

    Reboots the machine (runlevel 6). (init 0 = shutdown). On systemd systems, prefer reboot/shutdown.

  • cat /etc/syslog.conf | grep -v '#'

    Prints non-comment lines from syslog config.
    (Your path/pipe corrected; on many Linux systems syslog config may be /etc/rsyslog.conf.)

  • grep 'href=' file | cut -d'/' -f3 | grep url.com | sort -u

    Extracts hostnames from lines containing href= by splitting on /, filters for url.com, outputs unique sorted results. (Basic parsing; HTML can be messy.)

  • dd if=/dev/urandom of=file bs=3145728 count=100

    Generates a random file: 3,145,728 bytes per block (~3 MiB) × 100 ≈ 300 MiB total.


Important additions for this topic

Safer deletion / prompts / trash

  • rm -ri dir
    Interactive recursive delete (asks before removing).

Quick directory sizing / space usage

  • du -sh *
    Shows size of each item in current directory.

Faster searching / indexing

  • locate filename
    Fast filename search (uses a database; may require updatedb).

Copy / move / permissions

  • cp -a source dest

    Archive copy (preserves permissions, timestamps, symlinks).

  • rsync -avP source/ dest/

    Reliable copy with progress and resume behavior.

Inspect archives without extracting

  • tar tf file.tar.gz
    Lists contents of a tarball.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment