If I am in
C:\WINDOWS\Resources\Themes
and I have a file in an other directory called
to which I want to go but I do not know the path to it . Using a Linux command , is there a way to go directly to it without knowing its.
If I am in
C:\WINDOWS\Resources\Themes
and I have a file in an other directory called
to which I want to go but I do not know the path to it . Using a Linux command , is there a way to go directly to it without knowing its.
You would use the find
command ..
find some_top_level_directory -name example.txt
What do you mean by "go there"? Edit the file? Run the file? Use it some other way? What way?
As there may be many files with that name, Scrutinizer's proposal will show them all but won't "go there". You can edit a file in any path immediately like
vi path/shown/by/Scrutinizers/proposal/example.txt
Try Below
find ./* -type f -iname "source_file.txt" -exec dirname {} \; 2> /dev/null | cd `awk '{print $0}'`
This might work on some systems using some shells, but there are a few problems:
find ./* ...
can give you argument list too long errors when invoked in directories containing lots of files and/or several files with long filenames. It would be better to use find . ...
.source_file.txt
in more than one directory in the file hierarchy rooted in the current working directory, you will be invoking cd
with two or more operands instead of the single operand you're assuming will be specified.cd
is executed in a subshell environment, the current working directory of the shell execution environment you will be in when the pipeline completes will not have been changed.If the intent is to move into the directory containing a file named example.txt
and do something in that directory for each directory that contains a file by that name, something more like:
#!/bin/ksh
spot=$PWD
find . -name 'example.txt' | while read -r path
do file=${path##*/}
dir=${path%/*}
if ! cd "$dir"
then continue
fi
printf 'Process file "%s" in directory "%s"\n' "$file" "$dir"
# do whatever you want to do in this directory...
cd "$spot" # return to the directory in which we started
done
This was written and tested using a Korn shell, but this should work with any shell that performs the basic parameter expansions specified in the POSIX standards.