unix: execute gnuplut in different directories using find | xargs - unix

I'm pretty new to using the shell. I have many gnuplot scripts in subdirectories. These include relative paths of the input files. So to execute them in the right way I have to go to the parent directory of the script and then execute it. Is there a way to do this with a find | xargs combination?
find . -name "*gpf" | xargs -i{} dirname {}
is something that I found so far. But I don't know how to go now to the parent directory and then exegute gnuplot right there. Is there a way to do this?
Many thanks.
Edit: Anyone else having any ideas that could help me with this issue? Would really help me a lot! Thanks in advance.

You can try to execute gnuplot with the -exec option of find:
find . -name "*.gpf" -exec gnuplot \{\} \;

Use GNU Parallel:
find . -name "*gpf" | parallel "cd {//};gnuplot {/}"
Learn more about GNU Parallel: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
10 second installation:
wget pi.dk/3 -qO - | sh -x

Related

How can I make this find command work recursively in the current directory?

I have this find command and need to make it work recursively in the current directory ( at the moment it searches all the files on the disk )
find . -name ‘OldName*’ -print0 | xargs -0 rename -S ‘OldName’ ‘NewName’
Any idea how to make it search in the current directory that I am navigated to in terminal ?
Would this work?
find . -name 'OldName' -exec rename -n 'OldName' 'NewName' \;

bzgrep not printing the file name

find . -name '{fileNamePattern}*.bz2' | xargs -n 1 -P 3 bzgrep -H "{patternToSearch}"
I am using the command above to find out a .bz2 file from set of files that have a pattern that I am looking for. It does go through the files because I can see the pattern that I am trying to find being printed on the console but I don't see the file name.
If you look at the bzgrep script (for example this version for OS X) you will see that it pipes the output from bzip2 through grep. That process loses the original filenames. grep never sees them so it cannot print them out (despite your -H flag).
Something like this should do, not exactly what you want but something similar. (You could get the prefix you were expecting by piping the output from bzgrep into sed/awk but that's a bit less simple of a command to write out.)
find . -name '{fileNamePattern}*.bz2' -printf '### %p\n' -exec bzgrep "{patternToSearch}" {} \;
I printed the file name through echo command and xargs.
find . -name "*bz2" | parallel -j 128 echo -n {}\" \" | xargs bzgrep {pattern}
Etan is very close with his answer: grep indeed does not show the filename when only dealing with one file, so you can make grep believe he's looking into multiple files, just by adding the NULL file, so the command becomes:
find . -name '{fileNamePattern}*.bz2' -printf '### %p\n'
-exec bzgrep "{patternToSearch}" {} /dev/null \;
(It's a dirty trick but it's helping me already for more than 15 years :-) )

UNIX get info about file in all directories matching a pattern

I have a bunch of directories that all contain a file /SubDir1/SubDir2/File, and I want to see the memory of each file under directories matching a certain pattern. How do I do this?
So far I have ls -l | grep "pattern* to get a list of the directories, but am stuck at this.
You should use the find command:
find . -name 'pattern*' -printf '%s\t%p\n'
By "memory of each file" I guess you mean file size.
The find command will do a better job:
find . -name "pattern*" -exec du -b {} \;
This will print the file size of every file named File in your arborescence along with the file path.
Bash Pitfall #1: Don't parse ls
You can use find or shell patterns:
for i in pattern*; do
cat "$i"
done
One of your special problems is to get a list of all files under a set of matching directories, and you can do that with a more elaborate pattern:
for i in pattern*/*; do
if [ -f "$i" ]; then
cat "$i"
fi
done
In addition to what SirDarius said, you can also use the -R option to ls to get a recursive listing.
Something like ls -lRh | grep "pattern" should do what you want.

Unix delete all folders in a directory with a specific name

Hi I've got lots of folders with the name "#eaDir" all across one of my disks and I'd like to search through, find all of them and delete them and their contents.
I know this is probably a combination of the find and rm command but I can't quite figure them out. Can anyone help?
Try this:
find . -type d -name '#eaDir' -print0 | xargs -rt0 rm -rv
Here's the same thing but using explicit long options for xargs:
find . -type d -name '#eaDir' -print0 | xargs --no-run-if-empty --verbose --null rm -rv
(using long options is always a good idea if you're writing scripts that will need to be maintained/reviewed by other people)
But before anything else:
man find
man xargs
find /path/to/the/disk -type d -name "#eaDir" -delete
Notice that the order here is fundamental: quoting the manpage,
Warnings: Don't forget that the find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.
So, as always, first try your find command with -print, then, when you checked that everything works fine, replace it with -delete. Notice that -delete implies -depth, so, to do meaningful testing with -print, you should explicitly specify it in the expression:
When testing a find command line that you later intend to use with -delete, you should explicitly specify -depth in order to avoid later surprises.
Goto root folder or directory and execute the following command:
find . -path '*/#eaDir/*' -delete -print && find . -path '*/#eaDir' -delete -print
This should work for you.

How do I get the 'find' command to skip over the files in the .git/ directory?

How can I get the Unix find command to omit the files in the .git/ directory? I've been using
find . | grep -v .git
but I'm wondering if there is a better way.
Use pruneoption:
find . -type d -name .git -prune -o -print
Or better yet, use ack to do grep like searches.

Resources