LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Filer out binaries (https://www.linuxquestions.org/questions/linux-newbie-8/filer-out-binaries-154982/)

teeno 03-08-2004 10:15 AM

Filer out binaries
 
Hi All,

This is probably the easiest question every. But here goes...

If the I have a directory the contains .c .o and binaries how do I filter out the .c .o files so that I can see only the binaries (they have on extension)

I can do:
Code:

ls *.c
To see just the .c files and the same to get just the .o files or even .? to get both the .c and .o

I also need to copy just the binaries to a different directory. How can I do this using the cp command?

The DOS commands would be:
Code:

dir *.exe
copy *.exe c:\temp

Thank you,

Pete

gregaryh 03-08-2004 10:18 AM

Same goes for Linux:

cp *.c /new/dir

teeno 03-08-2004 10:34 AM

Yes but the binaries don't have an extension.

eg.

file.c
file.o
file (no extension)

teeno 03-09-2004 04:34 AM

Should I conclude that this can't be done?

hw-tph 03-09-2004 05:09 AM

Code:

for file in *
do
        if [ -x "$file" ]
        then
                echo "$file is executable"
        fi
done

You could replace the echo with whatever you want - like a move command:
Code:

for file in *
do
        if [ -x "$file" ]
        then
                mv "$file" ~/tmp
        fi
done

To use this on the command line (without putting it in a script) you would have to use semicolons to break the lines: for file in *; do if [ -x "$file" ]; then echo "$file is executable"; fi; done

This requires the executables to be executable by the user the script runs as, since that's what the -x bash test relies on.


Håkan

slakmagik 03-09-2004 05:11 AM

Rather than trying to mess with the names, and going on the principle that they're executable and nothing else but directories are, this might work. No guarantees, but I tested it briefly and it seemed okay. Though once a couple of my files disappeared - think it was just a typo when I changed the destination. :D

Code:

for f in `ls .`; do if [ -x $f -a \! -d $f ]; then mv $f /target_dir/; fi; done
Gotta be an easier way that I'm just going braindead on. But there are strange limitations in 'ls' and the 'mv', 'cp', etc. commands.

hw-tph 03-09-2004 05:12 AM

I beat ya to it but you beat me on the directories. :)
I really should have thought of that.


Håkan

slakmagik 03-09-2004 05:15 AM

Yeah - I'm just relieved to see a similar approach. :) Seems like there ought to be a more direct way.

teeno 03-09-2004 05:15 AM

Ok. Thanks for your help.

I think I will just copy all the files and delete the .c and .o files using rm *.c


Thanks
Peter


All times are GMT -5. The time now is 10:42 AM.