Skip to content

Instantly share code, notes, and snippets.

@fulgorek
Created June 22, 2018 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fulgorek/b27f818b68ddc34cae16c37952ffe250 to your computer and use it in GitHub Desktop.
Save fulgorek/b27f818b68ddc34cae16c37952ffe250 to your computer and use it in GitHub Desktop.
bash basic
#1. print the first 10 lines of /var/log/syslog
head -n 10 /var/log/syslog
#2. print the last 10 lines of /var/log/syslog
tail -n 10 /var/log/syslog
#3. print the Nth line of /var/log/syslog
sed "100q;d" /var/log/syslog (replace 100 for the line number you want to)
#4. print username, uid, gid fields from /etc/passwd
cat /etc/passwd | awk '{split($0,a,":"); print a[1],a[3],a[4]}'
#5. print disk free space
df -k | grep "/$" | awk '{print $4}' show space for / mount in bytes
#6. search for the pattern 'error' in /var/log/syslog and print the first matching line
grep -e 'error' /var/log/syslog | head -n 1
#7. search for the pattern 'error' in /var/log/syslog and print the last matching line
grep -e 'error' /var/log/syslog | tail -n 1
#8. search for the pattern 'error' in /var/log/syslog and print the total count of matching lines
grep -c 'error' /var/log/syslog
#9. search for the pattern 'error' in /var/log/syslog and print the hourly counts of matching lines
grep -r 'error' /var/log/syslog | cut -f 1-3 -d " "
#10. write a function that takes a file path as its only argument and prints 'yes' (if file exists) or 'no' (otherwise)
function check_file {
[[ -f ${1} ]] && echo "yes" || echo "no"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment