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)
# 1  
Old 03-31-2011
Some manipulations with files and folders. (loop, find, create and remove)

Hello!

I need to realize such task.

1. In my user's home dir I have folder1;
2. In folder1 I have some (various count) subfolders with random names;
3. In these subfolders I have one file anyname.pdf (various name in each subfolder) and file content.txt (constant name in each subfolder)
## If in subfolder more than one .pdf or more than one .txt file, I must miss this subfolder (move it to Folder3 - it's one of the folders of my user's home dir) ;

So,
4. I must to scan every subfolders in folder1,
enter in each one;
5. If this subfolder has more than one .pdf file and more than one .txt file, I must go to next subfolder (with moving this subfolder to Folder3);
6. In "good" subfolders I must take file content.txt;
7. It has such structure:

dfdf{some trash}wqwq
begin_of_useful_info
info info ... info
end_of_useful_info

dfdf{some trash}wqwq

8. In this file content.txt I must find and cut only "useful_info" (between begin_of_useful_info and end_of_useful_info,including it):

begin_of_useful_info
info info ... info
end_of_useful_info

( begin_of_useful_info and end_of_useful_info is key-words on each content.txt, it must be cutted too)

9. this info I must put in new .txt file with name as .pdf file in this subfolder!

Example:
In folder1/4323353/ I have files GHTY34.pdf and content.txt. So, after operation I must get a file GHTY34.txt with useful_info)

10. delete parsed file content.txt
(so, now I have again two files in subfolder somename.pdf and somename.txt)

11. then I must copy files .pdf and new .txt with similar names somewhere, for example in folder2 (my user's home dir) and delete analized subfolder.
12. Go to next subfolder.

That's all Smilie
Thanks!

---------- Post updated at 01:55 PM ---------- Previous update was at 11:53 AM ----------

I write not workable code, it's just a plan

PHP Code:
#!/bin/sh\
cd folder1
find 
-type d | while read directory
do  
  
cd "${directory#"./"}"
  # check numbers of .txt and .pdf
  # create new file .txt and remove old .txt file
  
cp pdffile ../../folder2/pdffile
  cp txtfile 
../../folder2/txtfile
  mv 
"${directory#"./"}" ../../folder3/"${directory#"./"}"  
  
cd ..
done 
maybe it can help to understand the idea that I want to do.

Last edited by optik77; 03-31-2011 at 03:06 PM..
# 2  
Old 03-31-2011
Don't PM me to get faster answers. None of us are on-call here.

You don't need to strip the ./ off the beginning. ./ just means "in the current directory" so the meaning's the same.

Quote:
5. If this subfolder has more than one .pdf file and more than one .txt file, I must go to next subfolder (with moving this subfolder to Folder3);
What does "next subfolder" mean? A folder inside subfolder or a folder beside subfolder?
# 3  
Old 03-31-2011
Corona688 OK, sorry)

It means a folder beside subfolder.
Example ($HOME/folder1 has 4 subfolders: 33456, 9876, 5677, 3456):

PHP Code:
$HOME/folder1/33456/abc.pdf
$HOME
/folder1/33456/content.txt

$HOME
/folder1/9876/qwer.pdf
$HOME
/folder1/9876/content.txt

$HOME
/folder1/5677/port.pdf
$HOME
/folder1/5677/port2.pdf
$HOME
/folder1/5677/content.txt

$HOME
/folder1/3456/mn.pdf
$HOME
/folder1/3456/content.txt 
subfolder 5677 is "bad" because has two pdf files, so I must move 5677 from $HOME/folder1 to $HOME/folder3 and go to 3456
# 4  
Old 03-31-2011
So it's more than one pdf OR more than one txt, not AND?
# 5  
Old 03-31-2011
yes! you are right!
subfolder must contain only 2 files: one TXT and one PDF.
# 6  
Old 03-31-2011
Are there ever folders deeper than folder1/subfolder?
# 7  
Old 04-01-2011
No, there aren't.
only
HTML Code:
folder1/subfolder/file.(txt|pdf)
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