The command you are looking for is find
. It will traverse an arbitrarily complex directory structure and report back all files which match the criteria you specify.
find . -name 1.txt -exec dirname {} \;
This runs the dirname
command on each matching file, which prints just the directory name.
If you only want to go one directory deep, GNU find
has a -maxdepth
option, or you can just use the shell's wildcard matching facilities.
dirname */1.txt
This assumes the file exists directly in a subdirectory of the current directory, and will only work if there is exactly one wildcard match (dirname
only works on one file at a time) and the various workarounds are not a lot more elegant than just using find
.
If, on the other hand, you know that 1.txt
exists in the current directory, the command pwd
will print out the directory name.
The command grep Directory 1.txt
will open the file 1.txt
in the current directory, and print out any lines matching the text Directory
. It will simply ignore the standard input you are feeding it from the ls
command, which is just as well, because ls
does not print "Directory" anywhere useful. Also, parsing ls
output is very complex, so you should probably avoid trying.