Init - logo

Command line tips

Here are some useful tips for the command line. Tested with bash

Find and list all log files beneath the current directory

find . -iname "*.log" -exec ls -lh {} \;

Remove the first character of all files, e.g. "101-doc1.txt" becomes "01-doc1.txt"

for i in *; do mv "$i" "`echo "$i" | sed 's/^.//'`"; done

Remove the last 7 lines of a file

for i in *; do tac < "$i" | tail -n +8 | tac > .tmpfile && mv .tmpfile "$i"; done

Remove all white lines from a file

for i in *; do grep . "$i" > .tmpfile && mv .tmpfile "$i"; done

Uppercase all files in a directory

for i in *; do mv "$i" "`echo "$i" | tr '[a-z]' '[A-Z']`"; done

Uppercase words in filenames

for i in *; do mv "$i" "`echo $i | sed 's/\<./\U&/g'`"; done

Echo the last part of the current directory

echo $PWD | sed 's/.*\///'

Echo the artist of an mp3 in the from of "01 - Artist - Track.mp3"

echo "01 - Artist - Track.mp3" | cut -d- -f2 | sed 's/ //'

Setting time on a Linux box

date MMDDhhmmYYYY

Create directories Season1 through Season6 in one statement

mkdir Season{1..6}