Skip to content

CowSmiles/fastlocate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Table of Contents

fastlocate

fast locate a file or directory using locate command in linux.

The script can get the results according given keywords using locate command. When there is only one result, it will output only one path match the keywords. While reading a input when there are many results.

Combine the script and bash, we can do a lot of fast moves.

depends

the script depends the following software:

  • python3
  • mlocate

usage

usage: fastlocate.py [-h] (-d | -f) [-l LIMIT] [-i IGNORE]
                     keywords [keywords ...]

locate the file using locate command in linux.

positional arguments:
  keywords              keywords

optional arguments:
  -h, --help            show this help message and exit
  -d, --dir             to find directory.
  -f, --file            to find file.
  -l LIMIT, --limit LIMIT
                        the maximum number of the results.
  -i IGNORE, --ignore IGNORE
                        ignore the paths containing the given word.

Example

Search the directory whose base name is "test" and also whose path contains "Dropbox":

fastlocate.py -d test Dropbox

Because there is only one result. So it outputs:

/home/klniu/Dropbox/code/go/test

Search the directory whose base name is "ldar" and also whose path contains "Dropbox":

fastlocate.py -d ldar Dropbox

Because there are many results. So it outputs a menu for you to select the right item:

1) /home/klniu/Dropbox/code/python/ldar
2) /home/klniu/Dropbox/code/python/ldar/system/ldar
3) /home/klniu/Dropbox/documents/ldar
Please select the directory(default=1, "q" for quit):
input a integer and the script will output the right path. Press enter to select the first result, or "q" to quit.

Awesome

Combile the script and bash, we can move around fast:

put the fastlocate.py in some place, such as ~/bin, and:

chmod +x ~/bin/fastlocate.py

put the following functions into ~/.bashrc, we can do a lot of thing:

# cd around 
function lcd(){
    result=`~/bin/fastlocate.py -d $@|tee /dev/stderr`
    if [[ ! -z $result ]]; then
        cd $result
    fi
}

# vim a file around
function lvim(){
    result=`~/bin/fastlocate.py -f -i % $@|tee /dev/stderr`
    if [[ ! -z $result ]]; then
        vim -p -g --servername GVIM --remote-tab-silent $result
    fi
}

# krusader a file around
function lfm(){
    result=`~/bin/fastlocate.py -d $@|tee /dev/stderr`
    if [[ ! -z $result ]]; then
        krusader $result
    fi
}

# open a file using xdg-open
function open(){
    result=`$CODE/fastlocate/fastlocate.py -f $@|tee /dev/stderr`
    if [[ ! -z $result ]]; then
        xdg-open $result > /dev/null 2>&1 
    fi
}

In terminal, input:

lvim .bashrc /home
to edit .bashrc

lcd Dropbox
to change directory to Dropbox

lfm test /home
to open the test directory which locates in /home in krusader.

You can create your own functions to extend the script.

Tips

You can edit the /usr/lib/systemd/system/updatedb.timer to update the database of mlocate every hour.

[Unit]
Description=Daily locate database update

[Timer]
OnBootSec=10min
OnUnitActiveSec=1h
Persistent=true

About

fast locate a file or directory using locate command in linux

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages