One-Liners

Helpful one liners


Recursively replace a string in files

This will lookup recursively all files (type f) below /your/path and replace the string your_old_string by your_new_string. find /your/path/ -type f | xargs /usr/bin/perl -w -i -p -e "s/your_old_string/your_new_string/g" You might want to exclude certain file types or choose by name: find /your/path/ -type f -name "*.xml" | xargs /usr/bin/perl -w -i -p -e "s/your_old_string/your_new_string/g" find /your/path/ -type f -name "my*file*" | xargs /usr/bin/perl […]


To print the first and last characters of a string in bash

The actual string: var="somebash" This outputs "some": echo ${var:0:4} echo $var | sed 's/\(.\{4\}\).*/\1/' echo $var | awk '{print substr($0,1,4)}' echo $var | cut -c1-4 printf "%.4s\n" "$var" echo "${var%${var#????}}" This outputs "bash": echo $var | cut -c4- echo "${var#????}" echo ${var:4} echo $var | awk '{print substr($0,5)}'


Finding files and directories with find

The tool find is integrated in every current linux distribution normally, so no need to install. Here are some helpful commands for daily usage: Find all files (-type f) named test.txt: find /home -name "test.txt" -type f Find all folders (-type d) named testdir below /home: find /home -name "testdir" -type d Find all links (-type l), […]