Python for Network Engineers

Rename Files From 1.Name or Name1 to 01Name

By George El. • January 2019 • Reading time: 3 minutes

there is nothing more annoying than seeing unordered files, and this happens a lot when you have numbers in front of files like 1.file, 2.file etc. the command is ls -1 | sed -r -e 's/([a-zA-z]+)([0-9]+)(\.txt)/\0 \2 \1 \3/' | xargs -n4 printf "%s %02d%s%s\n" | xargs -n2 mv and we will break it down

Find the Total Size of All Files With Certain Extension with one Line

By George El. • January 2019 • Reading time: 2 minutes

find all files with a certain extension in current directory and add the file sizes to calculate total file size in Mbytes find . -iname "*.txt" | xargs ls -l | awk '{print $5 }' | paste -sd+ | bc | awk '{print $1/1024/1024 }' the above example finds all files ending in txt, starting from the current directory.

Using Linux Xargs

By George El. • October 2018 • Reading time: 4 minutes

xargs is very useful utility because it takes standard input and passes it to the next function. In most cases it can replace a for loop lets say I wanna zip each txt file1-9 individually $ls -1 *.txt | xargs -n1 gzip file1.txt.gz file2.txt.gz file3.txt.gz file4.txt.gz file5.txt.gz file6.txt.gz file7.txt.gz file8.txt.gz file9.