Commands Summary §
Command | Purpose |
---|
head | Display the first few lines of a file or input |
tail | Display the last few lines of a file or input |
cut | Extract specific portion from each row of a file or input |
grep | Filters rows having a specified pattern in a file or input |
sed | Stream editor used to find, replace and delete patterns in a file |
awk | Pattern search and processing language |
less | A pager that displays text files with scroll |
cat | Displays a file |
xargs | Converts input from stdin into arguments for a command |
sort | Sorts a file or input |
pwd | Print current working directory |
ls | list files and folders in a directory |
echo | Print a message to stdout |
man | Show a commands manual |
history | Show command history |
clear | Clear command console |
netstat | Show system’s network info (routing & sockets) |
ifconfig | Show system’s network interface config |
wget | Download files from a URL |
ping | Send a connection request to a server, used to check network connectivity |
shutdown | Shutdown system |
ps | List running processes |
pkill | Kill a running process |
systemctl | Manage system services |
time | Calculate command execution time |
uname | Print info of the system kernel |
chmod | Change permissions of a file |
chown | Change ownership of a file |
cd | Change working directory |
mv | Move files |
cp | Copy files |
rm | Delete files |
mkdir | Makes a new directory |
rmdir | Deletes an empty directory |
touch | Creates a new file |
sudo | Runs a command as a superuser |
xdg-open | Open file or URL using the default application |
count | Returns row count |
whoami | Print current user |
Cut §
ls -l | cut -d ' ' -f1-3 # prints fields 1,2 & 3 based on the space delimiter
ls -l | cut -b1-2 # prints first 2 bytes of every line
Sed §
# Replace all first occurences of 'unix' with 'linux'
# in all lines
sed 's/unix/linux/' sample.txt
# s - indicates substitution
# / - delimiter
# To replace all occurences
sed 's/unix/linux/g' sample.txt
Awk §
# Signature: awk '<Pattern> {<command>}' <file>
# start and end regex with '/'
# Print lines having the pattern
awk '/option/ {print}' sample.txt
# From the input stream
cat sample.txt | awk '/option/ {print}'
# Print 1st & 3rd column of lines with the pattern
# with row number
awk '/option/ {print NR " - " $1 $3}' sample.txt
# Print number of lines
awk 'END {print NR}' sample.txt
# Number of .md files in my vault
find -name '*.md' -type f | awk 'END {print NR}'
# Get URLs of all image previews in a mardown file
cat file.md | awk 'match($0, /!\[\]\(([^)]+)\)/, arr) { gsub(/[()]/, "", arr[1]); print arr[1]}'
Xargs §
# Total number of lines in .md files in my vault
find -name '*.md' -type f -print0 | xargs -0 cat | awk 'END {print NR}'
# Get line count of each file
find -name '*.md' -type f -print0 | xargs -0 wc -l