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.
the script depends the following software:
- python3
- mlocate
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.
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.
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 /hometo edit .bashrc
lcd Dropboxto change directory to Dropbox
lfm test /hometo open the test directory which locates in /home in krusader.
You can create your own functions to extend the script.
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