Skip to content

Instantly share code, notes, and snippets.

@unrelentingfox
Last active August 30, 2023 21:21
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 unrelentingfox/031d5e60778aed7eaa0d3922b3dc34de to your computer and use it in GitHub Desktop.
Save unrelentingfox/031d5e60778aed7eaa0d3922b3dc34de to your computer and use it in GitHub Desktop.
A list of one liner bash commands that I found useful and would like to remember
# Find all files installed with a package e.g. tilda
dpkg -L tilda
# Get the size of a file
du -sh file_path
# Get the summary of disk usage of currect directory, human readable, with total, sorted by size
du -shc * | sort -h
# Show more context in grep
grep -C 3 file_path
# Show a few most recently modified files in directory
ls -lt | head
# Search for "foo" with 50 character buffer at the end in single line files in current directory
grep -roP "foo.{0,50}" .
# -r recursive
# -o print only what matched
# -p perl style regex
# Awk for viewing columns in a csv file where each value is formatted like "field","field","field"
awk -F "\"*,\"*" '{print $3,$2}' FileName.csv
# Get comma separated list of values from a line-separated csv column
paste -sd, file.csv > list.txt
# Use sed to replace text using regex
sed 's/originaltext/replacementtext/g' filename
# Use find and sed to replace the version number dbchangelog-3.0 to dbchangelog-3.5 in multiple files
find . -name "*changelog.xml" -exec sed -i 's/dbchangelog-[0-9]\.[0-9]/dbchangelog-3.5/g' {} +
# Live view of a log file
tail -f filename.txt
# find files in directory that are greater than 50 MB
ls -lR | awk 'NF == 9 && $1 !~ /^d/ && $5 > 50000000'
# remove all files in directory except a filename1 and filename2
rm -v !("filename1"|"filename2")
# recursively rename files (example *AcceptanceTest.java -> *IT.java)
find . -iname "*AcceptanceTest*" -exec rename 's/AcceptanceTest.java$/IT.java/' -vn '{}' \;
# run command on multiple ssh hosts
parallel-ssh -h hosts.txt -l username -O StrictHostKeyChecking=no -A 'echo Hello World'
# stop services with a wildcard
service --status-all | grep -oP 'prefix_.*' | xargs -i service {} stop
# adhock mount windows share with username
sudo mount -t cifs //path/to/target /mnt/local/destination/folder -o username=yourname
# rename
rename 's/_Rule/Rule/' ./*
# find any desktop entry file
locate .desktop | grep vim
# filter a csv file using a list of identifiers
grep -Fwf ids.txt targetFile.txt
# format all json files in the directory (python3 json.tool doesn't sort keys)
find -type f | grep '.json'| xargs -I % sh -c 'python3 -m json.tool % > test; mv t
est %'
# trim whitespace and duplicates from csv list
sed -e 's/\(.*\)/\L\1/' words.csv | sed 's/,[ \t]\+/,/g' | awk 'BEGIN{RS=ORS=","} !seen[$0]++'
# identify large files on system
sudo du -Shc * | sort -rh | head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment