3

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?

Radu Rădeanu
  • 169,590
Ubuntuser
  • 9,816

3 Answers3

7

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

Radu Rădeanu
  • 169,590
  • Thanks. But i am actually trying to compress all csv files into the same folder by going inside each folder. – Ubuntuser Sep 05 '13 at 13:36
  • @Ubuntuser And where you said something about this in your question? If you don't see clear, the question is: how to check whether a file is in current directory or subdirectory and if its in a subdirectory, my script should descend to that directory – Radu Rădeanu Sep 05 '13 at 13:38
  • @Ubuntuser If you have another question, please use Ask Ubuntu button. – Radu Rădeanu Sep 05 '13 at 13:41
  • I have asked the question http://stackoverflow.com/questions/18649393/find-and-gzip-files-in-subdirectories. See if you can answer it there :) – Ubuntuser Sep 06 '13 at 03:25
  • Also it will not work. dirname ./2012/t1.csv will give you ./2012. you cannot cd into that folder – Ubuntuser Sep 06 '13 at 04:53
  • @Ubuntuser It's working like a charm. See here. I told you that you may have a problem with your bash/terminal :) ...Maybe some broken settings. – Radu Rădeanu Sep 06 '13 at 05:21
  • Check this script please #!/bin/bash

    for file infind . -iname *.csv`do` `echo $file` `if [[ ! -f $file ]];then` `echo "Not in this directory, changing directory" `pwd cd $(dirname $file)

    fi done This doesnt change directory

    – Ubuntuser Sep 06 '13 at 05:39
  • 1
    If you mean that directory won't be changed /after/ script execution, this is right. The script is runing in a subshell, and cannot change the parent shell working directory. Replace pwd with comand you want to run inside new directory or source script this way: http://askubuntu.com/questions/84279/how-to-change-directory-using-script – sorgel Sep 06 '13 at 05:51
3
if [ -f t1.csv ]
then
    echo "File is in target dir"
else
    cd `dirname $(find . -iname 't1.csv')`
    pwd
fi
sorgel
  • 439
  • I dont think it helps. you see, find command gives the path. So if i do an if statement on the file, it will always find it. I need to check if this file is present in the current directory. – Ubuntuser Sep 06 '13 at 04:45
  • Yes it does: all you need to do extra is check if it has more than 1 backslash ;) (./111/111 versus ./1111) – Rinzwind Sep 06 '13 at 05:28
  • Rinzewind, please tell me more about why more then 1 backslash is needed? – sorgel Sep 06 '13 at 06:03
  • No, find do not test where the command [ -f t1.csv ] does, as can be seen in the code. – MUY Belgium Mar 10 '14 at 14:03
1

There's a pretty comprehensive discussion on it here:

... see also the grep man pages of course

Not sure about the 2nd part of your question though, but hope that sends you in the right direction.

Mr.President
  • 359
  • 1
  • 3
  • 12