pydata

Keep Looking, Don't Settle

linux function find, grep, locate, which and dkpg

The question comes from an issue I got: I have installed MySQLdb but when I try to import MySQLdb, it fails with error message that MySQLdb cannot be found. The steps to check:

  1. make sure the package is installed
  2. check where it is installed
  3. add the path to PYTHONPATH

Then import it again, it works.

All these will be very easy if you are familiar with linux commands. It will be very convenitent and helpful in job if you master some linux functions. This blog is to list the useful function and its usage

1. find function and its usage

# search multiple dirs
find /opt /usr /var -name foo.scala -type f     

# fina all cache, xml and html files
find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)   

# find and search, ignore case with -i option
find . -type f -name "*.java" -exec grep -il string {} \;

# find and copy, cp *.mp3 files to /tmp/MusicFiles
find . -type f -name "*.mp3" -exec cp {} /tmp/MusicFiles \;     

# list user and corresponding disk usage
find /data01/saswork -printf "%u  %s\n" | awk '{user[$1]+=$2}; END{ for( i in user) print i " " user[i]}'   

# how to list all my files, and size > than a limit, in the given directory
for r in $(find /data01/ -type f -user hsong| grep "^\/data01"); do ls -la $r;done|cut -d " " -f 3,5,10
for r in $(find /data02/temp/temp_sasprod/ -type f -user hsong| grep "^\/data02"); do ls -la $r;done|cut -d " " -f 3,5,10
for r in $(find /data02/temp/temp_sasprod/ -type f -size +500k -user hsong| grep "^\/data02"); do ls -lSR $r;done | less

2. locate to find where the function is

locate python

3. dpkg to check packages installed or not, and the path

  • dpkg -L PackageName to find where the package is
dpkg –L MySQLdb
  • check if the package is installed or not
dpkg --get-selections | grep mysql

Reference

  1. A collection of Unix/Linux find command examples