Skip to content

Instantly share code, notes, and snippets.

@ouyi
Last active Apr 28, 2019
Embed
What would you like to do?
Frequently used Linux Bash shell commands
# --- Join strings ---
function join_by { local IFS="$1"; shift; echo "$*"; }
# join_by ',' foo bar baz
# https://serverfault.com/questions/311856/portable-unix-way-to-join-strings-with-separator
echo -e "a\nb\nc" | tr '\n' ',' | sed 's/,$//g' # a,b,c
# --- Misc ---
# Retrieve the directory where the current script is in
dirname $BASH_SOURCE[0]
# Test exit status of piped commands
echo "true or false" |& tee out.log; test ${PIPESTATUS[0]} -eq 0
# List process id, start time, and command
ps -eo pid,lstart,cmd
# Examine RPM contents without installation
rpm2cpio myrpmfile.rpm | cpio -idmv
# Copy files newer than 2014-11-11 09:15 with rsync
touch -t 201411110915.00 ts
find . -newer ts -type f -print0 | rsync -0av --ignore-existing --files-from=- /source/path
# Create a tar ball of files whose names match a pattern, omitting directory structure
tar czf /var/tmp/test.tar.gz -C /path/to/files/ $(find /path/to/files/ -name '*pattern*' -printf "%f\n")
# Similar to the above but without using another shell, allowing this to be passed to `sudo -s bash -c "..."`
find '$src_path' -name '$pattern' -printf '%f\n' | tar czf $tmp_file -C $src_path --files-from=-
# Read a file line by line
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "Text read from file: $line"
done < "$1"
# Remove the last line from a file
head -n -1 foo.txt > temp.txt ; mv temp.txt foo.txt
# Remove the first line from a file
tail -n +2 foo.txt
# Retrieve and modify image date
exiftool -a -u -g1 b1.jpg
exiftool -AllDates="2019:04:28 10:25:30+02:00" b1.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment