Hi everybody!

Help me please to figure out the comand that I need to use to find a file and open it at same time

for example:

root@serg-PORTEGE-Z835:~# locate bash_profile
/usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bash_profile

and lets say I want to open this file, but I don't want to use cd and go all the way down to that file.

Is ther any easy and short way to do this?

Dani AI

Generated

— the answers from and are on the right track: a full filesystem search will find everything, and less is great for quick viewing. Below are practical, safer ways to "find and open" without cd, plus a few caveats.

A fast way when you have locate (it uses a file DB and is very quick) is to open the first match in your preferred editor. This example uses the $EDITOR variable with a vi fallback. Note that locate relies on an indexed DB; run updatedb or wait for its periodic update if you need very recent files (locate man page, ).

file=$(locate -n 1 bash_profile)
[ -n "$file" ] && "${EDITOR:-vi}" "$file"

When you need up-to-the-second results (no DB), use find and handle filenames safely (spaces, newlines) with null terminators. Restrict the start path to avoid long scans (for example /home instead of /), and run with appropriate privileges if some paths are inaccessible (find manual, xargs man).

find /home -type f -iname 'bash_profile' -print0 | xargs -0 -r ${EDITOR:-vi}

For interactive selection use an interactive picker like fzf (shows matches, you pick one) and then open it with your editor (fzf). Also note ’s tip: less is handy for quick inspection and lets you press v to edit the current file in $EDITOR.

Recommended Answers

All 4 Replies

find / -name 'bash_profile' -exec cat {} \;
Beware you might get more than one file

how about if the file that I'm looking is a text file and I want to open it in vi editor?

Just replace the cat with vi.

I usually use the "less" command, which will let me look at the file, and optionally edit it if I want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.