To achieve a recursive loop through directories, we will use bash loops, specifically, a for a loop. The for loop is a common type of loop in Bash and other programming languages. Next, we need to prompt the user for a valid directory to loop through. To accept user input, we use the echo command in Bash. With the concepts of loops and user input out of the way, we can put our shell together.
The first operation is to find files recursively with specific extensions and move them. Active Oldest Votes. Improve this answer. Because I had to look it up: "read Community Bot 1.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.
Linked 0. James Scriven James Scriven 7, 1 1 gold badge 29 29 silver badges 35 35 bronze badges. This does not work as expected if there is a space in the file name the for loop splits the results of find on whitespace. How do you avaoid splitting on whitespace? I'm trying a similar thing and I have a lot of directories with whitespaces that screw up this loop. I've edited James' answer to show.
Matthew your edit didn't fix anything at all: it actually made the command only work if there's a unique found file. At least this version works if there are no spaces, tabs, etc.
I rolled back to the old version. Just don't use this method. Show 3 more comments. Or find's -delete option. One should always use find Using xargs with no options is almost always bad advice and this is no exception. Use find … -exec instead. Gilles'SO-stopbeingevil': Why is that bad advice? Show 1 more comment. Tomek Tomek 1, 7 7 silver badges 6 6 bronze badges. This method worked for me, even with filenames containing spaces on OSX — ideasasylum.
Worth noting that globstar is only available in Bash 4. I dont think you need to specify the first argument. Wouldn't it be better like this? This question is tagged bash but it would be nice to point out that unlike several of the solutions here, this really is Bash-only.
Or, well, it works in several other extended shells, too. Show 2 more comments. Here is an example using shell bash :! Oliver Charlesworth Oliver Charlesworth k 30 30 gold badges silver badges bronze badges. This method handles spaces well. I think "-n" flag after echo not needed. Just test it yourself: with "-n" your script gives wrong number of files.
For exactly one file in directory it outputs "Count: 0" — Lopa. This doesn't work with all file names: it fails with spaces at the end of the name, with file names containing newlines and with some file names containing backslashes. These defects could be fixed but the whole approach is needlessly complex so it isn't worth bothering.
For bash since version 4.
0コメント