In a shell script how would I find a file by a particular name and then navigate to that directory to do further operations on it?
From here I am going to copy the file across to another directory (but I can do that already just adding it in for context.)
You can use something like:
This will locate the first
lsregular file then change to that directory.In terms of what each bit does:
findwill start at/and search down, listing out all regular files (-type f) calledls(-name ls). There are other things you can add tofindto further restrict the files you get.| head -1will filter out all but the first line.$()is a way to take the output of a command and put it on the command line for another command.dirnamecan take a full file specification and give you the path bit.cdjust changes to that directory, the--is used to prevent treating a directory name beginning with a hyphen from being treated as an option tocd.If you execute each bit in sequence, you can see what happens: