Skip to content

Instantly share code, notes, and snippets.

@bkamapantula
Last active August 8, 2016 11:03
Show Gist options
  • Save bkamapantula/0045ee04e5e460f3bef4 to your computer and use it in GitHub Desktop.
Save bkamapantula/0045ee04e5e460f3bef4 to your computer and use it in GitHub Desktop.
Useful bash commands
# gets fileName.ext from /path/to/fileName.ext. if it's /path/to, it gets 'to'
file_name_w_ext=$(basename "$1")
# gets fileName from the given file
file_name="${file_name_w_ext%.*}"
# gets directory path
file-path=$(dirname "$1")
# collapse multiple columns into a single column (leaving the first column)
awk '{for(i=2;i<=NF;i++) {print $i}}' infile.txt > infile_col_collapse.txt
# sort, uniq
sort infile.txt | uniq > infile_uniq.txt
# find common lines between two files - each file has a single column. Here -1 suppresses lines unique to file_one and
# -2 suppresses lines unique to file_two, retaining only the common lines
comm -12 file_one file_two > common_lines.txt
# combine multiple images to one image. +append helps add images horizontally
convert -density 300 +append file1.pdf file2.pdf file3.pdf output.pdf
# images can also be combined using montage. This creates a 2x3 grid (2 rows by 3 columns)
montage -density 300 -geometry +0+0 -tile 3x3 file1.pdf file2.pdf file3.pdf file4.pdf file5.pdf file6.pdf output.pdf
# for each line in a text file, do something. Here for each line, we list the file and get the size and name of file.
ls -lh $(<file.txt) | awk '{print $5, $9}'
# another version of above
while read in; do ls -lh "$in" | awk '{print $5,$9}'; done < file.txt
# for each line in file (expects two columns, 2nd column being path to a file), copy the interest file to another location
cut -f2 file.txt | while read ln; do cp $ln /path/to/destination/; done
# grep a file for all the strings in another file
grep -f strings_to_search.txt file_to_be_searched.txt > matched_strings.txt
# look up process ID for a task running on a port (9989)
lsof -i :9989 | grep LISTEN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment