Bash Script Help

Associate
Joined
28 Jun 2005
Posts
1,320
Can anyone help with a bash script I need to write? I've found various methods on the net but can't seem to get the two together. I need to search a directory, and for every file it finds in the root directory, do something. I can't work out how to get the script to only concentrate on files, not directories, and only in the root directory, and not worry about sub-directories beneath the root.

I've worked out each of the criteria but can't work out how to put the two together.
 
The option ' -maxdepth 0 ' for find will stop recursion. Similarly -type f will make it ignore directories.

What do you want to do with the files? I'm quite fond of the for, do loop in bash, e.g.

Code:
for i in *.doc;
do cp $i doc/ ;
done

The colons are probably optional unless typing it all on one line. The following is working here

Code:
for i in `find *.doc`; do cp $i backup/ ; done

The apostrophe things need to be slanting in that direction. Any use?

Man find suggests
find . -type f -exec file '{}' \; which may be more suitable. Also might mean my above code needs a dot after find to behave itself.
 
Last edited:
Code:
find . * -type f -maxdepth 0

I doubt thats the best solution, but it'll output a list of every file in the current directory. 'ls' probably has an option to ignore directories, if so thats simpler and probably faster.

Its normally better to describe what you actually want to achieve, rather than something you've decided is the only possible solution.
 
The box I'm working on is Solaris and even though the script is running under bash, the maxdepth option is not being accepted.

find: bad option -maxdepth
find: [-H | -L] path-list predicate-list
 
The box I'm working on is Solaris and even though the script is running under bash, the maxdepth option is not being accepted.

What a brainache. Do you have a Linux box nearby? For one-time operations, I'd seriously considering just mounting the Solaris volume over the network and working on the files from there. Proper GNU find is just too useful

Code:
find . * -type f -maxdepth 0 -exec echo {} \;

Of course replace the echo {} with whatever you wanted to do with the files. "\;" is the delimiter to indicate the end of the exec command. {} denotes the found filename.

I've pickled this out. It's not as good as find and relies on your ls command having the -F/--classify/--file-type/etc flags:

Code:
ls -F | grep -v "/$"

Or this if you need hidden files:

Code:
ls -AF | grep -v "/$"
 
Back
Top Bottom