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 two text files line-by-line and show differences.
diff file file2
-
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).-fforces permissions if possible.
-
touch -r ref_file file
Sets
filetimestamps (atime/mtime) to matchref_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
-
sudo fdisk -l
Lists disks and partitions (connected drives). (
-lis “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
-
md5sum -t file
Computes MD5 checksum. (
-tis “text mode”; often unnecessary on Linux.) -
echo -n "str" | md5sum
Generates an MD5 hash of a string (
-navoids adding a newline). -
sha1sum file
Computes SHA1 checksum. (Your
shalsumcorrected tosha1sum.)
-
Sorts input and outputs unique lines (removes duplicates).
sort -u
Example:sort -u list.txt
-
grep -c 'str' fileCounts matching lines containing
strinfile. -
grep -Hnri word * | vim -
Recursively searches for
wordin files under the current directory:-Hshow filename,-nline number,-rrecursive,-iignore case
Pipes results intovimfor viewing (vim -reads stdin).
-
grep -rial word .Recursively lists filenames that contain
word:-rrecursive,-iignore case,-atreat binary as text,-lonly names.
-
tar cf file.tar files
Creates a
.tararchive fromfiles. -
tar xf file.tar
Extracts a
.tararchive. -
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 file
Compresses
fileintofile.gz(original removed by default). -
gzip -d file.gz
Decompresses
file.gzback tofile. (file. gzcorrected tofile.gz.)
-
Compresses/“packs” an executable with maximum compression (
upx -9 -o out.exe orig.exe
-9) outputtingout.exe.
-
Creates a zip archive recursively from a directory.
zip -r zipname.zip Directory/
-
Copies a slice of data from
dd skip=1000 count=2000 bs=5 if=infile of=outfile
infiletooutfile:- skips 1000 blocks, then copies 2000 blocks, block size 5 bytes
Total extracted bytes =count * bs(here 10,000 bytes).
- skips 1000 blocks, then copies 2000 blocks, block size 5 bytes
-
Splits
split -b 9K file prefix
fileinto 9 KB chunks namedprefixaa,prefixab, etc.
-
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 / -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.
-
Detects file type (text, ELF executable, PDF, image, etc.).
file file
-
chattr +i file
Makes a file immutable (cannot be modified/deleted even by root until removed).
-
chattr -i file
Removes immutable attribute.
-
Keeps changing into
while [ $? -eq 0 ]; do cd flag/; done
flag/repeatedly while the previouscdsucceeded; useful when directories are nested the same way.
-
unset HISTFILEDisables 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 namedoutfile. (For C++ typically useg++.) -
init 6
Reboots the machine (runlevel 6). (
init 0= shutdown). On systemd systems, preferreboot/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 forurl.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.
-
Interactive recursive delete (asks before removing).
rm -ri dir
-
Shows size of each item in current directory.
du -sh *
-
Fast filename search (uses a database; may require
locate filename
updatedb).
-
cp -a source destArchive copy (preserves permissions, timestamps, symlinks).
-
rsync -avP source/ dest/
Reliable copy with progress and resume behavior.
-
Lists contents of a tarball.
tar tf file.tar.gz