Some manipulations with files and folders. (loop, find, create and remove)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Some manipulations with files and folders. (loop, find, create and remove)
# 8  
Old 04-01-2011
Quote:
Originally Posted by optik77
subfolder 5677 is "bad" because has two pdf files, so I must move 5677 from $HOME/folder1 to $HOME/folder3 and go to 3456
I nearly understand this now and am working on something but, once you put 5677 into 3456, won't that make 3456 bad, causing it to be moved into the one below it, causing that one to be moved into the one below it, etc, etc, until the last folder below 5677 is one giant folder holding every file from above it?

Or once a folder's declared to be "bad" are you supposed to just move it into the next and then skip the next? That's what I'm assuming for the moment.

---------- Post updated at 10:23 AM ---------- Previous update was at 10:06 AM ----------

Code:
#!/bin/sh

MOVE=""
BASE="folder1"

# using ls instead of find, since ls sorts.  -r reverses the sort.
ls -r "${BASE}" | while read DIR
do
        TXT=""  ;       PDF=""  ;       TOOMANY=0

        DIR="${BASE}/${DIR}"

        # Skip things in base folder that aren't dirs
        [ -d "$DIR" ] || continue

        if [ ! -z "$MOVE" ]
        then
                # Move contents of $MOVE into $DIR, then skip $DIR

                # Remove the echo from these once you're sure it does
                # what you want
                echo mv "${MOVE}"/* "${DIR}"
                echo rmdir "${MOVE}"
                MOVE=""

                # Remove the continue if you want the domino effect
                continue
        fi

        for FILE in "${DIR}"/*
        do
                case "$FILE" in
                *.txt)  [ -z "$TXT" ] || TOOMANY=1
                        TXT="$FILE"
                        ;;
                *.pdf)  [ -z "$PDF" ] || TOOMANY=1
                        PDF="$FILE"
                        ;;
                *)      echo "$FILE not txt or pdf?"
                        ;;
                esac
        done

        [ -z "$TXT" -o -z "$PDF" ] && echo "Warning, not enough files in $DIR"

        if [ "$TOOMANY" -gt 0 ]
        then
                # Too many files in $DIR, set MOVE var for next loop
                MOVE="${DIR}"
        fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Find files in specific folders

Hi Team, I am new to the linux commands and I really need help . I would be really thankful if I can get some inputs. I have below folders in the path "/home/temp" 20170428 20170427 20170429 changes tempI need to get the files generated in the last 15 mins in all the above folders... (4 Replies)
Discussion started by: JackJinu
4 Replies

2. UNIX for Beginners Questions & Answers

Loop through the folders and search for particular string in files

Hello, Opearting System Environment : HP Unix B.11.31 U I look for script to On specific folders list On specific filelist Search for given string For Example : r48_buildlib.txt contains wpr480.0_20161027 wpr480.0_20161114 wpr481.0_20161208 wpr482.0_20161222... (4 Replies)
Discussion started by: Siva SQL
4 Replies

3. Shell Programming and Scripting

zipping files then remove the folders

Hi, i am using the below script to zip the files with respect to their timestamp of the files.It is working fine. For example lets take I have two files in this directory /path/folder1 with the timestamp as this month and previous month. When i run this script first time it is creating two... (7 Replies)
Discussion started by: velava
7 Replies

4. Shell Programming and Scripting

Loop folders, delete files, copy new ones

Folks, I am hopeful that you may be able to help me out with writing a script that can be run nightly (as cron?) to loop through all subfolders within the "/media" directory, delete all of the files in each of them, and then copy in all of the files from the "/home//sansa" directory to each of... (6 Replies)
Discussion started by: acraig
6 Replies

5. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

Hi, all: I've got two folders, say, "folder1" and "folder2". Under each, there are thousands of files. It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command. However, if I change the above question a... (1 Reply)
Discussion started by: jiapei100
1 Replies

6. Shell Programming and Scripting

how to remove inner files without removing child folders

Hi, I want to write a command to remove all the files from a folder and inner child folder without removing the child folders of parent folder. like I have folder like this... Code: folder a/b/c file a/test.sql file a/b/test2.txt file a/b/c/temp.jpeg now I want to remove... (5 Replies)
Discussion started by: mkashif
5 Replies

7. Red Hat

how to remove inner files without removing child folders

Hi, I want to write a command to remove all the files from a folder and inner child folder without removing the child folders of parent folder. like I have folder like this... folder a/b/c file a/test.sql file a/b/test2.txt file a/b/c/temp.jpeg now I want to remove all... (2 Replies)
Discussion started by: mkashif
2 Replies

8. Shell Programming and Scripting

Need to create a script to show what files in what folders

Hi everyone, I'm stuck with this scenario where our system creates files every night and puts them in several folders according from whom it came from. I have managed to create a script which will list the folders in one column and the files that are in them in another column, now my problem... (6 Replies)
Discussion started by: kumaran21
6 Replies

9. UNIX for Dummies Questions & Answers

how can i remove files with extension in many folders

hello i have 2 question if i have 1 folder and under this folder many many sub folders and in every folders many files with man extension like *php * jpg * gif i need to remove all *php files 1- from tha main folder only 2- from tha main folder and all sub folders the second how... (6 Replies)
Discussion started by: ateya
6 Replies

10. UNIX for Dummies Questions & Answers

copy all files and folders and cjange or remove ownership

So tried: cp -r -p test1/ user@machine:///srv/www/vhosts/domain.co.uk/httpdocs/backup/ but this didn't work either :( Anyone able to help with this? Many thanks Mr M (3 Replies)
Discussion started by: misterm
3 Replies
Login or Register to Ask a Question