Daily Weekly Monthly

Daily Shaarli

May 3, 2025

Process monitoring

ps Command

  • ps → Current shell process status
  • ps -E → All running processes
  • ps aux → All in BSD style with details
  • ps -EF → Running process with details
  • ps -u root → Processes of specific user
  • ps -EF | grep ? → Find process by pattern

top Command

  • top -u username → Show only that user's processes
  • top +d c → Absolute path
  • top +d k → Kill by PID
  • top +d M or +d P → Sort by memory or process usage

kill Commands

  • kill -l → List all signals
  • kill PID → Default terminate
  • kill -1 → Restart
  • kill -2 → Equivalent to Ctrl+C
  • kill -9 → Force kill
  • kill -15 → Graceful kill
  • killall → Kill all processes by name
  • pkill → Kill by process name
Basic File Commands / File Search Commands
  • touch one two three → Create 3 files

  • mkdir folder → Make directory

  • mv -R / cp -R → Move or copy folder including subfolders

  • find . -name "george" → Search from current directory

  • locate → Faster, but relies on a database

  • updatedb → Update the locate database


Wildcards

Symbol Meaning
* Zero or more characters
? Single character
[] Range of characters
\ Escape character
^ Beginning of the line
\$ End of the line
File Display Commands / Filters / Text Processing Input

File Display Commands

cat → Show entire content
cat -A → Show non-printable characters

more → Paginate output
less → Same as more but allows navigation with arrow keys

head → Show top lines (default: 10)
tail → Show bottom lines (default: 10)

head -2 myfile
tail -2 myfile

Filters / Text Processing Input

  • cut → Cut input
  • awk → List by columns
  • grep & egrep → Search by keyword
  • sort → Sort in alphabetical order
  • uniq → Remove duplicate lines
  • wc → Word count (including lines)

cut Command Examples

  • cut -c1 filename → First letter of each line
  • cut -c1,2,4 filename → Characters 1, 2, and 4
  • cut -c1-3 filename → Range: characters 1 to 3
  • cut -c1-3,6-8 filename → Ranges: characters 1-3 and 6-8
  • cut -b1-3 filename → Byte range 1 to 3
  • cut -d: -f6 /etc/passwd → Field 6 using : delimiter
  • cut -d: -f6-7 → Fields 6 and 7
  • ls -l | cut -c2-4 → Extract character range 2 to 4 from ls output

awk Command Examples

  • awk '{print $1}' file → Print 1st column of file
  • ls -l | awk '{print $1, $3}' → Print 1st and 3rd columns from ls -l
  • ls -l | awk '{print $NF}' → Print last column
  • awk '/jerry/ {print}' file → Search for "jerry" and print matching lines
  • awk -F: '{print $1}' /etc/passwd → Use : as delimiter, print 1st field
  • echo "hello tom" | awk '{$2="adam"; print}' → Replace column 2 with "adam"
  • awk 'length($0) > 15' file → Print lines longer than 15 characters

grep Command Examples

  • grep keyword file → Search for keyword
  • grep -c keyword file → Count matching lines
  • grep -i keyword file → Ignore case
  • grep -n keyword file → Show line numbers
  • grep -v keyword file → Invert match (everything except keyword)
  • grep keyword file | awk '{print $1}' → Pipe grep to awk to extract 1st column
  • ls -l | grep keyword → Filter ls -l output by keyword
  • egrep -i "keyword1|keyword2" file → Search for multiple keywords (case-insensitive)

sort Command Examples

  • sort file → Sort alphabetically
  • sort -r file → Sort in reverse order
  • sort -u file → Sort and remove duplicates
  • sort file | uniq → Same as above with explicit uniq
  • sort file | uniq -d → Show only duplicate lines
  • sort file | uniq -c → Count duplicates
  • sort -k4 -n file → Sort by 4th field (numeric)
  • ls -l | sort -k4 -n → Sort by size from ls -l

wc Command Examples

  • wc -c filename → Byte count
  • wc -w filename → Word count
  • wc -l filename → Line count
  • ls -l | wc -l → Count lines from ls -l
  • wc -l filename → Count number of lines in file

cmp / diff

  • diff → Compare files line by line
  • cmp → Compare files byte by byte

tar / gzip

  • tar cvf export.tar somedir → Compress directory
  • tar xvf export.tar → Extract tar archive
  • gzip export.tar → Compress with gzip
  • gzip -d export.tar.gz → Decompress gzip archive

truncate Command

  • truncate -s10 filename → Chop file to 10 bytes
  • truncate -s60 filename → Extend file to 60 bytes

Combining & Splitting Files

  • cat file1 file2 file3 > fileN → Combine multiple files into one

Linux File Ownership, ACLs, and I/O Redirects

File Ownership

  • chown → Change ownership
  • chgrp → Change group

Access Control List (ACL)

  • setfacl → Set file ACL
  • getfacl → Get file ACL

Add Permission to User

setfacl -m u:user:rwx /path/to/file
setfacl -m g:group:rw /path/to/file

Recursive Inheritance from Folder

setfacl -R -m entry /path/to/dir

Remove Specific ACL Entry

setfacl -x u:user /path/to/file

Remove All ACL Entries

setfacl -b /path/to/file

Check ACL

ls -ltr

Example output:

-rw-rw-r--+

Help Commands

  • whatis command
  • command --help
  • man command

Add Text to a File

echo "my text" > myfile    # Overwrite
cat myfile                 # Read the text
echo "hello" >> myfile     # Append

Input and Output Redirects
3 Redirect Types

  • stdin → 0 → < or << → Feeding file content to a command
  • stdout → 1 → > or >>
  • stderr → 2 → 2> or 2>> → Write error output to a file

tee Command

  • tee → Output + Save
echo "test" | tee myfile
tee -a file  # Append