I have a ~/GitHub dir, which I can locate with find GitHub. I have a default installation of fzf, which uses find. When I run fzf on the home dir, fzf doesn't find GitHub dir.
If I pipe so: find GitHub | fzf it shows the dir in the results.
What gives?
Related
I run windows command in particular directory and I need to pass files and subdirectories in this directory with full path. How to retrieve full path of current directory? What is analog of bash $PWD?
$pwd is indeed a valid command in PowerShell as well (if you're using PowerShell). $pwd in PowerShell is basically an alias for Get-Location - both of them will give you the full path of current directory.
In case you are using cmd, use cd (just cd without any parameters)
I often use fzf to navigate the filesystem, especially the Alt-c key binding.
When invoked, fzf generates a list from the current working directory.
Is it possible to make fzf generate a list from a specified directory?
I have tried fzf <dir>, but it results in an error (unknown option). Also, I can't find any options like -C <dir> for specifying the start directory.
I had a more general issue which might be useful for you. The following is from a blog post I wrote about it:
Configuring FZF to search useful directories beyond the working directory
I use fzf both as a command line tool and from within Vim using the fzf.vim plugin. It makes finding (and opening) files intuitive, fast, and frees you from needing to remember their location or exact name. By default, fzf searches recursively within the current directory, which is often just what you want. If you need to search for a file in some directory beyond the current working directory you need to specify that path as an argument to fzf, after which it's business as usual (fzf will recursively search the specified directory).
The Problem
It always felt a shame to have to occasionally precisely specify a path in order to get a fuzzy search going... precisely specifying a path is the exact thing that fzf is supposed to unburden your from! My initial approach was to supply the home directory path and let fzf search everything, the home directory path can be specified in only a couple of characters so there's no real burden in that case.
The problem with doing this is that you end up searching a lot of directories which you know don't have the file you want. The main offenders were directories you end up with if you install say, anaconda3. The results would be swamped with thousands of internal files, with very long paths. The long paths tended to 'soak up' any letters I entered in the search, so it was difficult for fzf to filter them out.
The Solution
You can choose which searching tool fzf uses under the hood. The default is the standard linux find command, but you can also use fd, ripgrep or silver searcher. Apart from being a lot faster than the default find, these latter tools respect .ignore files. This means that fzf will skip any files or directories listed in a .ignore file. We can turn this feature to our advantage.
First, we install fd. If you run Ubuntu 19.04 (Disco Dingo) or newer, you can install the officially maintained package:
sudo apt install fd-find
If you use an older version of Ubuntu, you can download the latest .deb package from the release page and install it via:
# adapt version number and architecture
sudo dpkg -i fd_8.2.1_amd64.deb
Now we configure fzf to use fd by adding the following line in your .bashrc:
export FZF_DEFAULT_COMMAND="fdfind . $HOME"
If you're using an older version than Ubuntu 19.10, the above line needs to be modified like so:
export FZF_DEFAULT_COMMAND="fd . $HOME"
Now fzf will always search recursively from the home directory, and respect any .ignore files. So let's make one in the home directory:
touch ~/.ignore
I find that the list of directories that I might conceivably (~15) want to recursively search with fzf is shorter that the list of directories that I would never want searched. The total number of files in the directories I want searched is about 5000 or so - easily handled by fd.
In the .ignore file, I first list all my home directories, each followed by a '/':
# start by igoring every home directory
anaconda3/
arch/
cache/
code/
Desktop/
.
.
.
Then below those, put the directories that you want to be searched, each preceded by a '!' and followed by a '/':
# now un-ignore the ones I care about
!code/
!Desktop/
!documents/
!downloads/
.
.
.
The '!' will 'cancel out' the previous ignore commands.
And there we have it. We can invoke fzf wherever we are in the file system and start typing vague things about the file(s) we have in mind and fzf will search in a set of predefined directories and find it with ease. This completely removes the barrier of thinking where a file might be and how precisely it was named.
N.B. I have noticed that, for some reason, a couple of subdirectories were not showing up in the fzf search, and so I explicitly created some '!path/to/missed/directory/' lines in this section...
N.B. You may be wondering "What if I find myself in an unusual directory not on the list, and want to use fzf?". I had the same concern so I put a couple of aliases in my .bashrc that can toggle the above configuration on and off at will (be sure to use 'fdfind' for Ubuntu 19.10+, as disused above):
# restore fzf default options ('fzf clear')
alias fzfcl="export FZF_DEFAULT_COMMAND='fd .'"
# reinstate fzf custom options ('fzf-' as in 'cd -' as in 'back to where I was')
alias fzf-="export FZF_DEFAULT_COMMAND='fd . $HOME'"
If you're using Vim to create the .ignore file, an easy way to get a list of all the directories in your home directory is the following command:
:.!ls ~/
Append a '/' to all lines by putting the cursor on the first directory in the list and entering the following command:
:.,$ norm A/
Similar to above, insert the '!' before each one by putting the cursor on the first directory in the list and entering the following command:
:.,$ norm I!
Assuming you're using bash or similar, this is built into the default completion options which get installed with fzf: https://github.com/junegunn/fzf#fuzzy-completion-for-bash-and-zsh
tldr; enter start file or directory, append ** and hit Tab. So if you'd enter cd /foo/** then tab opens fzf with /foo as start directory.
edit at the time of writing the commands for which this works are hardcoded in fzf's bash helpers, which is why this works for cat and cd but not for tac or nano. This is the complete list:
awk cat diff diff3
emacs emacsclient ex file ftp g++ gcc gvim head hg java
javac ld less more mvim nvim patch perl python ruby
sed sftp sort source tail tee uniq vi view vim wc xdg-open
basename bunzip2 bzip2 chmod chown curl cp dirname du
find git grep gunzip gzip hg jar
ln ls mv open rm rsync scp
svn tar unzip zip
To add other commands use this in e.g. your .bashrc, after the place where fzf gets sourced (something like [ -f ~/.fzf.bash ] && source ~/.fzf.bash):
__fzf_defc "tac" _fzf_path_completion "-o default -o bashdefault"
Alternatively: open an issue to ask for the commands you want to be added to fzf by default, things like tac and nano are super common anyway.
You can do something like:
find <dir> | fzf
fd . <dir> | fzf
Source.
I would like to find the location of a Git repository I made on my mac. Is there a way to find, for exemple, albatrocity/git-version-control.markdown on macOS using the Terminal? I installed everything with default parameters. I guess it must be in the User directory but I don't find anything related to GitHub there.
When I find it, I would like to completely remove it to maker a "proper" install.
EDIT: sudo find / -name "parsing.py" -print
I used a file that I know the folder contained when Terminal showed me nothing with sudo find / -wholename "*albatrocity/git-version-control.markdown"
You can use find's -wholename option to find a file based on its name and folder:
find <directory> -wholename "*albatrocity/git-version-control.markdown"
Example, if you want to search in the /Users/ directory:
find /Users/ -wholename "*albatrocity/git-version-control.markdown"
If you have locate on mac, and a regularly running updatedb, locate might be much faster:
locate albatrocity | grep git-version-control.markdown
It uses a hashtable to fast access filenames, but can be out of date, if the database isn't updated regularly or the file is too young (typically less than one day old).
If this is without success, then I would go for a full search with find, but maybe restrict it to a possible, narrowed path.
I have a find expression looking for node_modules directories. Some of the results are node_modules directories within a parent node_modules directory.
I'm only interested in finding the top most node_modules directory and performing an action on it, i.e. add a .metadata_never_index file so that Spotlight (macOS) doesn't index it.
I'm struggling to find a solution which deals with this; can find do this for me, or do I need to calculate it manually (maybe through string manipulations)?
As a bonus, I'd like to ignore all folders which already contain the .metadata_never_index file, if possible.
EDIT: Some context; this will run as a cron job and periodically look for node_modules in my ~/Sites directory, i.e multiple top-level projects.
After a bit more searching, I found a solution on SuperUser.
To summarise Paxali's answer:
find /home/code -type d -name ".git" | grep -v '\.git/'
In english: find me all directories named ".git" and filter out any occurences in the resultlist which contain ".git/" (dot git slash).
Except in my case, I used node_modules/ instead of .git/.
My working code:
find -f ~/Sites . -name node_modules | grep -v 'node_modules/' | while read fname; do
touch $fname/.metadata_never_index
done
Find the top most modules can be found by using
npm command with flag --depth
npm list --depth=0
you can also create aliases as follows:
alias ng="npm list -g --depth=0 2>/dev/null"
alias nl="npm list --depth=0 2>/dev/null"
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you remove Subversion control for a folder?
Command line to delete matching files and directories recursively
I recently converted my cvs repository to svn using cvs2svn and I recently noticed that every directory has a hidden folder called .svn. My current build script copies a lot of directories from my versioned resources directories and it ends up copying the .svn files. Is there anyway to make svn not include these files when I checkout or do I need to write a script to delete all these .svn files. There are many files that have these hidden .svn directories so this would be a pain unless I could write a recursive script to do this but I don't if I can do this for my windows installer. Is there an easy way to stop svn from putting this hidden directory everywhere in my project?
I'm not sure in your specific case that you want to remove all those .svn directories. But if you do, here is a bash one-liner to do just that:
find . -name .svn -print0 | xargs -0 rm -r
You can do a thing called an SVN Export to get the files without the .svn directories
http://svnbook.red-bean.com/en/1.7/svn.ref.svn.c.export.html
I posted this yesterday over here, but here is again because I kind of put it in the wrong thread anyway...
I've got something that should make your day. Original source is here.
This is a (perfectly safe) Shell Extension that will add "Delete SVN Folders" to your right click menu in Windows. Run it on any directory containing those pesky files.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN]
#="Delete SVN Folders"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN\command]
#="cmd.exe /c \"TITLE Removing SVN Folders in %1 && COLOR 9A && FOR /r \"%1\" %%f IN (.svn) DO RD /s /q \"%%f\" \""
To make this part of your build script copy that call to cmd.exe and execute it.
find . -name '.svn' -depth -exec rm -rf '{}' \; -ls
on Win32/Win64 systems, the following command should do the job:
del /q /s .svn
Those folders are required for how subversion works with a working copy (i.e. where you've done a checkout).
One option would be for you to do an export to another location. The export would not have the .svn folders, and you could run your script on that. Documentation: svn export, TortoiseSVN Export
Another option would be to modify your script to ignore hidden directories, or build a better build tool.
And a PowerShell version
ls -Force -Recurse -Filter .svn | rm -Force -Recurse
svn export is what you want. It will give you a clean copy of the code tree without the .svn directories (note that this copy is not under version control and svn commands won't work on it once it's exported).
I utilize this method to launch code on production servers. Our build script takes an export of the code branch, tars and gzips it, uploads it to the correct server, and unzips/untars it.
If wanna do this with ruby just paste my script at the root folder you want to remove recursively: http://fabianosoriani.wordpress.com/2009/03/19/ruby-script-to-remove-svn-folders/
You could take a look at SVK, which is a layer on top of Subversion. One of the advantages is that the repository metadata for your working copy, which is normally stored in the .svn dirs, is instead kept in a single central location, so you don't have the ugly hidden .svn dir problem. It's pretty nice.
find "/YourFolder" -name ".svn" -exec rm -fdR {} \;
Check this
link
I dont know if I would delete them, but maybe modify your build script to ignore copying them would be a good idea. I use Ant to build compile and build my war and it does so ignoring .svn dirs.