iainheng
3/28/2019 - 4:34 PM

Useful linux commands

Useful linux commands

Use Grep to Find Files Based on Content

The find command is only able to filter the directory hierarchy based on a file’s name and meta data. If you need to search based on the content of the file, use a tool like grep. Consider the following example:

find . -type f -exec grep "example" '{}' \; -print

This searches every object in the current directory hierarchy (.) that is a file (-type f) and then runs the command grep "example" for every file that satisfies the conditions. The files that match are printed on the screen (-print). The curly braces ({}) are a placeholder for the find match results. The {} are enclosed in single quotes (') to avoid handing grep a malformed file name. The -exec command is terminated with a semicolon (;), which should be escaped (\;) to avoid interpretation by the shell.

Before the implementation of the -exec option, this kind of command might have used the xargs command to generate a similar output:

find . -type f -print | xargs grep "example"

Remove all files except extension

/bin/rm -f -- ^*.php

Uninstall nodejs

sudo apt-get remove nodejs

sudo apt-get remove npm

Then go to /etc/apt/sources.list.d and remove any node list if you have. Then do a

sudo apt-get update