Here’s a bash function that you can throw in your .bashrc and use more easily:
#!/bin/bash
# Put this in your ~/.bashrc and use it like a script...
append_filename() {
USAGE="append: Puts text between filename and extension"
USAGE="$USAGE\nUsage: $0 APPENDAGE file1 file2 file3 ..."
if [ "$#" == "0" ]; then
echo -e "$USAGE"
return 1 # exit kills shell this is called from
fi
# get appended string
APPENDAGE=$1
shift
while (( "$#" )); do
if [ -f "$1" ]; then # don't rename directories
mv "$1" "${1%.*}$APPENDAGE.${1##*.}"
fi
shift # grab the next file in list
done
}
I had a group of files that I needed to append a suffix to, but leave the extension intact. Basically, I needed to split the filename and extension, then put them back together with some filling in the middle…
If you got a file or group of files to put on a removable drive with a FAT32 file system – and any of them are over 4GB – then you need a way to make it smaller for transport. The easiest way is with split, cat and tar.
xargs converts find’s output to arguments, the sed RE drops comments and blank lines, and wc counts them lines. Thanks to theseposts on stack overflow for some reference material.
If you want to make sure you didn’t go over 79 or 80 characters in a file you can simply run:
#!/bin/bash
grep -Pn '.{80,}' file-to-check
This uses grep’s perl regex option (-P) so we can use the expression .{80,} to find any lines that have >= 80 characters. The -n option prints the line numbers in the output so you can find them easily.
=== UPDATE ===
Thanks to Tomasz Muras from Open Source Tech Blog, I now know about ssh-copy-id, which basically renders this post useless, except for academic reasons… Nevertheless, it’s a good example of piping through ssh to programs on a remote machine, so I’ll leave it up.
If you’ve already got access to a server or workstation via ssh, but are tired of entering your password, or need more security… You can transfer the public key on the client straight into the authorized_keys2 file on the server with one step:
PUB_KEY will most likely be id_rsa.pub or id_dsa.pub. REMOTE_HOST and REMOTE_PORT are the hostname and port number of the server you’re sending the key to (you can leave the port option out if its on the standard ssh port 22).
I’ve got a pretty good list of songs I like, and I wanted to copy them all to a USB stick for the car… So here’s the bash one-liner I wrote to take an m3u playlist and copy all the files to a new directory:
#!/bin/bash
cat playlist.m3u | grep -v '#' | \
while read i; do cp "${i}" /path/to/destination/ ; done