bash


Remove the first line from file / Remove first N lines from file

tail -n +2 someFile

*Notes: You MUST include the + sign or else the last 2 lines will be printed instead.

 

To remove N lines from the start of the file

tail -n +$M someFile

*NOTES: M = N + 1
You MUST include the + sign in-front of the number M or else the output will be the last M lines instead.


One line for loop

for i in $(seq $START $STEP $END); do echo "Iteration $i"; someCommand; someOtherCommand ; done

*INFO: $START: The starting value for the for loop, can be replaced by an integer.
$STEP: The step that the for loop is performing at the end of each iteration, can be replaced by an integer.
$END: The ending value for the for loop, can be replaced by an integer.

*NOTE: All kinds of bash for loops can be coded as above and made into one liners.


Execute a command on the results of a search with the find command 1

find $LOCATION -name "$FILENAME" -exec somecommand '{}' \;

The above command will search in $LOCATION for all files named $FILENAME and apply the command that you define at somecommand.

The argument ‘{}’ inserts each file found into the somecommand syntax. The \; argument indicates the exec command line has ended — YOU MUST INCLUDE IT.


Get the total size of a directory or file

du -sh someName

*INFO: someName can be replaced by any folder or file name or even a regular expression (for example to get the size of all txt files you need to invoke du -sh *.txt)
By using the parameter -s (which stands for summarize) you will get back only the total size in bytes.
By using the parameter -h (which stands for human readable format), for any size output you will get it back in Kilobytes, Megabytes etc.