Matt Pavelle

Useful Bash Commands

# copy (via SCP) the most recent file (named: FileName.tgz) from a remote server (named: ServerName.com) to your local directory (named: .):
scp ServerName.com:/archive/`ssh ServerName.com "ls -rt /path/to/FileName.tgz | tail -1"` .

# recursively find all files that are JPEGs (by verifying they have a JPEG file header) and are missing the ".jpg" extension (for whatever reason) and add the ".jpg" extension onto the filename:
find . -type f -not -name "*.*" -print0 | xargs -0 file | grep 'JPEG image data' | sed 's/:.*//' | xargs -I % mv % %.jpg

# enter paths or filenames at the start of a command to make it easier to edit/alter the end of the command
$
                                
# fix a typo in the prior command by replacing string1 with string2 and rerunning the correction via: ^string1^string2
ehco one two three
bash: ehco: command not found
^ehco^echo
one two three
# jpeg manipulation requires exiftool and imagemagick # remove all exif data in jpeg file (named: original.jpg) except the icc_profile (this anonymizes and reduces image size) # then resize the file (named: original.jpg) to the stated dimensions (3000x4000 in this case) and save as a new file (named newfile.jpg)
exiftool -all= --icc_profile:all original.jpg
                                convert original.jpg -scale 3000x4800 -quality 92 -interlace line newfile.jpg

# jpeg manipulation requires imagemagick # for all jpeg files (named: *.jpg) in a directory, resize them and rename them resized-*.jpg
for i in `ls z*.jpg`; do convert $i -strip -scale 1500x2400 -quality 92 -interlace line resized-$i; done

# delete trailing whitespace from all lines in a file (named: original.txt) and save as a new file (named: stripped.txt)
sed 's/[ \t]*$//' original.txt > stripped.txt

# tar and compress a directory (named: /path/to/) but exclude two big directories (named: bigdir1 bigdir2) from the archive
sudo tar cfz ~/tarfile.tgz --exclude=/path/to/bigdir1 --exclude=/path/to/bigdir2 /path/to/

# start a python webserver in the current directory listening on port 8000 which can (for example) let someone temporarily download anything from that directory via a web browser
alias sharethis='python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"'
                                    sharethis