18.01.2020 Views

Working with Linux

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In order to do this, we combine find with wc -l:

find . -iname "*.js" -exec wc -l {} \;

This will give us all js files and the number of lines. We can pipe this to cut:

find . -iname "*.js" -exec wc -l {} \; | cut -f 1 -d ' '

To only output the number of lines, and then pipe to the paste command, we do this:

find . -iname "*.js" -exec wc -l {} \; | cut -f 1 -d ' ' | paste -sd+

The above will merge all our lines with the + sign as a delimiter. This, of course, can translate to

an arithmetic operation, which we can calculate using the binary calculator (bc):

find . -iname "*.js" -exec wc -l {} \; | cut -f 1 -d ' ' | paste -sd+ | bc

This last command will tell us how many lines our javascript files contain. Of course, these are not

actual lines of code, as they can be empty lines or comments. For a precise calculation of lines of

code, you can use the sloc utility.

In order to mass rename files, like changing the file extension name to node for all js files we can

use this command:

find . -type f -iname "*.js" -exec rename "s/js$/node/g" {} \;

You can see the rename syntax is quite similar to sed. In addition, there are no more .js files left, as

all have been renamed to .node:

Some software projects require all source code files to have a copyright header. As this is not

required in the beginning, often times we can find ourselves in the situation that we have to add

copyright information at the beginning of all our files.

In order to do this, we can combine find with sed like this:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!