1

Hi i was trying to make a script with fzf to cd into a dir by searching name of a file in that directory

fcd(){
    cd $(find | fzf ) 
}

but it will throw error when filename contains space, as it will split it into 2.


EDIT: Thanks

cd "$(...)", not cd $(...); the >quotes are mandatory. – Charles Duffy

so Can anyone help with such a script to cd into directories with file names using fzf?


Actual question before edit V

after so much searching and modifying, i ended up with this

cd $(fzf| awk '{print "\047"$0"\047"}'|xargs dirname | awk '{print "\047"$0"\047"}')

take file tree

a
└── b
    └── c d
        ├── e.txt
        └── f g.txt

if we change cd with echo in above script and search for 'f g.txt', it will correctly print out 'a/b/c d' and if we type cd 'a/b/c d', it is going to that dir. but when combine both and run like above script, is giving

bash: cd: too many arguments

whereever i search, i get solution as cd $(find . -d |fzf) which is not i needed, i wanted to search a filename and go to it's parent directory.

Thanks in advanced :)

Vaisakh K M
  • 105
  • 1
  • 7
  • 1
    `cd "$(...)"`, not `cd $(...)`; the quotes are mandatory. – Charles Duffy Jun 22 '22 at 17:29
  • And no, adding literal quotes to your data cannot and will not substitute for missing syntactic quotes, so the `awk`ery makes no sense. – Charles Duffy Jun 22 '22 at 17:30
  • To be a little more explicit about how to apply the comment above: `fcd() { cd "$(find | fzf)"; }` – Charles Duffy Jun 22 '22 at 19:15
  • 2
    Or, to deal with moving to the parent directory after having a file identified: `fcd() { local target; target="$(find . -type f -print | fzf)" || return; cd "${target%/*}"; }` – Charles Duffy Jun 22 '22 at 19:17
  • Thanks, this was exactly what i was looking for can you post as an answer, so you will get reputation – Vaisakh K M Jun 22 '22 at 20:27
  • No, a closed question cannot be answered. Anyhow, it's just reiterating what the two duplicates teach (one showing why/when it's necessary to add quotes, the other showing how to get the directory containing a file). – Charles Duffy Jun 22 '22 at 20:30

0 Answers0