Ubuntu – How to check whether a file is in current directory or its subdirectories

bashscripts

I use find to search for files in a directory or its subdirectories.
find . -iname '*.csv', and it gives me the following output

./2012/t1.csv
./2012/t2.csv
./2013/t1.csv

Now, I want to check in a script whether the file t1.csv is in the current folder or in one of the subdirectories. If it's in a subdirectory, my script should descend to that directory.
How can I do that?

Best Answer

To check only for t1.csv, you can use:

find . -name 't1.csv' 

Now, to go in the first directory where t1.csv file is found, you can use:

cd $(dirname $(find . -name 't1.csv' 2>/dev/null | head -1)  2>/dev/null)

enter image description here

Related Question